Lect09 CPP Pattern
Lect09 CPP Pattern
2022
Contents
2. Design Patterns
3. Creational Patterns
Object Oriented Design
Introduction
Object
Oriented
Design
Design Patterns
Creational
Patterns • Object oriented design is a process of planning a software system where
objects will interact with each other to solve specific problems.
Abstract factory
Builder
Factory method
Prototype • The saying goes,
“Proper object oriented design makes a developer’s life easy, whereas bad
Singleton
4
Single responsibility Principle (SRP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Principle
Builder
Factory method
“Every software module should have only one reason to change”.
Prototype
Singleton
• Software module: class, function etc.
• Reason to change: responsibility
5
Single responsibility Principle (SRP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Builder
Factory method
Prototype
Singleton
6
Open Close Principle (OCP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Principle
Builder
Factory method
“Software modules should be closed for modifications but open for exten-
Prototype
Singleton
sions.”
7
Open Close Principle (OCP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Builder
Factory method
Prototype
Singleton
8
Liskov substitution principle (LSP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Principle
Builder
Factory method
“Subclasses should be substitutable for base classes.”
Prototype
Singleton
9
Liskov substitution principle (LSP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Builder
Factory method
Prototype
Singleton
10
Interface Segregation principle (ISP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Principle
Builder
Factory method
“Clients should not be forced to implement interfaces they don’t use.”
Prototype
Singleton
• We should prefer many client interfaces rather than one general interface and
each interface should have a specific responsibility.
11
Interface Segregation principle (ISP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Builder
Factory method
Prototype
Singleton
12
Dependency Inversion principle (DIP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Principle
Builder
Factory method
“High-level modules should not depend upon low-level modules. Both
Prototype
Singleton
should depend upon abstractions.”
“Abstractions should not depend on details. Details should depend on
abstractions.”
13
Dependency Inversion principle (DIP)
Object
Oriented
Design
Design Patterns
Creational
Patterns
Abstract factory
Builder
Factory method
Prototype
Singleton
14
Design Patterns
Introduction
Object
Oriented
Design
Design Patterns
Creational
Patterns • One of the interesting things about software development is that when we
create a software system, we are actually modeling a real-world system.
Abstract factory
Builder
Factory method
Prototype • To write the business software systems, the developers must thoroughly
understand the business models.
Singleton
16
Why Design Patterns?
Object
Oriented
Design
Design Patterns
Creational
Patterns Each pattern describes a problem which occurs over and over again in our
Abstract factory
Builder
environment, and then describes the core of the solution to that problem.
Factory method
Prototype
• The pattern name is a handle we can use to describe a design problem, its
Singleton
solutions, and consequences in a word or two
• The problem describes when to apply the pattern. It explains the problem
and its content.
• The solution describes the elements that make up the design, their
relationships, responsibilities, and collaborations.
• The consequences are the results and trade-offs of applying the pattern.
17
Classification of patterns
Object
Oriented
Design
Design Patterns
Creational
Patterns Three main groups of patterns:
Abstract factory
Builder
• Creational patterns create objects for us, rather than having us instantiate
Factory method
Prototype objects directly. This gives our program more flexibility in deciding which
Singleton
objects need to be created for a given case.
• Structural patterns help us compose groups of objects into larger
structures, such as complex user interfaces or accounting data.
• Behavioral patterns help us define the communication between objects in
our system and how the flow is controlled in a complex program.
18
Creational Patterns
• Abstract factory
• Builder
• Factory method
• Prototype
• Singleton
Intent
Object
Oriented
Design
Design Patterns
Creational
Patterns • Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
Abstract factory
Builder
Factory method
Prototype • A hierarchy that encapsulates: many possible “platforms”, and the
construction of a suite of “products”.
Singleton
20
Problem
Object
Oriented
Design
Design Patterns
Creational
Patterns • If an application is to be portable, it needs to encapsulate platform
dependencies.
Abstract factory
Builder
Factory method
Prototype • These “platforms” might include: windowing system, operating system,
database, etc.
Singleton
21
Intent
Object
Oriented
Design
Design Patterns
Creational
Patterns • Separate the construction of a complex object from its representation so that
the same construction process can create different representations.
Abstract factory
Builder
Factory method
Prototype • Parse a complex representation, create one of several targets.
Singleton
22
Problem
Object
Oriented
Design
Design Patterns
Creational
Patterns • An application needs to create the elements of a complex aggregate.
Abstract factory
Builder • The specification for the aggregate exists on secondary storage and one of
Factory method
Prototype many representations needs to be built in primary storage.
Singleton
23
Intent
Object
Oriented
Design
Design Patterns
Creational
Patterns • Define an interface for creating an object, but let subclasses decide which
class to instantiate. Factory Method lets a class defer instantiation to
Abstract factory
Builder
Factory method
Prototype subclasses.
• Defining a “virtual” constructor.
Singleton
24
Problem
Object
Oriented
Design
Design Patterns
Creational
Patterns • A framework needs to standardize the architectural model for a range of
applications, but allow for individual applications to define their own domain
Abstract factory
Builder
Factory method
Prototype objects and provide for their instantiation.
Singleton
25
Intent
Object
Oriented
Design
Design Patterns
Creational
Patterns • Specify the kinds of objects to create using a prototypical instance, and
create new objects by copying this prototype.
Abstract factory
Builder
Factory method
Prototype • Co-opt one instance of a class for use as a breeder of all future instances.
Singleton
26
Problem
Object
Oriented
Design
Design Patterns
Creational
Patterns Application “hard wires” the class of object to create in each “new” expression.
Abstract factory
Builder
Factory method
Prototype
Singleton
27
Intent
Object
Oriented
Design
Design Patterns
Creational
Patterns • Ensure a class has only one instance, and provide a global point of access to
it.
Abstract factory
Builder
Factory method
Prototype • Encapsulated “just-in-time initialization” or “initialization on first use”.
Singleton
28
Problem
Object
Oriented
Design
Design Patterns
Creational
Patterns • Application needs one, and only one, instance of an object. Additionally, lazy
initialization and global access are necessary.
Abstract factory
Builder
Factory method
Prototype
Singleton
29
References
Deitel, P. (2016).
C++: How to program.
Pearson.
Gaddis, T. (2014).
Starting Out with C++ from Control Structures to Objects.
Addison-Wesley Professional, 8th edition.
Jones, B. (2014).
Sams teach yourself C++ in one hour a day.
Sams.