CMPE 246 Lecture 10 - (Feb 6)
CMPE 246 Lecture 10 - (Feb 6)
Design Studio
Dr. Ling Bai
[email protected]
IEEE Member, ACM Member
Faculty of Applied Science | School of Engineering
The University of British Columbia, Okanagan Campus
1137 Alumni Avenue, Kelowna BC, V1V 1V7 Canada
Instructor Name: Ling Bai
2
Lecture 10
3
1- Start
4
1- Start
Object vs Class
5
1- Start
7
1- Start
Naturally, a real program contains
more than a single class.
Some of these classes might be
organized into class hierarchies.
9
1- Start
Object-oriented programming is based on four pillars.
10
1- Start
UML (Unified Modeling Language): Uses diagrams to visually represent
software structure and design.
- speed
- color
+ drive()
+ stop()
11
2- Design Patterns
Useful: you can use the module on Canvas; or ues these link
https://design-patterns-101.vercel.app/
https://refactoring.guru/design-patterns/catalog
12
2- Design Patterns
Design Pattern vs Algorithm
13
2- Design Patterns
14
2- Design Patterns
15
2- Design Patterns
You can say, “Oh, just use a Singleton for that,” and everyone will
understand the idea behind your suggestion. No need to explain what a
singleton is if you know the pattern and its name.
16
2- Design Patterns
Design patterns differ by their complexity, level of detail and scale of applicability.
In addition, they can be categorized by their intent and divided into three groups.
17
2- Design Patterns
A. Creational Patterns
18
2- Design Patterns
B. Structural Patterns
19
2- Design Patterns
C. Behavioral Patterns
20
3- Design Methods
A. Creational Patterns
5 design methods: Factory, Abstract Factory, Builder, Prototype, and Singleton.
21
3- Design Methods
A. Creational Patterns
a. Factory
Factory Method (also known as Virtual Constructor) is used to replace
class constructors.
22
3- Design Methods
A. Creational Patterns
a. Factory
Imagine that you’re creating a logistics management app. Adding a new
class to the program isn’t that simple if the rest of the code is already
coupled to existing classes.
23
3- Design Methods
A. Creational Patterns
a. Factory
Adding a new class to the program isn’t that simple if the rest of the code is
already coupled to existing classes.
24
3- Design Methods
A. Creational Patterns
a. Factory
The Factory Method suggests that you replace direct object construction
calls with calls to a special factory method.
25
3- Design Methods
A. Creational Patterns
a. Factory
26
3- Design Methods
A. Creational Patterns Factory Method that provides an interface for
creating objects in a superclass, but allows
a. Factory
subclasses to alter the type of objects that will be
Structure created.
27
3- Design Methods
A. Creational Patterns
b. Abstract Factory
An Abstract Factory has abstracted out a theme, and it produces families
of related or dependent objects without specifying their concrete classes.
28
3- Design Methods
A. Creational Patterns
b. Abstract Factory
Imagine that you’re creating a furniture shop simulator. Your code consists
of classes that represent:
variants.
3- Design Methods
A. Creational Patterns
b. Abstract Factory
You need a way to create individual furniture objects so that they match
other objects of the same family. Add new products and families?
30
3- Design Methods
A. Creational Patterns
b. Abstract Factory
The first thing the Abstract Factory suggests is to explicitly declare
interfaces for each distinct product of the product family
31
3- Design Methods
A. Creational Patterns
b. Abstract Factory
The next move is to declare the Abstract Factory—an interface with a list of
creation methods for all products that are part of the product family
32
3- Design Methods
A. Creational Patterns
b. Abstract Factory
The client code has to work with both factories and products via their
respective abstract interfaces.
33
3- Design Methods
A. Creational Patterns
b. Abstract Factory
Structure
34
3- Design Methods
A. Creational Patterns
c. Builder
Builder lets you construct complex objects step by step.
35
3- Design Methods
A. Creational Patterns
c. Builder
Imagine a complex object that requires laborious, step-by-step initialization
of many fields and nested objects.
36
3- Design Methods
A. Creational Patterns
c. Builder
For example, let’s think about how to create a House object.
38
3- Design Methods
A. Creational Patterns
c. Builder
Different builders execute the same task in various ways.
39
3- Design Methods
A. Creational Patterns
c. Builder
You can go further and extract a series of calls to the builder steps you use
to construct a product into a separate class called director.
41
3- Design Methods
A. Creational Patterns
d. Prototype
Prototype (also known as Clone) allows you to clone existing objects
without making your code dependent on their classes.
42
3- Design Methods
A. Creational Patterns
d. Prototype
Not all objects can be copied that way because some of the object’s fields
may be private and not visible from outside of the object itself.
Copying an object
“from the outside” isn’t
43
always possible.
3- Design Methods
A. Creational Patterns
The Prototype interface
d. Prototype declares the cloning
methods.
Structure
44
3- Design Methods
A. Creational Patterns
e. Singleton
Singleton restricts a class to only one single instance.
45
3- Design Methods
A. Creational Patterns
e. Singleton
Just like a global variable, the Singleton pattern lets you access some
object from anywhere in the program.
46
3- Design Methods
A. Creational Patterns
e. Singleton
Structure
48
3- Design Methods
B. Structural Patterns
a. Adapter
Adapter (also known as Wrapper or Translator) allows classes with
incompatible interfaces to collaborate.
49
3- Design Methods
B. Structural Patterns
a. Adapter
Imagine that you’re creating a stock market monitoring app.
You can’t use the analytics library “as is” because it expects the data in a format that’s
50
incompatible with your app.
3- Design Methods
B. Structural Patterns
a. Adapter
You can create an adapter. This is a special object that converts the
interface of one object so that another object can understand it.
51
3- Design Methods
B. Structural Patterns
a. Adapter
Structure
52
3- Design Methods
B. Structural Patterns
b. Bridge
Bridge is a structural design that lets you split a large class or a set of
closely related classes into two separate hierarchies—abstraction and
implementation—which can be developed independently of each other.
53
3- Design Methods
B. Structural Patterns
b. Bridge
56
3- Design Methods
B. Structural Patterns
c. Composite
Composite is a structural design that lets you compose objects into tree
structures and then work with these structures as if they were individual
objects.
57
3- Design Methods
B. Structural Patterns
c. Composite
58
create an ordering system with box and products
3- Design Methods
B. Structural Patterns
c. Composite
The Composite suggests that you work with Products and Boxes through
a common interface which declares a method for calculating the total
price.
59
3- Design Methods
B. Structural Patterns
c. Composite
Structure
60
3- Design Methods
B. Structural Patterns
d. Decorator
The Decorator pattern (also known as Wrapper) extends (or decorates)
an object’s behavior dynamically, without making a new subclass.
61
3- Design Methods
B. Structural Patterns
d. Decorator
Imagine that you’re working on a notification library which lets other
programs notify their users about important events.You realize that users
of the library expect more than just email notifications.
62
3- Design Methods
B. Structural Patterns
d. Decorator
Various notification
methods become
decorators.
63
3- Design Methods
B. Structural Patterns
d. Decorator
Structure
64
3- Design Methods
B. Structural Patterns
e. Facade
The Facade pattern provides a simplified interface to a library, a
framework, or any other complex system.
65
3- Design Methods
B. Structural Patterns
e. Facade
Imagine that you must make your code work with a broad set of objects
that belong to a sophisticated library or framework.
66
3- Design Methods
B. Structural Patterns
e. Facade
A facade is a class that provides a simple interface to a complex
subsystem which contains lots of moving parts.
67
3- Design Methods
B. Structural Patterns
e. Facade
Structure
68
3- Design Methods
B. Structural Patterns
f. Flyweight
The Flyweight reduces the cost of creating and manipulating a large
number of similar objects, sharing common parts of state between
multiple objects instead of keeping all of the data in each object.
69
3- Design Methods
B. Structural Patterns
f. Flyweight
You decided to create a simple video game: move map and shoot. You
chose to implement a realistic particle system and make it a distinctive
feature of the game.
70
3- Design Methods
B. Structural Patterns
f. Flyweight
71
3- Design Methods
B. Structural Patterns
f. Flyweight
Structure
72
3- Design Methods
B. Structural Patterns
g. Proxy
Proxy is a structural design that lets you provide a substitute or
placeholder for another object.
73
3- Design Methods
B. Structural Patterns
g. Proxy
You have a massive object that consumes a vast amount of system
resources. You need it from time to time, but not always.
75
3- Design Methods
B. Structural Patterns
g. Proxy
Structure
76
Next
!!!Please join the proposal session and team session, where you will
form groups and discuss any design-related advanced projects ideas.
We will release some ideas and collaboration tools for teamwork project, and
will emphasiz the scoring criteria based on syllabus.
77
Dr. Ling Bai
(email: [email protected])