0% found this document useful (0 votes)
20 views

module 2 SA & DP (1)

The document provides a comprehensive catalog of design patterns used in software engineering, detailing their intents and functionalities. It covers various patterns such as Abstract Factory, Adapter, Bridge, and Composite, explaining how they assist in object management and system architecture. Additionally, it discusses structural patterns, their advantages, usage scenarios, and key components for patterns like Decorator, Facade, Flyweight, and Proxy.

Uploaded by

bhargavahero625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

module 2 SA & DP (1)

The document provides a comprehensive catalog of design patterns used in software engineering, detailing their intents and functionalities. It covers various patterns such as Abstract Factory, Adapter, Bridge, and Composite, explaining how they assist in object management and system architecture. Additionally, it discusses structural patterns, their advantages, usage scenarios, and key components for patterns like Decorator, Facade, Flyweight, and Proxy.

Uploaded by

bhargavahero625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Module 2

Catalog of Design Patterns:

The Design Patterns are organized into a form of a catalog. These


Design Patterns collectively assist in software engineering by finding
objects, specifying objects implementations, objects interfaces,
determining objects granularity, implementing reuse mechanisms.
The Intents specify what the design pattern does. Some of the patterns
with their names and intents are as follows:

 Abstract Factory: It indicates what factory is to be instantiated,


provides an interface to create families of objects without any
specification of their concrete classes.

 Adaptor: Id adapt or converts an interface of a class into


another one according to the client expectation and hence,
overcomes the problem of incompatible interfaces thereby
enabling the classes to work together.

 Bridge: It separates abstraction from its implementation to


make them independent.

 Builder: It separates the complex objects constructions from


their representation in order to create different representations
with the same construction process.
 Chain of Responsibility: It enables the handling of command
objects by passing them to other objects by using the logic
present in the processing of objects. Its decouples sender and
receiver by formatting a chain of receiving objects to pass the
request until the request is handled by an object.

 Command: It encapsulates the action and its parameters and


hence, enables to parameterize the different requests of the
clients such as long or queue requests. It also assists undoable
operations.

 Composite: It represents the objects in a tree structure where


each object represents the same interface. This enables clients to
treat individual objects and their compositions uniformly.

 Decorator: It adds additional functionality to a class at runtime.


This Enables flexibility to subclass for adding functionality.

 Facade: It creates a simplified/unified interface of existing


interfaces in the subsystems so as to handle common tasks
easily.

 Factory Method: It focuses on objects creation of specific


implementation. Lets the subclass decide as to which class to be
instantiated.

 Flyweight: It performs sharing of common objects properties by


a large number of objects to save space.
 Interpreter: It deals with the implementation of a specified
computer language that solves specific problems. It interprets
sentences in language by representing the grammar of language
along with an interpreter.

 Iterator: It enables sequential aggregate objects elements by


hiding their underlying representations.

 Mediator: It provides a unified interface to the set of interfaces


in a subsystem. It provides loose coupling which enables objects
to refer to each explicitly and also varies objects interaction
independently.

 Momento: It supports the rollback mechanism by enabling the


objects, to restore to their previous state without violation of
encapsulation.

 Observer: Whenever an object changes its state, it raises an


event that notifies other objects and updates them automatically.
This defines a one-to-many dependency between the objects.

 Prototype: Here prototypical instance determines the type of


objects to be created. Further new objects are created by cloning
this prototype.

 proxy: It provides an illusion by applying placeholder to other


objects in order to have control over it.

 Singleton: It provides restrictions on instantiating a class to a


single object and also makes it globally accessible.
 State: It Permits an alteration in the object’s behaviour with
alteration in its state.

 Visitors: It describes the skeleton of a program, enables


subclasses to define some steps of the algorithm, and also to
redefine certain steps without affecting the structure of the
algorithm.

 Strategy: It defines the Family of algorithms and their selection


based upon the clients.

Structural patterns:

Structural patterns are concerned with how classes and objects


can be composed, to form larger structures. The structural
pattern simplifies the structure by identifying the relationships. These
patterns focus on, how the classes inherit from each other and how
they are composed from other classes.

There are following 7 types of structural patterns.

1. Adapter Pattern: Adapting an interface into another according to


client expectation.

2. Bridge Pattern: Separating abstraction (interface) from


implementation.

3. Composite Pattern: Allowing clients to operate on hierarchy of


objects.
4. Decorator Pattern: Adding functionality to an object
dynamically.

5. Facade Pattern: Providing an interface to a set of interfaces.

