0% found this document useful (0 votes)
33 views42 pages

Chapter Three Part Three

This document discusses structural design patterns including adapter, bridge, composite, and decorator patterns. It provides examples of each pattern, describing their intents, participants, real world examples, advantages, and applicability. Unified Modeling Language diagrams are also included to illustrate the structure of each pattern.

Uploaded by

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

Chapter Three Part Three

This document discusses structural design patterns including adapter, bridge, composite, and decorator patterns. It provides examples of each pattern, describing their intents, participants, real world examples, advantages, and applicability. Unified Modeling Language diagrams are also included to illustrate the structure of each pattern.

Uploaded by

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

Chapter Three

Design Pattern

03/11/21 Software Component Design (SENG 4093) 2


Part Three
Structural Design Pattern

03/11/21 Software Component Design (SENG 4093) 3


Structural Design Pattern
Structural design patterns are concerned with
how classes and objects can be composed, to
form larger structures.
The structural design patterns simplifies the
structure by identifying the relationships.
As a simple example, consider how multiple
inheritance mixes two or more classes into
one.
These patterns focus on, how the classes inherit
from each other and how they are composed
from other classes.
Types of Structural Design Pattern
Adapter Pattern Adapting an interface into another
according to client expectation.
Bridge Pattern Separating abstraction (interface) from
implementation.
Composite Pattern Allowing clients to operate on
hierarchy of objects.
Decorator Pattern Adding functionality to an object
dynamically.
Facade Pattern Providing an interface to a set of
interfaces.
Flyweight Pattern:Reusing an object by sharing it.
proxy Pattern
Adapter Design Pattern (Structural)
Intent
An 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.
Also Known as
TheAdapter Pattern is also known as
Wrapper.
Types of Adapters Design Pattern
Adapters can be classified broadly into two categories
Class Adapter: adapt through sub classing. It is also
designed by sub classing the adaptee class.
In addition, a class adapter implements the interface
expected by the client object.
Object Adapter: Object adapters adapt through object
compositions.
An object adapter contains a reference to an adaptee object.
Similar to a class adapter, an object adapter also implements
the interface, which the client expects.
When a client object calls an object adapter method, the
object adapter invokes an appropriate method on the adaptee
instance whose reference it contains.
Real world example
The most common example of this type can be found with
mobile charging devices. If our charger is not
supported by a particular kind of switchboard, we need to
use an adapter.
Even the translator who is translating language for one
person is following this pattern in real life.
In real-life development, in many cases, we cannot
communicate between two interfaces directly.
They contain some kind of constraint within themselves.
To deal with this kind of incompatibility between those
interfaces, we may need to introduce adapters.
Real world example
In this example, we can calculate the area of a rectangle easily. If we see
the Calculator class and its getArea()
method, we’ll know that we need to supply a rectangle as an input in the
getArea() method to get the area
of the rectangle. Now suppose we want to calculate the area of a triangle,
but we need to get the area of the
triangle through the getArea() method of Calculator. How can we do that?
Answer
To do that we have made a CalculatorAdapter for the triangle and passed
a triangle in its getArea()
method. The method will translate the triangle input to rectangle input
and in turn, it will call the getArea()
of Calculator to get the area of it.
Advantage of Adapter Pattern
It allows two or more previously incompatible
objects to interact.
It allows reusability of existing functionality.
Applicability or Usage of Adapter pattern:
It is used:
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.
UML for Adapter Pattern
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 for Adapter Pattern
UML for Adapter Pattern
Step 1: Create a CreditCard interface (Target interface).
Step 2: Create a BankDetails class (Adaptee class).
Step 3: Create a BankCustomer class (Adapter class).
Step 4: Create a AdapterPatternDemo class (client class).

