23 design modes
the basic elements needed for understanding one design mode :
- what is the problem it solves ?
- how to solve ?
- how is the solving effect ?
Restrict to instantiate only one object
- the lazy mode: create when use
- static inner class
- DCL: double-checked lock
- the hungry mode: create when load
- static init
use
clone()
to reduce the time cost ofnew
, ifnew
is a time costing progress ,it helps a lot.the
clone()
ofObject
is shallow copying, if deep copying is needed , overridesclone()
with the Object IO stream and implementsSerializable
- simple
- manager
Give the factory different key to create different objects that implements the same interface. the factory need to know all the key and its Corresponding object's creation. the key is hardCode, and it is hard to maintain when the key set is gradually bigger.
the factory create an object implementing the interface, but do not expose the specific type of the object. so we can update or change the return type and the client is insensible. the shortcoming is that it needs to define factory for every product.
create a series objects, that are from the same platform or have some reason stick them together. the client only need to create the right factory, do not have to know the real type the factory creates.
reuse objects to reduce the operation times of
new
, if we need objects of the same class and only properties are different ,even the same.the flyweight model always keeps a
Map
of objects ,new
object for the first time and saves it in theMap
, returns the reference after.
save one object's status and recover after.
there are 3 roles in memento model:
- originator: the object to be saved.
- memento: the saved data.
- caretaker: the container of dates of different memento version.
the object can generate and recover from a memento by itself.
one object changed, notify the other objects. 1 to N
there are 2 roles in observer model:
- observable: as part "1"
- observer: as part "N"
the observable maintains a list of observers, and when it comes to changing something in the observable, the observable will notify all the observers in the list in order of the changes by a call to their
update
method with the args of the changed data or the references of itself.
decorator enriches methods of an existing class.
IO stream
andBuffered IO stream
is a paradigm of the model.the decorator and the origin class implement the same interface which contains the methods to be enriched. the decorator maintains an object of the origin class, and override the methods in interface with a call of methods of origin and other code of enriching operation.
make better controlling of the origin object or add some self operations when calling the delegated method.
One thought: Now I have a super talent coder and nobody knows him. Everyone can contact me and propose product requirements,even if I do not know coding, but I can make it perfectly with the talent . In this case, I am the delegator of him.
the code implements of delegate model are most similar to decorator model, but still have differences in encapsulation.
if one request or message will be handled with multiple receivers, string the receivers into a chain, and make the chain a replacer of the receivers and as a filter of the request.
reduces the complexity of communicating between multiple objects that communicate by referencing to each other, and will simplify the communication mode from "N to N" to "N to 1 to N".
separate the construction and representation of object.it means a new way to creat an object other than the normal flow as
new constructor(arg1,arg2)
,and the representation of an object could be more clear and readable for code --- no setter and a string of args when the object is complicated.
turn the implementation of an interface into another interface's.
extract the method of subclasses that have the same logic into their super class and modifies with
final
.
combines the classes extend from the same super abstract class or implement one same interface into a class ,and exposed methods for calling the origin methods of them.
use a composition relationship replaces an inheritance relationship to reduce the coupling between objects.
use an empty object to replace the real
Null
object, providing default properties and method body.
also known as filter model.use different criteria which can be easily combined with logical to filter a list of object.