Department of
Software Engineering
Chapter Four
Structural Design Patterns
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 1
Department of
Software Engineering
Objectives
Describe structural design pattern and the types of it.
Discuss types of structural design patterns, the intent of each design pattern,
application area of each design pattern and its pros and cons.
Give real world examples for each structural design pattern and show with sample
codes
Apply those structural design patterns in different situations when you are
developing software systems and applications
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 2
Department of
Software Engineering
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.
concern class and object composition
Concept of inheritance is used to compose interfaces and define ways to compose objects to
obtain new functionalities.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 3
Department of
Software Engineering
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 4
Department of
Software Engineering
Feature Class Structural Pattern Object Structural Pattern
Technique Inheritance Object Composition
Flexibility Less flexible More flexible
When defined Design time Runtime
Example patterns Class Adapter Facade, Proxy, Bridge
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 5
Department of
Software Engineering
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 6
Department of
Software Engineering
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 7
Department of
Software Engineering
1. Adapter design pattern
It works as a bridge between two incompatible interfaces.
It combines the capability of two independent interfaces or classes to work together.
It involves a single class which is responsible to join functionalities of independent or
incompatible interfaces.
It provide the interface according to client requirement while using the services of a
class with a different interface.
It acts as an intermediary between two classes, converting the interface of one class
so that it can be used with the other.
It also called Wrapper.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 8
Department of
Software Engineering
1. Adapter design pattern
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 9
Department of
Software Engineering
1. Adapter design pattern
Real world examples
1. Mobile charging devices. If our charger is not
supported by a particular kind of switchboard, we need to use an adapter.
2. Even the translator who is translating language for one person.
3. Memory reader in computer or laptops
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 10
Department of
Software Engineering
1. Adapter design pattern
Credit card bank system UML diagram example for adapter (class) pattern
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 11
Department of
Software Engineering
2. Bridge design pattern…
It 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.
It involves an interface which acts as a bridge which makes the functionality of concrete
classes independent from abstract class
Both types of classes can be altered structurally without affecting each other.
It also known as Handle or Body.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 12
Department of
Software Engineering
2. Bridge design pattern…
Real world examples
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 fulfil the
customers’ needs.
In a software organization, the marketing team plays the role of the bridge between
the clients and the development team.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 13
Department of
Software Engineering
2. Bridge design pattern…
Advantages
It enables the separation of implementation from the interface.
It improves the extensibility.
It allows the hiding of implementation details from the client.
When it is applicable
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 14
Department of
Software Engineering
2. Bridge design pattern…
Quiz system UML diagram example for bridge pattern
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 15
Department of
Software Engineering
3. Composite design pattern…
It 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.
This pattern creates a class contains group of its own objects. This class provides ways to
modify its group of same objects.
Composite pattern is used where we need to treat a group of objects in similar way as a
single object.
Composite pattern composes objects in term of a tree structure to represent part as well as
whole hierarchy
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.
It is also known as a “has-a” relationship among objects
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 16
Department of
Software Engineering
3. Composite design pattern…
Real world examples
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(e.g college organization)
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 17
Department of
Software Engineering
3. Composite design pattern…
Advantages
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/interface
When it is applicable
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.
The structure can have any level of complexity, and is dynamic.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 18
Department of
Software Engineering
3. Composite design pattern…
Elements
Component - Declares interface for objects in composition.
o Implements default behavior for the interface common to all classes as appropriate.
o Declares an interface for accessing and managing its child components.
Leaf - Represents leaf objects in composition. A leaf has no children. Defines behavior for
primitive objects in the composition.
Composite - Defines behavior for components having children.
o Stores child component. Implements child related operations in the component interface.
Client - Manipulates objects in the composition through the component interface.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 19
Department of
Software Engineering
3. Composite design pattern…
Examples of UML diagram for employee in organization
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 20
Department of
Software Engineering
4. Decorator design pattern
It provides to attach a flexible additional responsibilities to an object dynamically.
It allows to add new functionality to an existing object without altering its structure.
It uses composition instead of inheritance to extend the functionality of an object at
runtime.
This main principle is that we cannot modify existing functionalities but we can extend
them.
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.
It is also known as Wrapper
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 21
Department of
Software Engineering
4. Decorator design pattern
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.
Add borders or scrollbars to a GUI component
Add stream functionality such as reading a line of input or compressing a file before sending it over
the wire.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 22
Department of
Software Engineering
4. Decorator design pattern
Advantages
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.
When it is applicable
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 23
Department of
Software Engineering
4. Decorator design pattern
UML diagram for food type
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 24
Department of
Software Engineering
5. Façade design pattern
It 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
Hides the complexities of the system and provides an interface to the client using which the
client can access the system.
adds an interface to exiting system to hide its complexities.
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 supports loose coupling.
Here we emphasize the abstraction and hide the complex details by exposing a simple interface.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 25
Department of
Software Engineering
5. Façade design pattern
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 26
Department of
Software Engineering
5. Façade design pattern
Advantages
It shields the clients from the complexities of the sub-system components.
It promotes loose coupling between subsystems and its clients
When it is applicable
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
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 27
Department of
Software Engineering
5. Façade design pattern
UML diagram for mobile shop
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 28
Department of
Software Engineering
6. Proxy design pattern
It 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.
It provides a surrogate or placeholder object to control access to the original object.
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.
It is also known as Surrogate or Placeholder.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 29
Department of
Software Engineering
6. Proxy design pattern
Real world examples
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 30
Department of
Software Engineering
6. Proxy design pattern
Advantage
For the protection of the original object from the outside world.
When it is applicable: it can be classified and used as
It can be used in Virtual Proxy scenario
o represent large object that should be loaded on demand
It can be used in Protective Proxy scenario
o protect access to the original object
It can be used in Remote Proxy scenario
o local representative for object in a different address space
It can be used in Smart Proxy scenario
o Adds additional protection mechanism to access to the original object
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 31
Department of
Software Engineering
6. Proxy design pattern
UML diagram for internet access
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 32
Department of
Software Engineering
7. Filter/criteria design pattern
It enables developers to filter a set of objects, using different criteria, chaining
them in a decoupled way through logical operations.
It is as combining multiple criteria to obtain single criteria.
It is useful when we want to select subset of objects that satisfies one or multiple
conditions.
Components in filter design pattern structure
Filter Interface : defines the method(s) for filtering objects.
Concrete Filters : to implement the filter interface.
Criteria Interface : defines the criteria objects. Criteria objects encapsulate individual filtering
criteria.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 33
Department of
Software Engineering
7. Filter/criteria design pattern
Concrete Criteria : implement the criteria interface. Each concrete criteria class represents a specific
filtering condition.
Filter Manager : An optional component that manages and combines different filters and criteria.
Object to be Filtered : The object or collection of objects that need to be filtered based on the specified
criteria.
Benefits
Better maintainability of code.
Better readability.
Allows you to make complex filters more easily.
Flexibility in Criteria Composition
Scalability and Dynamic criteria addition
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 34
Department of
Software Engineering
7. Filter/criteria design pattern
UML diagram
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 35
Department of
Software Engineering
8. Flyweight design pattern
It provides to reuse already existing similar kind of objects by storing them and
create new object when no matching object is found.
It reduces the number of low-level, detailed objects within a system by sharing
objects.
If instances of a class that contain the same information can be used interchangeably, the
Flyweight pattern allows a program to avoid the expense of multiple instances that contain
the same information by sharing one instance.
It defines a structure for sharing objects. Objects are shared for at least two
reasons: efficiency and consistency.
It focuses on sharing for space efficiency.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 36
Department of
Software Engineering
8. Flyweight design pattern
Advantage
It reduces the number of objects.
It reduces the amount of memory and storage devices required if the objects are
persisted
When it is applicable
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.
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 37
Department of
Software Engineering
8. Flyweight design pattern
UML diagram
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 38
Department of
Software Engineering
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 39
Department of
Software Engineering
Surprise Quiz
NO. Pattern type Question
1. A text editor allows adding scrolling, borders, and shadows to a window without modifying the
original window class. Each feature can be added dynamically. Which pattern supports this and
why?
2. A complex multimedia system includes audio processing, video decoding, and file management
modules. To simplify usage, the developer creates a single class with simple methods like
playMovie(). Which pattern is used here?
3. A cloud storage application loads large image files only when the user opens them, using a
placeholder object first. Which design pattern is demonstrated here and what problem does it solve?
4. A graphics application must display thousands of identical icons on the screen. Instead of creating
separate objects for each icon, the system shares a single object instance. Which pattern is applied
and why?
5. In a university management system, a faculty contains departments, and departments contain
instructors. The system allows operations like “calculate total staff” to be applied to both individual
instructors and entire departments. Which design pattern enables this uniform treatment?
Software Component Design & Pattern By: Kiros Shiferaw (MSc) 40