Design Patterns Part 2
Design Patterns Part 2
Part 2
By: Shereen Ali
Abstract Factory Design
• It is an interface or abstract class for creating families of related (or
dependent) objects but without specifying their concrete sub-classes.
• It means Abstract Factory lets a class returns a factory of classes.
• So, this is the reason that Abstract Factory Pattern is one level higher
than the Factory Pattern.
• An Abstract Factory Pattern is also known as Kit.
Advantage and Usage of Abstract
Factory
• Abstract Factory Pattern isolates the client code from concrete (implementation) classes.
• It eases the exchanging of object families.
• It promotes consistency among objects.
Usage of Abstract Factory Pattern
• When the system needs to be independent of how its object are created, composed,
and represented.
• When the family of related objects has to be used together, then this constraint needs
to be enforced.
• When you want to provide a library of objects that does not show implementations and
only reveals interfaces.
• When the system needs to be configured with one of a multiple family of objects.
Example
• We want calculating the loan payment for different banks like HDFC,
ICICI, SBI etc.
• We are going to create a Bank interface and a Loan abstract class as
well as their sub-classes.
• Then we will create AbstractFactory class as next step.
• Then after we will create concrete
classes, BankFactory, and LoanFactory that will
extends AbstractFactory class
• After that, AbstractFactoryPatternExample class uses
the FactoryCreator to get an object of AbstractFactory class.
UML
Java Code
• Step 1: Create a Bank interface
• Step 2: Create concrete classes that implement the Bank interface.
• Step 3: Create the Loan abstract class.
• Step 4: Create concrete classes that extend the Loan abstract class..
• Step 5: Create an abstract class (i.e AbstractFactory) to get the factories for Bank and
Loan Objects.
• Step 6: Create the factory classes that inherit AbstractFactory class to generate the
object of concrete class based on given information.
• Step7 : Create a FactoryCreator class to get the factories by passing an information such
as Bank or Loan.
• Step 8 : Use the FactoryCreator to get AbstractFactory in order to get factories of
concrete classes by passing an information such as type.
Structural Design Pattern
(Adapter Pattern)
• It converts the interface of a class into another interface that a client
wants.
• In other words, to provide the interface according to client
requirements while using the services of a class with a different
interface.
• The Adapter Pattern is also known as Wrapper.
Advantages and Usage
• It allows two or more previously incompatible objects to interact.
• It allows reusability of existing functionality.
Usage :
• When an object needs to utilize an existing class with an incompatible
interface.
• When you want to create a reusable class that cooperates with
classes which don't have compatible interfaces.
Some important feature in design
the UML
• There are the following specifications for the adapter pattern:
• Target Interface: This is the desired interface class which will be used
by the clients.
• Adapter class: This class is a wrapper class which implements the
desired target interface and modifies the specific request available
from the Adaptee class.
• Adaptee class: This is the class which is used by the Adapter class to
reuse the existing functionality and modify them for desired use.
• Client: This class will interact with the Adapter class.
UML
Coding Steps
• Step 1: Create a CreditCard interface (Target interface).
• Step2 : Create a BankDetails class (Adaptee class).
• Step3 : Create a BankCustomer class (Adapter class).
• Step 4: Create a AdapterPatternDemo class (client class).
Bridge Pattern
• A Bridge Pattern says that just "decouple the functional abstraction
from the implementation so that the two can vary independently".
• The Bridge Pattern is also known as Handle or Body.
Advantages and Usage
• It enables the separation of implementation from the interface.
• It allows the hiding of implementation details from the client.
Usage of Bridge Pattern
• When you don't want a permanent binding between the functional
abstraction and its implementation.
• When both the functional abstraction and its implementation need to
extended using sub-classes.
• It is mostly used in those places where changes are made in the
implementation does not affect the clients.
UML
Let’s Implement the Previous Design Patterns
Thank you