SOFTWARE DESIGN AND
ARCITECTURE(SDA)
LECTURE 6
ENGR.RAMSHA MASHOOD
SENIOR LECTURER
DEPARTMENT OF SOFTWARE ENGINEERING
BAHRIA UNIVERSITY KARACHI CAMPUS
July 30, 2024 1
DESIGN PATTERNS
July 30, 2024 2
WHAT IS DESIGN
PATTERN
DEFINITION
EACH PATTERN DESCRIBES A PROBLEM WHICH OCCURS
OVER AND OVER AGAIN, AND THEN DESCRIBES THE CORE
OF THE SOLUTION TO THAT PROBLEM, IN SUCH A WAY THAT
YOU CAN USE THAT SOLUTION MILLION TIMES OVER, WITH
OUT EVER DOING IT THE SAME WAY TWICE.
(CHRISTOPHER ALEXANDER)
(BOOK: DESIGN PATTERN BY ERICH GAMMA | RICHARD HELM)
July 30, 2024 4
DESIGN PATTERN
Shared language of design –
Increases communication
bandwidth – Decreases
misunderstandings
BENEFI
TS OF Learn from experience –
Becoming a good designer is
PATTER hard
NS
Understanding good designs is
a first step – Tested solutions
to common problems
07/30/2024 6
FOUR ESSENTIAL ELEMENTS:
• THE PATTERN NAME IS A HANDLE WE CAN USE TO
DESCRIBE A DESIGN PROBLEM, ITS SOLUTIONS, AND
CONSEQUENCES IN A WORD OR TWO. NAMING A PATTERN
IMMEDIATELY INCREASES OUR DESIGN VOCABULARY. IT
LETS US DESIGN AT A HIGHER LEVEL OF ABSTRACTION.
• THE PROBLEM DESCRIBES WHEN TO APPLY THE PATTERN.
IT EXPLAINS THE PROBLEM AND ITS CONTEXT. IT MIGHT
DESCRIBE SPECIFIC DESIGN PROBLEMS SUCH AS HOW TO
REPRESENT ALGORITHMS AS OBJECTS .
July 30, 2024 7
FOUR ESSENTIAL ELEMENTS:
• THE SOLUTION DESCRIBES THE ELEMENTS THAT MAKE UP THE DESIGN,
THEIR RELATIONSHIPS, RESPONSIBILITIES, AND COLLABORATIONS. THE
SOLUTION DOESN'T DESCRIBE A PARTICULAR CONCRETE DESIGN OR
IMPLEMENTATION, BECAUSE A PATTERN IS LIKE A TEMPLATE THAT CAN
BE APPLIED IN MANY DIFFERENT SITUATIONS
• THE CONSEQUENCES ARE THE RESULTS AND TRADE-OFFS OF APPLYING
THE PATTERN. THOUGH CONSEQUENCES ARE OFTEN UNVOICED WHEN
WE DESCRIBE DESIGN DECISIONS, THEY ARE CRITICAL FOR
EVALUATING DESIGN ALTERNATIVES AND FOR UNDERSTANDING THE
COSTS AND BENEFITS OF APPLYING THE PATTERN
July 30, 2024 8
WHY USE PATTERNS
DESCRIBING DESIGN PATTERNS
How do we describe design patterns?
• Graphical notations, while important and useful, aren't sufficient.
• They simply capture the product of the design process as
relationships between classes and objects.
• To reuse the design, we must also record the decisions,
alternatives, and trade-offs that led to it.
• Concrete examples are important too, because they help you see
the design in action. We describe design patterns using a
consistent format.
• Each pattern is divided into sections according to the following
template.
• The template lends a uniform structure to the information, making
design patterns easier to learn, compare, and use.
July 30, 2024 10
DESCRIBING DESIGN PATTERNS
• Pattern Name and Classification The pattern's name conveys the
essence of the pattern succinctly. A good name is vital, because it
will become part of your design vocabulary.
• Intent A short statement that answers the following questions:
What does the design pattern do? What is its rationale and intent?
What particular design issue or problem does it address?
• Also Known As Other well-known names for the pattern, if any.
• Motivation A scenario that illustrates a design problem and how
the class and object structuresin the pattern solve the problem.
The scenario will help you understand the more abstract
description of the pattern that follows.
• Applicability What are the situations in which the design pattern
can be applied? What are examples of poor designs that the
July 30, 2024 11
pattern can address? How can you recognize these situations?
DESCRIBING DESIGN PATTERNS
• Structure A graphical representation of the classes in the pattern
using a notation based on the Object Modeling Technique (OMT)
[RBP+91]. We also use interaction diagrams [JCJO92, Boo94] to
illustrate sequences of requests and collaborations between
objects.
• Participants The classes and/or objects participating in the design
pattern and their responsibilities.
• Collaborations How the participants collaborate to carry out their
responsibilities.
• Consequences How does the pattern support its objectives?
What are the trade-offs and results of using the pattern? What
aspect of system structure does it let you vary independently?
July 30, 2024 12
DESCRIBING DESIGN PATTERNS
• Implementation What pitfalls, hints, or techniques
should you be aware of when implementing the
pattern? Are there language-specific issues?
• Sample Code Code fragments that illustrate how you
might implement the pattern in C++ or Smalltalk.
• Known Uses Examples of the pattern found in real
systems. We include at least two examples from
different domains.
• Related Patterns What design patterns are closely
related to this one? What are the important differences?
With which other patterns should this one beJulyused?
30, 2024 13
Creational patterns provide
object creation mechanisms that
increase flexibility and reuse of
existing code.
CLASSIFICATI Structural patterns explain how
ON OF DESIGN to assemble objects and classes
PATTERNS into larger structures, while
keeping these structures flexible
and efficient.
Behavioral patterns take care of
effective communication and the
assignment of responsibilities
between objects.
CLASSIFICATION
Patterns can be classified
• With respect to purpose.
• With respect to scope.
With respect to purpose, patterns are classified to
Creational, Structural and Behavioral. For example,
• The Observer pattern is a behavioral pattern (because it help
us model the behavior and interactions of objects)
• The Builder pattern is a creational pattern (because it details
how an object can be created in a particular way) and so on.
July 30, 2024 16
CLASSIFICATION
CLASSIFICATION
CREATIONA
L PATTERN
July 30, 2024 19
SINGLETON
• SINGLETON IS A CREATIONAL DESIGN PATTERN THAT
LETS YOU ENSURE THAT A CLASS HAS ONLY ONE
INSTANCE, WHILE PROVIDING A GLOBAL ACCESS
POINT TO THIS INSTANCE.
July 30, 2024 20
SOLVE TWO PROBLEM
Ensure that a class has just a single instance.
Why would anyone want to control how many
instances a class has? The most common
reason for this is to control access to some
shared resource—for example, a database or a
file.
Provide a global access point to that
instance. Remember those global variables that
you (all right, me) used to store some essential
objects? While they’re very handy, they’re also
very unsafe since any code can potentially
overwrite the contents of those variables and
crash the app.
July 30, 2024 21
REAL WORLD ANALOGY
• The government is an excellent example of the Singleton pattern.
A country can have only one official government. Regardless of
the personal identities of the individuals who form governments,
the title, “The Government of X”, is a global point of access that
identifies the group of people in charge.
• Take a scenario, say for a Company application, there is only one
CEO. If you want to create or access CEO object, you should
return the same CEO object every time.
• One more, after logging into an application, current user must
return same object every time
July 30, 2024 22
July 30, 2024 23
FACTORY METHOD
In Factory pattern, we create object without exposing the
creation logic. In this pattern, an interface is used for
creating an object, but let subclass decide which class to
instantiate. The creation of object is done when it is required.
The Factory method allows a class later instantiation to
subclasses
July 30, 2024 24
July 30, 2024 25
July 30, 2024 26
STRUCTURE
July 30, 2024 27
ABSTRACT FACTORY
PATTERN
•PROVIDES AN INTERFACE FOR CREATING
FAMILIES OF RELATED OR DEPENDENT
OBJECTS WITHOUT SPECIFYING THEIR
CONCRETE CLASSES
•OR
•ABSTRACT FACTORY PATTERN IS A SUPER
FACTORY WHICH CREATES OTHER FACTORIES.
THIS FACTORY IS ALSO CALLED FACTORY OF
FACTORIES.
•ALSO KNOWN AS: KIT
07/30/2024 28
IN DEPTH
• CONSIDER AN INTERFACE TOOLKIT THAT SUPPORTS MULTIPLE LOOK-AND-FEEL
STANDARDS, SUCH AS MOTIF AND PRESENTATION MANAGER. DIFFERENT
LOOK-AND-FEELS DEFINE DIFFERENT APPEARANCES AND BEHAVIORS FOR
USER INTERFACE “WIDGETS” LIKE SCROLL BARS, WINDOWS AND BUTTONS. TO
BE PORTABLE ACROSS LOOK-AND-FEEL STANDARDS, AN APPLICATION SHOULD
NOT HARDCODE ITS WIDGETS FOR A PARTICULAR LOOK AND FEEL. THIS CAN
BE SOLVED BY DEFINING AN ABSTRACT WIDGET FACTORY CLASS THAT
DECLARES AN INTERFACE FOR CREATING EACH BASIC KIND OF WIDGET.
THERE’S ALSO AN ABSTRACT CLASS FOR EACH KIND OF WIDGET, AND
CONCRETE SUBCLASSES IMPLEMENT WIDGETS FOR SPECIFIC LOOK-AND-FEEL
STANDARDS. WIDGET FACTORY’S INTERFACE HAS NO OPERATION THAT
RETURNS A NEW WIDGET OBJECT FOR EACH ABSTRACT WIDGET CLASS.
CLIENTS CALL THESE OPERATIONS TO OBTAIN WIDGET INSTANCES, BUT
CLIENTS AREN’T AWARE OF THE CONCRETE CLASSES THEY’RE USING. THUS,
CLIENTS STAY INDEPENDENT OF THE PREVAILING LOOK AND FEEL07/30/2024 29
07/30/2024 30
07/30/2024 31
COLLABORATIONS:
NORMALLY A SINGLE INSTANCE OF A CONCRETE FACTORY
CLASS IS CREATED AT RUN-TIME
CONSEQUENCES:
THE ABSTRACT FACTORY PATTERN HAS THE FOLLOWING
BENEFITS AND LIABILITIES:
1. ISOLATING CONCRETE CLASSES
2. EXCHANGING PRODUCT FAMILIES EASY
3. PROMOTING CONSISTENCY AMONG PRODUCTS
4. SUPPORTING OF NEW KINDS OF PRODUCTS IS DIFFICULT
RELATED PATTERNS:
ABSTRACT FACTORY CLASSES ARE OFTEN IMPLEMENTED
07/30/2024 32
WITH FACTORY METHODS, BUT THEY CAN ALSO BE
IMPLEMENTED USING PROTOTYPE. A CONCRETE FACTORY
Factory Pattern:
1. Create object through inheritance
2. Produce only one product
3. Implements code in the abstract creator that make use
of the concrete type that sub class produces.
Abstract Factory Pattern:
1. Create object through composition
2. Produce families of products
3. Concrete factories implements factory method to create
product
07/30/2024 33
BUILDER
Builder is a creational design pattern that lets you
construct complex objects step by step. The
pattern allows you to produce different types and
representations of an object using the same
construction code.
July 30, 2024 34
BUILDER PATTERN
•BUILDER PATTERN BUILDS A COMPLEX
OBJECT USING SIMPLE OBJECTS AND
USING STEP BY STEP APPROACH.
•OR
•THE PROCESS OF CONSTRUCTING
OBJECT SHOULD BE GENERIC SO THAT
THE SAME PROCESS CAN BE USED TO
CREATE DIFFERENT REPRESENTATIONS
OF THE SAME COMPLEX OBJECT.
07/30/2024 35
July 30, 2024 36
July 30, 2024 37
EXAMPLE
07/30/2024 38
MOTIVATION
•THERE ARE TWO SPECIFIC PROBLEMS
THAT WE NEED TO SOLVE:
• TOO MANY CONSTRUCTOR
ARGUMENTS.
• INCORRECT OBJECT STATE.
•THIS IS WHERE THE BUILDER PATTERN
COMES INTO PLAY.
07/30/2024 39
UML
THE BUILDER DESIGN PATTERN USES THE FACTORY BUILDER PATTERN TO
DECIDE WHICH CONCRETE CLASS TO INITIATE IN ORDER TO BUILD THE
DESIRED TYPE OF OBJECT, AS WE WILL SEE BELOW IN THE UML DIAGRAM:
07/30/2024 40
IN DEPTH
THE PARTICIPANTS CLASSES IN THIS PATTERN ARE:
• THE BUILDER CLASS SPECIFIES AN ABSTRACT INTERFACE FOR
CREATING PARTS OF A PRODUCT OBJECT.
• THE CONCRETE BUILDER CONSTRUCTS AND PUTS TOGETHER PARTS
OF THE PRODUCT BY IMPLEMENTING THE BUILDER INTERFACE. IT
DEFINES AND KEEPS TRACK OF THE REPRESENTATION IT CREATES
AND PROVIDES AN INTERFACE FOR SAVING THE PRODUCT.
• THE DIRECTOR CLASS CONSTRUCTS THE COMPLEX OBJECT USING
THE BUILDER INTERFACE.
• THE PRODUCT REPRESENTS THE COMPLEX OBJECT THAT IS BEING
BUILT.
07/30/2024 41
July 30, 2024 42
PROTOTY
PE
PATTERN
•SPECIFYING THE KIND
OF OBJECTS TO CREATE
USING A
PROTOTYPICAL
INSTANCE. CREATING
NEW OBJECTS BY
COPYING THIS
PROTOTYPE
07/30/2024 43
UML
THE PATTERN USES ABSTRACT CLASSES, AS WE WILL SEE BELOW AND
ONLY THREE TYPES OF CLASSES MAKING ITS IMPLEMENTATION
RATHER EASY.
07/30/2024 44
IN DEPTH
THE CLASSES PARTICIPATING TO THE
PROTOTYPE PATTERN ARE:
• CLIENT - CREATES A NEW OBJECT BY
ASKING A PROTOTYPE TO CLONE ITSELF.
• PROTOTYPE - DECLARES AN INTERFACE
FOR CLONING ITSELF.
• CONCRETEPROTOTYPE - IMPLEMENTS
THE OPERATION FOR CLONING ITSELF.
07/30/2024 45
07/30/2024 46
07/30/2024 47
USEFUL LINKS
https://refactoring.guru/design-patterns
https://sourcemaking.com/design_patterns
July 30, 2024 48
CONTINUE…. ANY THING TO
ASK ??