Lecture 5 - Design Patterns
Lecture 5 - Design Patterns
Design Patterns
MAIN CONTENT
Creational patterns
Structural patterns
Behavioral patterns
INTRODUCTION TO DESIGN PATTERN
Example: JDBC
FACTORY PATTERN
Super class with many sub classes – return one of the sub-classes
Refer to newly created object using a common interface
Is used to define an interface or abstract class for creating an
object but let the subclasses decide which class to instantiate.
Usage:
When a class doesn't know what sub-classes will be required
to create
When a class wants that its sub-classes specify the objects to
be created
When the parent classes choose the creation of objects to its
sub-classes
FACTORY PATTERN
Example: Bank
BUILDER PATTERN
Constructs a complex object from simple objects using step-by-
step approach
Is mostly used when object can't be created in single step like in
the de-serialization of a complex object
Advantages:
Provides clear separation between the construction and
representation of an object
Provides better control over construction process
Supports to change the internal representation of objects
BUILDER PATTERN
Example: Pizza
PROTOTYPE PATTERN
Does cloning of an existing object instead of creating new one and
can also be customized as per the requirement.
Should be followed, if the cost of creating a new object is expensive
and resource intensive.
Usage:
When the classes are instantiated at runtime.
When the cost of creating an object is expensive or complicated.
When you want to keep the number of classes in an application
minimum.
When the client application needs to be unaware of object
creation and representation.
PROTOTYPE PATTERN
Example: Employee
STRUCTURAL PATTERN
Example: Employee
DECORATOR PATTERN
Example: Food
BRIDGE PATTERN
Example: Question
FACADE PATTERN
Provides a unified and simplified interface to a set of
interfaces in a subsystem, therefore it hides the complexities
of the subsystem from the client
Describes a higher-level interface that makes the sub-system
easier to use
Usage:
When you want to provide simple interface to a complex
sub-system.
When several dependencies exist between clients and the
implementation classes of an abstraction.
FACADE PATTERN
Example: MobileShop
FLYWEIGHT PATTERN
Reuses existing similar kind of objects by storing them and
create new object when no matching object is found
Advantages:
It reduces the number of objects.
It reduces the amount of memory and storage devices
required if the objects are persisted
Usage:
When an application uses number of objects
When the storage cost is high because of the quantity of
objects.
When the application does not depend on object identity.
PROXY PATTERN
Example: InternetAccess
BEHAVIORAL PATTERN
Example: Logger
COMMAND PATTERN
Encapsulates a request under an object as a command and pass
it to invoker object.
Invoker object looks for the appropriate object which can handle
this command and pass the command to the corresponding
object and that object executes the command
Usage:
When you need parameterize objects according to action
perform.
When you need to create, execute requests at different times.
When you need to support rollback, logging or transaction
functionality.
COMMAND PATTERN
Example: ActionListener
INTERPRETER PATTERN
Example: Pattern
ITERATOR PATTERN
Example: Iterator
MEDIATOR PATTERN
Example: ChatRoom
MEMENTO PATTERN
Example: Memento
OBSERVER PATTERN
Example: Observer
STATE PATTERN
Example: State
STRATEGY PATTERN
Example: Strategy
TEMPLATE PATTERN
Example: Game
THANK YOU
FOR LISTENING !