In general terms, a design pattern is a solution to a frequently occurring problem. This means that if you know the problem, then you can decide on which design pattern is correct.
Here is a list of design patterns and the problems that they solve.
The builder pattern solves the problem of a number of constructors managing the optional parameters. Thus, the builder uses an inner class builder that creates the object step by step, one parameter at a time.
The factory method – if you want to create an object yet fail to specify the precise class type. Thus, the factory gives a method that uses polymorphism to create the needed object type.
The abstract factory – if you want to make a factory object yet don’t specify the precise type of class. Thus, the abstract provides a way of using polymorphism to create the type of wanted factory object.
The singleton – numerous objects over the system, but this is not as efficient as having one instance. The singleton only allows the object to be instantiated one time by using a singular method.
The prototype – a costly object creation. The prototype clones the original to make new instances.
The façade – with either multiple or complex interfaces (yet we need to unite and simplify). The façade defines a new interface that unites all the other interfaces by composition.
The decorator – if your class is to do more by using the same interface. The decorator uses composition rather than inheritance to prolong the function of a class, at the same time maintaining the interface.
The flyweight – in case of a large number of similar objects which will affect memory usage. The flyweight retains a list of objects to share, rather than creating a new object every time.
The bridge – if you have a number of class interfaces yet they don’t look like the one that you would like, or if the execution changes frequently and you want to abstract the interface. The bridge uses composition to make a fresh interface from the caller.
The adapter – when interfaces are not compatible or there are a few legacy interface codes.
The adapter uses composition and creates a new interface for the caller.
The proxy – when you lack control over an object operation.
The proxy wraps the object so that control function can be added, yet still maintaining the same interface.
The composite – if you have to manage a hierarchical based data structure.
The composite makes a structure that uses composition, and then implements the same interface in the branches and leaves.
The command – if you wish to either execute commands at a later time, run them at multiple times or delete operations. The command takes in the receiver and all its parameters.
The strategy – if you want to be able to choose an algorithm at runtime. The strategy it uses an interface to toggle objects using polymorphism.
The observer – when you would like objects to be able to receive events from a master object. The observer retains a list of observer objects, all kept in an observable object and then can notify them when needed.
The chain of responsibility – when the same data goes through a number of algorithms to find the right one. The chain of responsibility uses a linked list so that the data is passed by each object to the next object.
The interpreter – when you have to convert either data, process rules or expressions. The interpreter uses both operands and operators to implement the same interface, by the use of composition to create the expressions.
The mediator – when there are many classes and the communication between them all is complex. The mediator covers the communication with a mediator object and this object has a list of the other objects that it needs to communicate with.
The state – when you would like an object to have a different behavior if and when its state changes. The state allows each state an object that implements the same interface, but each uses different algorithms.
The template – when you need the same steps, but they are carried out in different ways. The template creates a superclass with the steps yet without the need to implement each of the steps.
The visitor – when you would like separate algorithms from object structures from which they operate, but would like to be able to add new functions yet not touch the object that has the structure. The visitor allows you to create a visitor object with the algorithm and permits a visitable object of the object structure, then it raises the visitor algorithms through the visitable.
Hi, I’m Amanda Clark. Blogging about technology and computing tips is my passion. You can contact me on my website 192168ll.net.