ECS 160 Design Pattern

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

21 Terms

1
New cards

Encapsulation

internal details of a class are hidden, access is controlled using public, private, protected

2
New cards

Abstraction

a simplified representation of something more complex. Abstractions allow you to hide details to help you manage complexity, focus on relevant concepts, and reason about problems at a higher level.

3
New cards

Inheritence

allows the reuse of code, this would be a child that extends of a parent

4
New cards

Abstract Class

a class that cannot be instantiated, only single inheritance allowed, can choose to override any methods already written but needs to implement any methods not yet implemented, must be extended, "is-a"

5
New cards

Abstraction via Interface

defines a contract for what a class must do but not how, multiple inheritances allowed, is a contract you must follow, must be implemented, "can do"

6
New cards

Runtime Polymorphism

The compiler cannot make the determination but instead the determination is made while the program is running

7
New cards

Static Class Member

single copy of the field is created, belongs to the class

8
New cards

What are the three types of design pattern categories and what are the differences?

creational (how objects are created), structural (how objects and classes are composed and deal with the relationships between objects), behavioural (control how objects distribute responsibilities)

9
New cards

Singleton DP, what situation you'd use, explain how to implement, the category, the pros and cons

this is when you want one object of a class to exist and accessed

case study is logger

creational DP

to implement we need a private constructor + private static object + getter,

pro: include controlled access to the instance

con: are potential bottlenecks and difficulty in testing and can create hidden dependencies

<p>this is when you want one object of a class to exist and accessed</p><p>case study is logger</p><p>creational DP</p><p>to implement we need a private constructor + private static object + getter,</p><p>pro: include controlled access to the instance</p><p>con: are potential bottlenecks and difficulty in testing and can create hidden dependencies</p>
10
New cards

Abstract Factory DP, what situation you'd use, explain how to implement, the category, the pros and cons

creates a family of related objects without the client having to know the exact classes being created

case study is UI toolkit

creational DP

we would have the abstract family class, an application class that contains the fields of the children, and the children will extend the application class and implement what is needed, the application class has a private object that tells us which child we should go to

pro: promotes consistency, catches error at compile time

con: more classes = more complexity

<p>creates a family of related objects without the client having to know the exact classes being created</p><p>case study is UI toolkit</p><p>creational DP</p><p>we would have the abstract family class, an application class that contains the fields of the children, and the children will extend the application class and implement what is needed, the application class has a private object that tells us which child we should go to</p><p>pro: promotes consistency, catches error at compile time</p><p>con: more classes = more complexity</p>
11
New cards

Adapter DP, what situation you'd use, explain how to implement, the category, the pros and cons

adapter converts the interface of a class to another interface the client expects

case study is payment with app and stripe

structural DP

we will create an adapter wrapper (stripeAdapter) that implements our interface (payProc) and in this will let us know what interface to go to, we wrap our third party in it (stripeProc), anytime we called our regular payProc it can check if it is the stripe

pro: don’t modify app logic, remove “if” logic

con: overcomplicated with many layers + wrappers

<p>adapter converts the interface of a class to another interface the client expects</p><p>case study is payment with app and stripe</p><p>structural DP</p><p>we will create an adapter wrapper (stripeAdapter) that implements our interface (payProc) and in this will let us know what interface to go to, we wrap our third party in it (stripeProc), anytime we called our regular payProc it can check if it is the stripe</p><p>pro: don’t modify app logic, remove “if” logic</p><p>con: overcomplicated with many layers + wrappers</p>
12
New cards

Proxy DP, what situation you'd use, explain how to implement, the category, the pros and cons

mediated by intercepting a call, user accesses the proxy instead of the real object, and some additional logic can be added

case is adding logging functionality

structural DP

we have our proxy object that extends our person class and overrides the functions, has the same API as the inner object but with some extra functionality

pro: adds additional functionality without changing class

con: additional classes = additional complexity, may need to override many objects

<p>mediated by intercepting a call, user accesses the proxy instead of the real object, and some additional logic can be added</p><p>case is adding logging functionality</p><p>structural DP</p><p>we have our proxy object that extends our person class and overrides the functions, has the same API as the inner object but with some extra functionality</p><p>pro: adds additional functionality without changing class</p><p>con: additional classes = additional complexity, may need to override many objects</p>
13
New cards

Bridge DP, what situation you'd use, explain how to implement, the category, the pros and cons

splits one hierarchy into two separate ones that are connected, helps prevent class exploision

case study is vehicle and energy source

structural DP
our vehicle class will have a private energy object, our energy class will have multiple types of energy children, our vehicle class will have multiple vehicle children

pro: decouples abstraction and implementation, improves extensibility

con: it is still more classes, can have combos we don’t want

<p>splits one hierarchy into two separate ones that are connected, helps prevent class exploision</p><p>case study is vehicle and energy source</p><p>structural DP<br>our vehicle class will have a private energy object, our energy class will have multiple types of energy children, our vehicle class will have multiple vehicle children</p><p>pro: decouples abstraction and implementation, improves extensibility</p><p>con: it is still more classes, can have combos we don’t want</p>
14
New cards