Bridge Design Pattern (Structural)
Intent
A Bridge Pattern says that just "decouple the functional
abstraction from the implementation so that the two
can vary independently".
The abstraction and the implementation can be represented
either through an interface or an abstract class, but the
abstraction contains a reference to its implementer.
a child of an abstraction is called a refined abstraction and
a child of an implementation is called a concrete
implementation.
Also Known as
The Bridge Pattern is also known as Handle or
Body.
Real world example
In a software product development company, the
development team and the marketing team both play a
crucial role. Marketing teams do market surveys and
gather customers’ needs, which may vary depending on
the nature of the customers.
Development teams implement those requirements in their
products to fulfill the customers’ needs.

In a software organization, the marketing team plays the


role of the bridge between the clients and the development
team.
Advantage and applicability of Bridge 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
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 for Bridge Pattern
UML for Bridge Pattern
Step 1: Create a Question interface that provides the
navigation from one question to another or vice-versa.
Step 2: Create a JavaQuestions implementation class that
will implement Question interface.
Step 3: Create a QuestionManager class that will use
Question interface which will act as a bridge.
Step 4: Create a QuestionFormat class that will extend the
QuestionManager class
Step 5: Create a BridgePatternDemo class.
Composite Design Pattern (Structural)
Intent
 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".
This pattern is useful to represent part-whole hierarchies
of objects.
In object-oriented programming, a composite is an object
with a composition of one-or-more similar objects, where
each of these objects has similar functionalities.
(This is also known as a “has-a” relationship among
objects).
Real world example
We can think of any organization that has many departments,
and in turn each department has many
employees to serve. Please note that actually all are employees of
the organization. Groupings of employees
create a department, and those departments ultimately can be
grouped together to build the whole
organization.
In this example we are showing a college organization. We have
a Principal and two Heads of Departments: one for computer
science and one for mathematics. At present, in the mathematics
department, we have two lecturers; in the computer science
department we have three lecturers. At the end, one lecturer
from the computer science department retires/leaves. We have
represented the scenario with the following simple example.
Advantage and applicability of Composite
Pattern
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.
It is used:
 When you want to represent a full or partial hierarchy of
objects.
 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.
Elements for Composite Pattern
1) Component:
 Declares interface for objects in composition.
 Implements default behavior for the interface common to all classes as
appropriate.
 Declares an interface for accessing and managing its child components.
2) Leaf
 Represents leaf objects in composition. A leaf has no children.
 Defines behavior for primitive objects in the composition.
3) Composite
 Defines behavior for components having children.
 Stores child component.
 Implements child related operations in the component interface.
4) Client
 Manipulates objects in the composition through the component
interface.
Elements for Composite Pattern
UML for Composite Pattern
Steps for Composite Pattern
Step 1: Create an Employee interface that will be treated
as a component.
Step 2: Create a BankManager class that will be treated as
a Composite and implements Employee interface.
Step 3: Create a Cashier class that will be treated as a leaf
and it will implement to the Employee interface.
Step 4: Create a Accountant class that will also be treated
as a leaf and it will implement to the Employee interface.
Step 5: Create a CompositePatternDemo class that will
also be treated as a Client and ii will use the Employee
interface.
Decorator Design Pattern (Structural)
Intent
 A Decorator Pattern says that just "attach a flexible additional responsibilities
to an object dynamically".
 In other words, The Decorator Pattern uses composition instead of inheritance

to extend the functionality of an object at runtime.


This main principle of this pattern says that we cannot modify
existing functionalities but we can extend
them. In other words, this pattern is open for extension but closed
for modification. The core concept applies when we want to add
some specific functionalities to some specific object instead of to the
whole class.
Also Known as
The Decorator Pattern is also known as Wrapper.
Real world example
Suppose you already own a house. Now you have decided to
build an additional floor on top of it. You may not want to
change the architecture of the ground floor (or existing
floors), but you may want to change the design of the
architecture for the newly added floor without affecting the
existing architecture.
Real world example

1. Original house 2. Original house with a decorator

