The chain of responsibility pattern is a design pattern that defines a linked list of handlers, each of which is able to process requests. When a request is submitted to the chain, it is passed to the first handler in the list that is able to process it.
- AbstractClass (has AbstractClass "next", invokes as next if condition is not met) -> ClassA
- AbstractClass (has AbstractClass "next", invokes as next if condition is not met) -> ClassB
- AbstractClass (has AbstractClass "next", invokes as next if condition is not met) -> ClassC
- objA.next(objB)
- objB.next(objC)
- objA.execute(); // process or pass - execute or next.
- http://www.blackwasp.co.uk/ChainOfResponsibility.aspx
- https://www.tutorialspoint.com/design_pattern/chain_of_responsibility_pattern.htm
- https://www.javatpoint.com/chain-of-responsibility-pattern
- https://refactoring.guru/design-patterns/chain-of-responsibility/java/example
- https://sourcemaking.com/design_patterns/chain_of_responsibility/java/2
- As the name suggests, the chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request.
- The design pattern promotes loose coupling by allowing a series of handlers to be created in a linked list or chain.
- The If-ElseIf-Else idiom is shown with this behaviour.
- Is it up to the programmer/problem what it should be implemented in the condition and under which circumstances the next object in the chain is invoked.
- Technical similarities: Decoration patterns has also chain call but it is a one unit as structure, here you can just exit the chain.
- Chained objects are passing the data from one link to another until the all links are processed or some of them consumed the process and breaked it and returned the value or changed state of some object.
The command pattern is a design pattern that enables all of the information for a request to be contained within a single object. The command can then be invoked as required, often as part of a batch of queued commands with rollback capabilities.
- BaseClass(do1, do2) - Receiver
- InterfaceOrder::execute -> DoOrder1(has BaseClase)::execute - wrapp BaseClass::do1
- InterfaceOrder::execute -> DoOrder2(has BaseClase)::execute - wrapp BaseClass::do2
- Invoker
- (ListOfOrders) Add the order to the list. Execute later together with other commands.
- (ListOfOrders) Add the order to the list and execute it.
- The invoker wraps the commands.
- http://www.blackwasp.co.uk/Command.aspx
- https://www.tutorialspoint.com/design_pattern/command_pattern.htm
- https://www.javatpoint.com/command-pattern
- https://refactoring.guru/design-patterns/command/java/example
- https://sourcemaking.com/design_patterns/command/java/1
- Command pattern is a data driven design pattern that enables all of the information for a request to be contained within a single object.
- The command can then be invoked as required, often as part of a batch of queued commands with rollback capabilities.
- The order is an interface which is implemented for any order. They orders are based on the methods of the base class.
- The Orders are not more but claserization of the methods of the base class.
- Centralazie functionality like "save text", can be used from button or other element.
- The command pattern is based on the reciever class which can be also named as base class. Based on its properties command classes are defined and used to execute the setter/getter of the properties.
The mediator pattern is a design pattern that promotes loose coupling of objects by removing the need for classes to communicate with each other directly. Instead, mediator objects are used to encapsulate and centralise the interactions between classes.
- IMediator::method0()-> ConcreteMediator::method0() // setter/getter on all Concrete Colleague classes
- ColleagueBase(has a Mediator m) -> ColleagueA (can call m.method0())
- ColleagueBase(has a Mediator m) -> ColleagueB (can call m.method0())
- ConcreteMediator::method0()
- Mediator m is created
- ColleagueA(m) cA is created, m.setCA(cA)
- ColleagueB(m) cB is created, m.setCA(cB)
- The methods od the Colleagues can be invoked and the data should go trough the mediator.
- http://www.blackwasp.co.uk/Mediator.aspx
- https://www.baeldung.com/java-mediator-pattern
- https://www.tutorialspoint.com/design_pattern/mediator_pattern.htm
- https://www.javatpoint.com/mediator-pattern
- https://refactoring.guru/design-patterns/mediator/java/example
- https://sourcemaking.com/design_patterns/mediator/java/2
- The mediator pattern is a design pattern that promotes loose coupling of objects by removing the need for classes to communicate with each other directly.
- Instead, mediator objects are used to encapsulate and centralise the interactions between classes.
- Any Colleague need to have the mediatorObj.
- On other side the mediatorObj (ConcreteMediator) has to contain the references of the Colleagues.
- Should the Mediator need to be singleton?
- Technical similarities: It looks very similar to observer pattern.
- Any data flow should get through the mediator class, thats why any of other classes have reference of the mediator and the mediator has references of them.
The observer pattern is a design pattern that defines a link between objects so that when one object's state changes, all dependent objects are updated automatically. This pattern allows communication between objects in a loosely coupled manner.
- Subject(has list of IObserver, method "notifyAll" - intrates trough the IObserver::update)
- IObserver(has Subject, method "update")
- IObserver -> ObserverA
- IObserver -> ObserverB
- IObserver -> ObserverC
- New Subject
- Create the Observer with the subject object as parameter and add this observer into the list of subjects.
- Setting the state in Subject invoked notifyAll.
- http://www.blackwasp.co.uk/Observer.aspx
- https://www.tutorialspoint.com/design_pattern/observer_pattern.htm
- https://www.javatpoint.com/observer-pattern
- https://refactoring.guru/design-patterns/observer/java/example
- https://sourcemaking.com/design_patterns/observer/java/1
- https://sourcemaking.com/design_patterns/observer/java/2
- The observer pattern is a design pattern that defines a link between objects so that when one object's state changes, all dependent objects are updated automatically.
- This pattern from the interface/class relation point of view looks very similar to the mediator pattern.
- To use this pattern in Java you can the implementation from util package:
- interface java.util.Observer
- public class java.util.Observable
- Technical similarities: It looks very similar to mediator pattern.
The visitor pattern is a design pattern that separates a set of structured data from the functionality that may be performed upon it. This promotes loose coupling and enables additional operations to be added without modifying the data classes.
- InterfaceElement(::accept(Visitor)) -> ConcreteElementA - lightWeight classes withou much functionallity
- InterfaceElement(::accept(Visitor)) -> ConcreteElementB
- InterfaceElement(::accept(Visitor)) -> ConcreteElementC
- InterfaceVisitor(visitMethod for any concrete Visitor) -> Visitor1 - where the functionallity is implemented
- InterfaceVisitor(visitMethod for any concrete Visitor) -> Visitor2
- Initialize Elements as List.
- Define Visitors
- Iterate trough the elements and invoke the accept with the Visitors.
- http://www.blackwasp.co.uk/Visitor.aspx
- https://www.tutorialspoint.com/design_pattern/visitor_pattern.htm
- https://refactoring.guru/design-patterns/visitor/java/example
- https://sourcemaking.com/design_patterns/visitor/java/1
- https://sourcemaking.com/design_patterns/visitor/java/3
- https://sourcemaking.com/design_patterns/visitor/java/4
- The visitor pattern is a design pattern that separates a set of structured data from the functionality that may be performed upon it.
- This promotes loose coupling and enables additional operations to be added without modifying the data classes.
- The logic is placed in the visitor and operates on the element which invoked the accept(Visitor) method. Any visitor implements elements specific methods for visting which means the number of the different methods is the same as for the different element types
The interpreter pattern is a design pattern that is useful when developing domain-specific languages or notations. The pattern allows the grammar for such a notation to be represented in an object-oriented fashion that can easily be extended.
- Interface::interpret(context) -> TerminalExoression::interpret(context)
- Interface::interpret(context) -> NonTerminalExoression1::interpret(context)
- Interface::interpret(context) -> NonTerminalExoression2::interpret(context)
- http://www.blackwasp.co.uk/Interpreter.aspx
- https://www.tutorialspoint.com/design_pattern/interpreter_pattern.htm
- https://www.javatpoint.com/interpreter-pattern
- https://sourcemaking.com/design_patterns/interpreter/java/2
- Interpreter pattern provides a way to evaluate language grammar or expression.
- This pattern involves implementing an expression interface which tells to interpret a particular context.
- This pattern is used in SQL parsing, symbol processing engine etc.
The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements.
- IStartegy(::calculation) -> Algorithm1
- IStartegy(::calculation) -> Algorithm2
- IStartegy(::calculation) -> Algorithm2
- Create Client.
- Use Algorithm class by purpose.
- http://www.blackwasp.co.uk/Strategy.aspx
- https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
- https://www.javatpoint.com/strategy-pattern
- https://refactoring.guru/design-patterns/strategy/java/example
- https://sourcemaking.com/design_patterns/strategy/java/1
- This pattern is used to summarize algorithm classes which have identlcal entry point.
- The choosing algorithm can be encapsulated into the object creation method as part of the factory pattern.
The template method pattern is a design pattern that allows a group of interchangeable, similarly structured, multi-step algorithms to be defined. Each algorithm follows the same series of actions but provides a different implementation of the steps.
- AbstractClass(::severalMethods)
- AbstractClass -> Algorithm1
- AbstractClass -> Algorithm2
- AbstractClass -> Algorithm3
- Use the propriate calculcations by choosing the related Algorith class.
- http://www.blackwasp.co.uk/TemplateMethod.aspx
- https://www.tutorialspoint.com/design_pattern/template_pattern.htm
- https://www.javatpoint.com/template-pattern
- https://refactoring.guru/design-patterns/template-method/java/example
- https://sourcemaking.com/design_patterns/template_method/java/2
- This pattern is used when the algortihms have similar structure where the method are reasonable spitted and exchangeable.
The memento pattern is a design pattern that permits the current state of an object to be stored without breaking the rules of encapsulation. The originating object can be modified as required but can be restored to the saved state at any time.
- Memento(set/get State) - State storage unit.
- Originator(hasMemeto - saveStateToMemento/getStateFromMemento, get/set State)
- CarteTaker(has lists of Memento, add, get)
- State structure needs to be same in both classes. 2 Originator on saveStateToMemento creates new Memmento with its current state and returns it. 2 Originator on getStateFromMemento sets the state from Memento as argument to Orginator.
- http://www.blackwasp.co.uk/Memento.aspx
- https://www.tutorialspoint.com/design_pattern/memento_pattern.htm
- https://www.javatpoint.com/memento-pattern
- https://refactoring.guru/design-patterns/memento/java/example
- https://sourcemaking.com/design_patterns/memento/java/1
- Memento pattern is used to restore state of an object to a previous state.
In Null Object pattern, a null object replaces check of NULL object instance. Instead of putting if check for a null value, Null Object reflects a do nothing relationship. Such Null object can also be used to provide default behaviour in case data is not available.
- AbstractClass -> ConcreteClass
- AbstractClass -> NullClass
- ClientClass(List)::get -> if nothing found then return NullClass.
- https://www.tutorialspoint.com/design_pattern/null_object_pattern.htm
- https://sourcemaking.com/design_patterns/null_object/java/1
- Design where "nothing will come of nothing".
- The Null object pattern is a design pattern that simplifies the use of dependencies that can be undefined. This is achieved by using instances of a concrete class that implements a known interface, instead of null references.
- The null object is a presentation of an object in case of null with well defined structure.
The state pattern is a design pattern that allows an object to completely change its behaviour depending upon its current internal state. By substituting classes within a defined context, the state object appears to change its type at run-time.
- ContextClass(has State)
- IState (has method defined which behave differently in the implemnetations)
- IState -> StateA
- IState -> StateB
- IState -> StateC
- Create ContextClass with init State.
- Invoke State1::doMethod(Context) changes state.
- Invoke State2::doMethod(Context) changes state.
- Invoke State3::doMethod(Context) changes state.
- http://www.blackwasp.co.uk/State.aspx
- https://www.tutorialspoint.com/design_pattern/state_pattern.htm
- https://www.javatpoint.com/state-pattern
- https://refactoring.guru/design-patterns/state/java/example
- Base class has state reference which changes, when necessary, by referencing other state object which implicit exchange of the method logic.
- On any different state invoking the same method from the context would have different output.
The iterator pattern is a design pattern that provides a means for the elements of an aggregate object to be accessed sequentially without knowledge of its structure. This allows traversing of lists, trees and other structures in a standard manner.
Straightforward solution:
- Interface Interator
- public boolean hasNext();
- public Object next();
- Interface Container
- public Iterator getIterator();
- Container -> ClassWithCollection
- getIterator returned InnerIterator()
- private class InnerIterator implements hasNext()/next()
- Client
- initialize - ClassWithCollection objWithCollection
- iterate - for(Iterator iter = objWithCollection.getIterator(); iter.hasNext();)
- http://www.blackwasp.co.uk/Iterator.aspx
- https://www.tutorialspoint.com/design_pattern/iterator_pattern.htm
- https://www.javatpoint.com/iterator-pattern
- https://refactoring.guru/design-patterns/iterator/java/example
- https://sourcemaking.com/design_patterns/iterator/java/2
- https://sourcemaking.com/design_patterns/iterator/java/1
- This pattern is used for collection iteration.
- Inner class is used for the Iterator, same level as the collection.