6. Flyweight Pattern: Reusing an object by sharing it.

7. Proxy Pattern: Representing another object.

Adapter Pattern:

Adapter Pattern says that just "converts the interface of a class


into another interface that a client wants". In other words, to provide
the interface according to client requirement while using the services
of a class with a different interface. Adapter design pattern is
a structural pattern that allows the interface of an existing class to
be used as another interface.

Advantage of Adapter Pattern:

 It allows two or more previously incompatible objects to


interact.
 It allows reusability of existing functionality.

Usage of Adapter pattern:

 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.
 When you want to create a reusable class that cooperates with
classes which don't have compatible interfaces.

Components of Adapter Design Pattern


1. Target Interface
 Description: Defines the interface expected by the client. It
represents the set of operations that the client code can use.
 Role: It’s the common interface that the client code interacts with.
2. Adaptee
 Description: The existing class or system with an incompatible
interface that needs to be integrated into the new system.
 Role: It’s the class or system that the client code cannot directly
use due to interface mismatches.
3. Adapter
 Description: A class that implements the target interface and
internally uses an instance of the adaptee to make it compatible
with the target interface.
 Role: It acts as a bridge, adapting the interface of the adaptee to
match the target interface.
4. Client
 Description: The code that uses the target interface to interact
with objects. It remains unaware of the specific implementation
details of the adaptee and the adapter.
 Role: It’s the code that benefits from the integration of the
adaptee into the system through the adapter.

Bridge Patterns:

A Bridge Pattern says that just "decouple the functional abstraction


from the implementation so that the two can vary
independently". The Bridge design pattern allows you to separate
the abstraction from the implementation. It is a structural design
pattern.
There are 2 parts in Bridge design pattern:
1. Abstraction
2. Implementation
This is a design mechanism that encapsulates an implementation
class inside of an interface class.
 The bridge pattern allows the Abstraction and the Implementation
to be developed independently and the client code can access only
the Abstraction part without being concerned about the
Implementation part.
UML Diagram of Bridge Design Pattern:

Advantage of Bridge Pattern:

 It enables the separation of implementation from the interface.


 It improves the extensibility.
 It allows the hiding of implementation details from the client.

Usage of Bridge Pattern

o When you don't want a permanent binding between the


functional abstraction and its implementation.
o When both the functional abstraction and its implementation
need to extended using sub-classes.
o It is mostly used in those places where changes are made in the
implementation does not affect the clients.

Composite Patterns:

A Composite Pattern says that just "allow clients to operate in


generic manner on objects that may or may not represent a
hierarchy of objects". Composite Pattern is a structural design
pattern that allows you to compose objects into tree structures to
represent part-whole hierarchies. The main idea behind
the Composite Pattern is to build a tree structure of objects, where
individual objects and composite objects share a common interface.

Use Cases of Composite Patterns:


The Composite Pattern is useful in various scenarios, such as:
1. Graphics and GUI Libraries: Building complex graphical
structures like shapes and groups.
2. File Systems: Representing files, directories, and their
hierarchical relationships.
3. Organization Structures: Modeling hierarchical organizational
structures like departments, teams and employees.

Advantage of Composite Design Pattern:

 It defines class hierarchies that contain primitive and complex


objects.
 It makes easier to you to add new kinds of components.
 It provides flexibility of structure with manageable class or
interface.

Usage of Composite Pattern:

o When you want to represent a full or partial hierarchy of objects.


o When the responsibilities are needed to be added dynamically to
the individual objects without affecting other objects. Where the
responsibility of object may vary from time to time.

Decorator Patterns:

A Decorator Pattern says that just "attach a flexible additional


responsibilities to an object dynamically". The Decorator Pattern
uses composition instead of inheritance to extend the functionality of
an object at runtime. Decorator Design Pattern is a structural design
pattern that allows behaviour to be added to individual objects
dynamically, without affecting the behaviour of other objects from
the same class. It involves creating a set of decorator classes that are
used to wrap concrete components.
Advantage of Decorator Pattern:

 It provides greater flexibility than static inheritance.


 It enhances the extensibility of the object, because changes are
made by coding new classes.
 It simplifies the coding by allowing you to develop a series of
functionality from targeted classes instead of coding all of the
behaviour into the object.

Usage of Decorator Pattern:

o When you want to transparently and dynamically add


responsibilities to objects without affecting other objects.
o When you want to add responsibilities to an object that you may
want to change in future.
o Extending functionality by sub-classing is no longer practical.