3. Creating an additional decorator from an existing one (and


painting the house)
Advantage and applicability of Decorator
Pattern
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 behavior
into the object.
It is used:
When you want to transparently and dynamically add responsibilities
to objects without affecting other objects.
When you want to add responsibilities to an object that you may want to
change in future.
Extending functionality by sub-classing is no longer practical.
UML for Decorator Pattern
Steps for Decorator Pattern
Step 1: Create a Food interface.
Step 2: Create a VegFood class that will implements the Food
interface and override its all methods.
Step 3: Create a FoodDecorator abstract class that will implements
the Food interface and override it's all methods and it has the ability
to decorate some more foods.
Step 4: Create a NonVegFood concrete class that will extend the
FoodDecorator class and override it's all methods.
Step 5: Create a ChineeseFood concrete class that will extend the
FoodDecorator class and override it's all methods.
Step 6: Create a DecoratorPatternCustomer class that will use
Food interface to use which type of food customer wants means
(Decorates).
Facade Design Pattern (Structural)
Intent
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".
In other words, Facade Pattern describes a higher-level
interface that makes the sub-system easier to use.
Practically, every Abstract Factory is a type of Facade.
It is one of those patterns that supports loose coupling. Here
we emphasize the abstraction and hide the complex details by
exposing a simple interface.
Real world example
Suppose you are going to organize a birthday party and you
have invited 100 people. Nowadays, you can go to any party
organizer and let him/her know the minimum information
— (party type, date and time of the party, number of
attendees, etc.). The organizer will do the rest for you. You
do not even think about how he/she will decorate the party
room, whether people will take food from self-help counter
or will be served by a caterer, and so on.
Advantage and applicability of Facade Pattern
Advantage of Facade Pattern
It shields the clients from the complexities of the sub-system
components.
It promotes loose coupling between subsystems and its
clients.
It is used:
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.
UML for Facade Pattern
Steps for Facade Pattern
Step 1: Create a MobileShop interface.
Step 2: Create a Iphone implementation class that will
implement Mobileshop interface.
Step 3: Create a Samsung implementation class that will
implement Mobileshop interface.
Step 4: Create a Blackberry implementation class that will
implement Mobileshop interface .
Step 5: Create a ShopKeeper concrete class that will use
MobileShop interface.
Step 6: Creating a client that can purchase the mobiles from
MobileShop through ShopKeeper.
Proxy Design Pattern (Structural)
Intent
a 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.
A proxy is basically a substitute for an intended object. Access to the
original object is not always possible due to many factors. For
example, it is expensive to create, it is in need of being secured, it
resides in a remote location, and so forth. When a client deals with a
proxy object, it assumes that it is talking to the actual object.
Also Known as
This is also known as Surrogate or Placeholder.
Real world example
In a classroom, when a student is absent, his best friend may try to
mimic his voice during roll call to try to get attendance for his friend.
Consider an ATM implementation for a bank. Here we will find
multiple proxy objects. Actual bank information will be stored in a
remote server. We must remember that in the real programming
world, the creation of multiple instances of a complex object (heavy
object) is very costly. In such situations, we can create multiple proxy
objects (which must point to an original object) and the total creation
of actual objects can be carried out on a demand basis. Thus we can
save both the memory and creational time.
Advantage and applicability of Proxy Pattern
Advantage of Proxy Pattern
It provides the protection to the original object from the
outside world.
It is used:
It can be used in Virtual Proxy scenario
It can be used in Protective Proxy scenario
It can be used in Remote Proxy scenario
It can be used in Smart Proxy scenario
UML for Proxy Pattern
Steps for Proxy Pattern
Step 1: Create an OfficeInternetAccess interface.
Step 2: Create a RealInternetAccess class that will
implement OfficeInternetAccess interface for granting the
permission to the specific employee.
Step 3: Create a ProxyInternetAccess class that will
implement OfficeInternetAccess interface for providing
the object of RealInternetAccess class.
Step 4: Create a ProxyPatternClient class that can access
the internet actually.
03/11/21 42

You might also like