0% found this document useful (0 votes)
15 views78 pages

CMPE 246 Lecture 10 - (Feb 6)

The document outlines the course CMPE 246, taught by Dr. Ling Bai at the University of British Columbia, focusing on computer engineering design principles. It covers key concepts such as object-oriented programming, design patterns, and various design methods including creational, structural, and behavioral patterns. Additionally, it provides information on office hours, appointment booking, and encourages students to participate in proposal and team sessions for project collaboration.

Uploaded by

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

CMPE 246 Lecture 10 - (Feb 6)

The document outlines the course CMPE 246, taught by Dr. Ling Bai at the University of British Columbia, focusing on computer engineering design principles. It covers key concepts such as object-oriented programming, design patterns, and various design methods including creational, structural, and behavioral patterns. Additionally, it provides information on office hours, appointment booking, and encourages students to participate in proposal and team sessions for project collaboration.

Uploaded by

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

CMPE 246 (3) Computer Engineering

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

Instructor Contact Information: [email protected]

Office Hours: By appointment


Please click on the link to book a 15-minutes
appointment during office hours ( EME 3280,
Friday, 2:30pm-4:30pm). You may book multiple
time slots if needed. If none of the available
times work for you, please feel free to email me.
[email protected]
Appointment booking link:
https://lingbai.youcanbook.me

2
Lecture 10

3
1- Start

4
1- Start
Object vs Class

5
1- Start

Data stored inside the object’s fields


is often referenced as state
(attributes), and all the object’s
6
methods define its behavior. UML class diagram
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.

UML diagram of a class hierarchy.


All classes in this diagram are part 8

of the Animal class hierarchy.


1- Start

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.

Pseudocode: Uses a text-based, human-readable format to describe an


algorithm or logic.
Car

- 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

Design patterns are general solutions to commonly occurring problems


in software design.

Design patterns are a toolkit of tried and tested solutions to common


problems in software design. Even if you never encounter these
problems, knowing patterns is still useful because it teaches you how to
solve all sorts of problems using principles of object-oriented design.

15
2- Design Patterns

Design patterns define a common language that you and your


teammates can use to communicate more efficiently.

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

Creational design patterns provide various object creation mechanisms, which


increase flexibility and reuse of existing code.

18
2- Design Patterns
B. Structural Patterns

Structural patterns address how to assemble classes and objects to form


larger structures and provide new functionality.

19
2- Design Patterns
C. Behavioral Patterns

Behavioral patterns identify and address communication and responsibilities


between objects.

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.

Factory Method that provides an interface for creating objects in a


superclass, but allows subclasses to alter the type of objects that will be
created.

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:

Product families and their 29

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.

The Builder separates the construction of an object from its representation.

Builder allows the same construction process to create various


representations.

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.

The constructor with lots of


parameters has its downside: not
all the parameters are needed at
all times.
37
3- Design Methods
A. Creational Patterns
c. Builder
The Builder suggests that you extract the object construction code out of its
own class and move it to separate objects called builders.

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.

The director knows


which building steps to
execute to get a
40
working product.
3- Design Methods
A. Creational Patterns
c. Builder
Structure

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.

New objects are created from an existing prototypical instance, thus


boosting performance and minimizing memory footprints.

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.

Singleton provides a global point of access to this instance.

Singleton is useful when exactly one object is needed to coordinate actions


across the system.

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

Make the default constructor


private.
Create a static creation method
47
that acts as a constructor.
3- Design Methods
B. Structural Patterns
7 methods: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and
Proxy.

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

Adding new shape types


and colors to the hierarchy
will grow it exponentially. 54
3- Design Methods
B. Structural Patterns
b. Bridge

Extend the shape classes in two independent dimensions: by form and by 55


color.
3- Design Methods
B. Structural Patterns
b. Bridge
Structure

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.

By masking interaction with more complex components in the larger


system, the facade improves readability and usability for the client.

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

The Flyweight suggests that


you stop storing the extrinsic
state inside the object.
Instead, you should pass this
state to specific methods
which rely on it.

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.

Database queries can be really slow. 74


3- Design Methods
B. Structural Patterns
g. Proxy
The Proxy suggests that you create a new proxy class with the same
interface as an original service object.

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])

You might also like