Facade DP, what situation you'd use, explain how to implement, the category, the pros and cons

a simple front door to a complex subsystem

case study is translation

structural DP

our client will have access to a class in which that contains all of the background stuff

pro: shields client from subsystem components, provides a unified interface, weak coupling between subsystem and client (client does’t need to know)

con: many tasks can mean a lot of orchestration in the background

15
New cards

Decorator DP, what situation you'd use, explain how to implement, the category, the pros and cons

dynamically attaches additional functionality by wrapping objects inside another object

case study is fraud detection

structural DP

create an wrapper abstract class decorator (PayProcDecor) that wraps the inner object which extends the interface (payProc), create the method decorator and extend to our wrapper decorator (fraudDetectDecor) and this will contain the additional functionality, you can stack decorators

pro: avoids feature-laden classes high up in the hierarchy and instead have subclasses, pay as you go approach

con: can be hard to debug

16
New cards

Observer DP, what situation you'd use, explain how to implement, the category, the pros and cons

when an object changes, all interested objects in that change are automatically notified, coupling between subject and observer

case study is the graphs

behavioural DP

we will have an overall class (dataModel) that contains a list of the observers, adding observers, removing, notifying graph loop, and setting the data which calls notifying, while we also have our observer interface with an update method, and our different subclass of graphs will implement the observer interface with its specific update functionality
pro: supports broadcast communication

con: can cause cascading updates without knowing, one trigger update can cause many others to update

<p>when an object changes, all interested objects in that change are automatically notified, coupling between subject and observer</p><p>case study is the graphs</p><p>behavioural DP</p><p>we will have an overall class (dataModel) that contains a list of the observers, adding observers, removing, notifying graph loop, and setting the data which calls notifying, while we also have our observer interface with an update method, and our different subclass of graphs will implement the observer interface with its specific update functionality<br>pro: supports broadcast communication</p><p>con: can cause cascading updates without knowing, one trigger update can cause many others to update</p>
17
New cards

State DP, what situation you'd use, explain how to implement, the category, the pros and cons

lets objects behave differently depending on its current state without filling the main class with lots of i else

case study is music player

behavioural DP

we will have an interface mediaPlayerState that contains subclasses for the different state (stop, play, pause), and then the different states are classes that extend the interface and overrides the methods, our main class will keep track of the current state with this

pro: can’t miss a state due to the interface, avoid large if-else, extensible in adding new states

con: more classes

<p>lets objects behave differently depending on its current state without filling the main class with lots of i else</p><p>case study is music player</p><p>behavioural DP</p><p>we will have an interface mediaPlayerState that contains subclasses for the different state (stop, play, pause), and then the different states are classes that extend the interface and overrides the methods, our main class will keep track of the current state with this</p><p>pro: can’t miss a state due to the interface, avoid large if-else, extensible in adding new states</p><p>con: more classes</p>
18
New cards

Template DP, what situation you'd use, explain how to implement, the category, the pros and cons

defines the overall steps of an algorithm but allows subclasses to change certain steps

case study is traversing source to dest note

behavioural DP

in our abstract class, we will have concrete methods that is shared between algorithm, and a protected abstract method that is different from class to class, in these separate class, we will override that protected method, we can have a template where subclasses can optionally implement the methods (hook) or they must implement the abstract methods (abstract operations)

pro: allows code reuse, we have an “inverted” structure where parents call the operation of the child class

con: lots of override and more subclasses

<p>defines the overall steps of an algorithm but allows subclasses to change certain steps</p><p>case study is traversing source to dest note</p><p>behavioural DP</p><p>in our abstract class, we will have concrete methods that is shared between algorithm, and a protected abstract method that is different from class to class, in these separate class, we will override that protected method, we can have a template where subclasses can optionally implement the  methods (hook) or they must implement the abstract methods (abstract operations)</p><p>pro: allows code reuse, we have an “inverted” structure where parents call the operation of the child class</p><p>con: lots of override and more subclasses</p>
19
New cards

Visitor DP, what situation you'd use, explain how to implement, the category, the pros and cons

all logic for one op should be kept together, “all in one place”

case study is shapes

behavioural DP

our abstract class visitor will contain the visits for all shapes and a visit method that loops thorough our shape have an abstract shape class that our shapes will extend

pro: keep logic all together

con: can get complicated

<p>all logic for one op should be kept together, “all in one place”</p><p>case study is shapes</p><p>behavioural DP</p><p>our abstract class visitor will contain the visits for all shapes and a visit method that loops thorough our shape have an abstract shape class that our shapes will extend</p><p>pro: keep logic all together</p><p>con: can get complicated</p>
20
New cards

Single Dispatch

we only check the runtime of the object being called on

Z w = new W()

X x = new Y()

w.p(x)

we will check the runtime of argument w which is object W, and we will go to the method p that takes in an argument X of the compile time which is object X

21
New cards

Double Dispatch

we check the runtime type of the objects

Z w = new W()

X x = new Y()

w.p(x)

we will check the run time of both, so we will go to object W’s, find the method p that takes in an object Y

because of this, we do not need an accept