Bridge:
Bridge decouples abstraction from implementation. Both abstraction and implementation can vary independently.
- Create bridge implementer interface
- Create concrete bridge implementer classes
- Create an abstract class which contains the interface ( Composition is key here)
- Create concrete class implementing the abstract class
If you are looking for examples, have a look at tutorialspoint article.
Visitor:
Visitor lets you define a new operation without changing the classes of the elements on which it operates.
From GOF definition:
Allows for one or more operation to be applied to a set of objects at runtime, decoupling the operations from the object structure.
The implementation proceeds as follows.
- Create a
Visitor
class hierarchy that defines a pure virtual visit()
method in the abstract base class for each concrete derived class in the aggregate node hierarchy.
- Each
visit()
method accepts a single argument - a pointer or reference to an original Element derived class.
- Each operation to be supported is modeled with a concrete derived class of the
Visitor
hierarchy. The visit
() methods declared in the Visitor base class are now defined in each derived subclass by allocating the "type query and cast" code in the original implementation to the appropriate overloaded visit() method.
- Add a single pure virtual
accept()
method to the base class of the Element hierarchy
For working java code, have a look at tutorials point article and dzone article.
Strategy:
Strategy allows to switch from one algorithm to other algorithm (from a family of algorithms) dynamically at run time.
Have a look at this SE question for more details.
Real World Example of the Strategy Pattern