Software Design Patterns
Software Design Patterns
By Mohammed Haneef
inheritance
to
compose
Structural object patterns describe ways to compose objects to realize new functionality.
By Mohammed Haneef
Adapter Pattern
An adapter helps two incompatible interfaces to work together, This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. The adapter is also responsible for transforming data into appropriate forms.
By Mohammed Haneef
Adapter Pattern
Implementation of adapter design pattern
By Mohammed Haneef
Adapter Pattern
1. Implementation using Inheritance Method
When a class with incompatible method needs to be used with another class you can use inheritance to create an adapter class.
The adapter class which is inherited will have new compatible methods. Using those new methods from the adapter the core function of the base class will be accessed. This is called is-a relationship.
By Mohammed Haneef
Adapter Pattern
2. Implementation using Composition Method
Implementation using composition can be done by inheriting the base class by creating adapter and by having the base class as attribute inside the adapter.
We can access all the methods by having it as an attribute. This is nothing but has-a relationship. In most scenarios, prefer composition over inheritance. Using composition you can change the behaviour of class easily if needed. It enables the usage of tools like dependency injection.
By Mohammed Haneef
Bridge Pattern
Decouple an abstraction from its implementation so that two can vary independently. In strategy pattern we decouple the behavior but in Bridge we decouple the abstraction. Creates two different hierarchies. One for abstraction and another for implementation. Avoids permanent binding by removing the dependency between abstraction and implementation. We create a bridge that coordinates between abstraction and implementation. Abstraction and implementation can be extended separately. Should be used when we have need to switch implementation at runtime. Client should not be impacted if there is modification in implementation of abstraction. Best used when you have multiple implementations.
By Mohammed Haneef
Bridge Pattern
Elements of Bridge Design Pattern
Abstraction core of the bridge design pattern and defines the crux. Contains a reference to the implementer. Refined Abstraction Extends the abstraction takes the finer detail one level below. Hides the finer elements from implemetors. Implementer - This interface is the higer level than abstraction. Just defines the basic operations.
By Mohammed Haneef
Bridge Pattern
By Mohammed Haneef
Decorator Pattern
To extend or modify the behaviour of an instance at runtime decorator design pattern is used. Inheritance is used to extend the abilities of a class. Unlike inheritance, you can choose any single object of a class and modify its behaviour leaving the other instances unmodified. In implementing the decorator pattern you construct a wrapper around an object by extending its behavior.
The wrapper will do its job before or after and delegate the call to the wrapped instance
By Mohammed Haneef
Decorator Pattern
The wrapping could be achieved by the following sequence of steps:
1. Subclass the original "Component" class into a "Decorator" class (see UML diagram). 2. In the Decorator class, add a Component pointer as a field. 3. Pass a Component to the Decorator constructor to initialize the Component pointer. 4. In the Decorator class, redirect all "Component" methods to the "Component" pointer. and 5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.
By Mohammed Haneef
Decorator Pattern
Decorator UML class diagram
By Mohammed Haneef
Facade Pattern
Facade provides a single interface.
By Mohammed Haneef
Facade Pattern
A common design goal is to minimize the communication and dependencies between subsystems. As you can see from the simplified diagram below the facade pattern is one way to achieve this, it provides a unified class which reduces communication between the complex subsystem and its clients. Clients do not have to be aware of the actual implementation of the subsystem and are provided with a simpler interface to this subsystem.
By Mohammed Haneef
Facade Pattern
Applicability / Uses
You want to provide a simple interface to a complex subsystem. The application of design patterns often results in a lot of small classes which makes subsystems more flexible and customizable. A facade can provide a default view for clients which don't need to customize. You want to reduce coupling between clients-subsystems or subsystems-subsystems.
You want to layer your subsystems. Use a facade to define an entry point to each subsystem level and make them communicate only through their facades, this can simplify the dependencies between them.
By Mohammed Haneef
Thank You
By Mohammed Haneef