Facade Patterns:
A Facade Pattern says that just "just provide a unified
and simplified interface to a set of interfaces in a subsystem,
therefore it hides the complexities of the subsystem from the
client". Facade Pattern describes a higher-level interface that
makes the sub-system easier to use. Practically, every Abstract
Factory is a type of Facade.

Facade Method Design Pattern is a part of the Gang of


Four design patterns and it is categorized under Structural
design patterns. Before we dive deep into the details of it,
imagine a building, the facade is the outer wall that people see,
but behind it is a complex network of wires, pipes, and other
systems that make the building function.

Advantage of Facade Patterns:

 It shields the clients from the complexities of the sub-system


components.
 It promotes loose coupling between subsystems and its clients.
Usage of Facade Pattern:

o When you want to provide simple interface to a complex sub-


system.
o When several dependencies exist between clients and the
implementation classes of an abstraction.

Flyweight Patterns:
A Flyweight Pattern says that just "to reuse already existing
similar kind of objects by storing them and create new object
when no matching object is found". Flyweight design pattern is a
structural pattern that focuses on optimizing memory usage by
sharing a common state among multiple objects. It aims to reduce the
number of objects created and to decrease memory footprint,
particularly useful when dealing with a large number of similar
objects.

Advantage of Flyweight Patterns:

 It reduces the number of objects.


 It reduces the amount of memory and storage devices required if
the objects are persisted.

Usage of Flyweight Patterns:

o When an application uses number of objects


o When the storage cost is high because of the quantity of objects.
o When the application does not depend on object identity.

Components of Flyweight Design Pattern


The Flyweight design pattern typically consists of the following
components:
1. Flyweight Interface/Class:
 Defines the interface through which flyweight objects can
receive and act on extrinsic state.
2. Concrete Flyweight Classes:
 Implements the Flyweight interface and represents objects that
can be shared.
 Stores intrinsic state (state that can be shared) and provides
methods to manipulate intrinsic state if needed.
3. Flyweight Factory:
 Manages a pool of flyweight objects.
 Provides methods for clients to retrieve or create flyweight
objects.
 Ensures flyweight objects are shared appropriately to maximize
reusability.
4. Client:
 Uses flyweight objects to perform operations.
 Maintains or passes extrinsic state to flyweight objects when
needed.
 Does not manage the lifecycle of flyweight objects directly but
interacts with them via the factory.

Proxy Patterns:

Proxy means an object representing another object. Proxy


Pattern "provides the control for accessing the original object".
So, we can perform many operations like hiding the information of
original object, on demand loading etc. Proxy pattern is also known
as Surrogate or Placeholder. RMI API uses proxy design pattern.

Proxy Design Pattern is a structural design pattern that provides


a surrogate or placeholder for another object to control access to it.
This pattern is useful when you want to add an extra layer of control
over access to an object. The proxy acts as an intermediary,
controlling access to the real object.

Advantage of Proxy Patterns:

 It provides the protection to the original object from the outside


world.

Usage of Proxy Patterns:

o Virtual Proxy scenario: Consider a situation where there is


multiple database call to extract huge size image.
o Protective Proxy scenario: It acts as an authorization layer to
verify that whether the actual user has access the appropriate
content or not.
o Remote Proxy scenario: A remote proxy can be thought about
the stub in the RPC call. The remote proxy provides a local
representation of the object which is present in the different
address location. Another example can be providing interface
for remote resources such as web service or REST resources.
o I Smart Proxy scenario: A smart proxy provides additional
layer of security by interposing specific actions when the object
is accessed. For example, to check whether the real object is
locked or not before accessing it so that no other objects can
change it.

Components of Proxy Design Patterns:


1. Subject
The Subject is an interface or an abstract class that defines the
common interface shared by the real subject and Proxy classes. It
declares the methods that the Proxy uses to control access to the real
subject.
 Declares the common interface for both real subject and Proxy.
 Usually includes the methods that the client code can invoke on
the real subject and the Proxy.
2. Real Subject
The real subject is the actual object that the Proxy represents. It
contains the real implementation of the business logic or the resource
that the client code wants to access.
 It Implements the operations declared by the Subject interface.
 Represents the real resource or object that the Proxy controls
access to.
3. Proxy
The Proxy acts as a surrogate or placeholder for the real Subject. It
controls access to the real object and may provide additional
functionality such as lazy loading, access control, or logging.
 Implements the same interface as the real subject.
 Maintains a reference to the real subject.
 Controls access to the real subject, adding additional logic if
necessary.

You might also like