0% found this document useful (0 votes)
89 views33 pages

Sda - 9

Uploaded by

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

Sda - 9

Uploaded by

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

SOFTWARE DESIGN AND ARCHITECTURE

LECTURE 27,28
Ms. Rabia Iftikhar
rabia.iftikhar@faculty.muet.edu.pk
Fair Use Notice

The material used in this presentation i.e., pictures/graphs/text, etc. is solely


intended for educational/teaching purpose, offered free of cost to the students for
use under special circumstances of Online Education due to COVID-19 Lockdown
situation and may include copyrighted material - the use of which may not have
been specifically authorised by Copyright Owners. It’s application constitutes Fair
Use of any such copyrighted material as provided in globally accepted law of many
countries. The contents of presentations are intended only for the attendees of the
class being conducted by the presenter.
Outlines
Structural Design Patterns
Adaptor,
Facade,
Decorator,
Composite,
Proxy
Structural Design Patterns
A structural design pattern serves as a blueprint for how different classes and objects are
combined to form larger structures.
Unlike creational patterns, which are mostly different ways to fulfill the same fundamental
purpose, each structural pattern has a different purpose. What unifies structural patterns? They
all involve connections between objects.
Structural patterns define how each component or entity should be structured so as to have
very flexible interconnecting modules which can work together in a larger system.
Adapter
Adapter is a structural design pattern that allows objects with incompatible interfaces
to collaborate.
Adapter
Applicability
Use the Adapter pattern when
you want to use an existing class, and its interface does not match the one you need.
(The Adapter pattern lets you create a middle-layer class that serves as a translator between your code and a legacy class,
a 3rd-party class or any other class with a weird interface.)

Use the pattern when you want to reuse several existing subclasses that lack some common functionality that
can’t be added to the superclass.
(You could extend each subclass and put the missing functionality into new child classes. However, you’ll need to
duplicate the code across all of these new classes, which smells really bad)
Example
Imagine that you’re creating a stock market monitoring app. The app downloads the stock data
from multiple sources in XML format and then displays nice-looking charts and diagrams for the
user.
At some point, you decide to improve the app by integrating a smart 3rd-party analytics library.
But there’s a catch: the analytics library only works with data in JSON format.
Let’s get back to our stock market app. To solve the dilemma of incompatible formats, you can create XML-to-
JSON adapters for every class of the analytics library that your code works with directly. Then you adjust your
code to communicate with the library only via these adapters. When an adapter receives a call, it translates the
incoming XML data into a JSON structure and passes the call to the appropriate methods of a wrapped analytics
object.
Pros and Cons
Single Responsibility Principle. You can separate the interface or data conversion code from the
primary business logic of the program.
 Open/Closed Principle. You can introduce new types of adapters into the program without
breaking the existing client code, as long as they work with the adapters through the client
interface.
 The overall complexity of the code increases because you need to introduce a set of new
interfaces and classes. Sometimes it’s simpler just to change the service class so that it matches
the rest of your code.
Adapter
Participants
Target
defines the domain-specific interface that Client uses.
Client
collaborates with objects conforming to the Target interface.
Adaptee
defines an existing interface that needs adapting.
Adapter
adapts the interface of Adaptee to the Target interface.
Adapter-Structure
Software Examples of Adapter
Patterns:
Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using
third party libraries use adapters as a middle layer between the application and the 3rd party
library to decouple the application from the library. If another library has to be used only an
adapter for the new library is required without having to change the application code.
Adapter- Example (UML Diagram)
Adapter- Example (UML Diagram)
This example includes following elements:
PrintableList(Target)
PrintString(Adaptee)
PrintableListAdapter(Adapter)
Façade Pattern
Intent
Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any
other complex set of classes.
Wrap a complicated subsystem with a simpler interface.
Façade Pattern
Motivation
Think of a component that solves a complex business problem. That component may expose lot of
interfaces to interact with it. To complete a process flow we may have to interact with multiple
interfaces.
To simplify that interaction process, we introduce facade layer. Facade exposes a simplified interface (in
this case a single interface to perform that multi-step process) and internally it interacts with those
components and gets the job done for you. It can be taken as one level of abstraction over an existing
layer.
Façade Pattern
Without façade layer With façade layer
Client classes

Subsystem
classes
Façade Pattern
Real World Examples
Lets take a car, starting a car involves multiple steps. The facade you have got is just a key hole.
Similarly consider microwave oven, it consists of components like transformer, capacitor, waveguide and
some more. To perform an operation these different components needs to be activated in a sequence.
What you have is a single switch that gets our job done. 
The customer service representative acts as a Facade, providing an interface to the order fulfillment
department, the billing department, and the shipping department.
These were real world examples for facade design pattern. In software scenario, you can have interfaces
which acts as a facade. Methods in these interfaces contains the interaction sequence, formatting and
converting data for input for components. As such it will not hold the business logic.
Façade Pattern
Applicability
Use the Facade pattern when

You want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. A
facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing
more customizability will need to look beyond the facade.
Façade Pattern- Structure
Façade Pattern- Example Structure
Composite Pattern
Intent
Composite is a structural design pattern that lets you compose objects into tree structures and then
work with these structures as if they were individual objects.

Problem
For example, imagine that you have two types of objects: Products and Boxes.
A box can contain several Products as well as a number of smaller Boxes. These little boxes can also hold
some products or even smaller boxes and so on…
Say you decide to create an ordering system that uses these classes. Orders could contain simple products without
any wrapping, as well as boxes stuffed with products...and other boxes. How would you determine the total price of
such an order?
Composite Pattern
Motivation
There are times when a program needs to manipulate a tree data structure and it is necessary to treat
both Branches as well as Leaf Nodes uniformly.
Consider for example a program that manipulates a file system. A file system is a tree structure that
contains Branches which are Folders as well as Leaf nodes which are Files. Note that a folder object
usually contains one or more file or folder objects and thus is a complex object where a file is a simple
object. Note also that since files and folders have many operations and attributes in common, such as
moving and copying a file or a folder, listing file or folder attributes such as file name and size, it would
be easier and more convenient to treat both file and folder objects uniformly by defining a File System
Resource Interface.
In graphics editors a shape can be basic or complex. An example of a simple shape is a line, where a
complex shape is a rectangle which is made of four line objects. Since shapes have many operations in
common such as rendering the shape to screen, and since shapes follow a part-whole hierarchy,
composite pattern can be used to enable the program to deal with all shapes uniformly.
Composite Pattern
Menus that contain menu items, each of which could be a menu.
Row-column GUI layout managers that contain widgets, each of which could be a row-column
GUI layout manager.
Directories that contain files, each of which could be a directory.
Containers that contain Elements, each of which could be a Container.
Composite Pattern
Applicability
Use the Composite pattern when
you want to represent part-whole hierarchies of objects.
you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients
will treat all objects in the composite structure uniformly.
Example
Composite Pattern- Structure
Composite Pattern- Structure
Pros and Cons
You can work with complex tree structures more conveniently: use polymorphism and recursion
to your advantage.
 Open/Closed Principle. You can introduce new element types into the app without breaking the
existing code, which now works with the object tree.
 It might be difficult to provide a common interface for classes whose functionality differs too
much. In certain scenarios, you’d need to overgeneralize the component interface, making it
harder to comprehend.

You might also like