Ultimate Design Patterns - Behavioral Flashcards

1
Q

What are we going to learn in this course?

A

How to design re-usable, extensible software

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is not covered in this course?

A

No algorithms, no loops, no if statments, no logic

These topics are covered in other courses

We are not building an application!

Our focus is on building re-usable, easily extensible software

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What must every software engineer working with object oriented systems know?

A

How to structure classes so they can collaborate

How to design software that can be easily extended

Twenty-three classic design patterns (creational, relational, behavioral) all software engineers must know!

How to design reusable and extensible object-oriented software

Master these!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What has helped Mosh tremendously when learning new frameworks?

A

Knowing design patterns

Sees these patterns repeated in various frameworks and libraries

Every new syntax looks similar to him!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are design patterns?

A

Solutions to repeating features in software design

Design pattern shows us how to structure classes and how they communicate

Example

Undue feature (Memento Pattern) is a repeating feature in software design

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What design patterns are covered?

A

Gang of Four Patterns

23 design patterns across creational, structural and behavioral domains

Creational - different way to create objects

Structural - relationships between objects

Behavioral - communication or interaction between objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some of the benefits to mastering the classic design patterns?

A
  1. Can communicate without other developers - express your ideas abstractly using design patterns
  2. Makes you a better designer - Know how to build reusable and maintainable software no matter the language or framework you use!
  3. learn new frameworks faster - design patterns are used in different frameworks and languages, knowing them helps you learn them faster!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How should we take this course?

A

Take it beginning to end

Various principles are discussed in each section

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a design smell?

A

overly complicated design

Many moving parts

design patterns applied without context

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the focus of this course?

A

The art of designing object oriented software

Patterns ordered to teach us important concepts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if we blindly apply design patterns to our applications?

A

Going to increase complexity

Now more moving parts

Haven’t solved the problem

Created an overly complicated design

aka “design smell”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does a real engineer do?

A
  1. Try to understand the problem
  2. Brainstorm various solutions
  3. Pick the one that best fits the problem

How we should engineer software

Apply design patterns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the Behavior Design Patterns?

A

All about interaction (communication) between classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the Structural Design Patterns?

A

All about the relationship between classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How should we apply design patterns?

A
  1. Try to understand the problem
  2. Brainstorm various design pattern solutions
  3. Pick the design pattern that best fits the problem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What do code monkeys do?

A

Write code

Not understanding the problem they are trying to solve

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is abusing design patterns?

A

A few decision making statements in a single place, nothing wrong!

In State pattern, had decision making statements in multiple methods AND to support a new tool had to modify code!

All decision making was in single place, will not have a new state in the future so no maintainability or extensibility issues!

Code is now overly complicated without benefits

More moving parts

Logic spread over two classes!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What did Leonardo DaVinci say?

A

Simplicity is the ultimate sophistication

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What should we not do?

A

Abuse design patterns!

Use them without understanding the problem, context and the various solutions!

Use them to improve extensibility and maintainability NOT to make complex applications with no additional benefts!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a tightly coupled application?

A

One in which all the classes are dependent on eachother!

If you change one class, cascading changes happen in other classes

Ex. If you get a flat tire, you only replace one tire

Not all the other tires, the engine and the seatbelt

A car is loosely coupled. Each part isn’t dependent on the other parts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can we build loosely coupled applications?

A

Using interfaces!

Components work together but are not dependent on eachother!

Can replace individual parts separately!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are interfaces?

A

A contract that specifies the capabilities that a class should provide as services

Highly misunderstood construct in Java and other languages!

Ex. Open a restaurant, need a cheff

Not dependent on a particular cheff, just someone with the particular capabilities

This is a loosely coupled system!

Represent role of a cheff as interface

actual cheffs, classes that implement cheff interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When should we use an interface vs. abstract class?

A

allow us to build loosely coupled applications!

Use abstract class if want to provide common code to child classes

methods in an interface are considered abstract (cannot instantiate them) so can omit abstract keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the open close principle?

A

Very important!

Open for extension - can add new classes

Closed for modification - cannot change code in classes

Support new feature?

add new classes, test them!

Makes application robust and extensible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the three UML relationships?

A

Inheritance - methods defined in super class

composition - made of another class

dendency - use another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How can we represent inheritance using UML?

A

An arrow from child to parent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How is the composition relationship modeled using UML?

A

An arrow with a diamond ending

Shape is composed of size class

In Shape class, have field called size

What is it?

ex. Car class is composed of wheel class, because every car has four wheels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do we represent a class in UML?

A

Top - name of class

middle section - fields (type of field)

below - methods (parameter or return type

  • = private

+ = public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do we represent the dependency relationship?

A

Means?

Somewhere in Shape class, reference Document class

render method’s parameter is dependent on Document class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What do we use the memento pattern for doing?

A

Building undue features!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the single responsibility principle?

A

An important principle in OOP

Says:

every class should have a single responsibility

Ex. Restaurant

Waitor only takes order, doesn’t do taxes, shopping, cooking!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

To design extensible software in what ways should we design our classes?

A

Design our claseses such that each class has only a single responsibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is the memento pattern?

A

Used to implement undue

Originator - Editor

Memento - EditorState

Caretaker - History

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How do we implement the memento pattern?

A

History - keeps track of changes in editor, stores editorState objects

EditorState -

Editor -

.restore(state) takes state object and resets fields based on what is in state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Implement the undue feature using the memento pattern for content, fontName and fontSize.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is an enum?

A

A set of constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the problem with this code?

A

It’s not maintainable and lacks extensibility!

Why?

not easy to maintain and extend = The more tools we add, the more decision making statements will be required and we have to go to different methods and make changes

ex. change mouseDown ( ) and mouseUp ( ) to accomodate a new tool

Soln?

Ploymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What is the state pattern?

A

Allows an object to behave differently depending on the state

always use meaningful names that fit the domain of your application!

Textbook names are too general.

39
Q

How do we implement the State Pattern?

A

Don’t have a long list of decision statements

Code is more maintainable

Can easily extend

40
Q

When do we use the State pattern?

A

To implement behaving differently depending on the state of another object

41
Q

What is an excercise?

A
42
Q

What is the problem here?

A

everytime we change the internals of the class, we get breaking changes!

Changing the internals of a class shouldn’t affect its consumers!

If we replace the original datastructure storing urls we will have breaking changes in our main class

Ex. Two breaking changes in main class by replacing an arrayList with a fixed array internally!

43
Q

What problem does the iterator pattern solve?

A

everytime we change the internals of the class, we get breaking changes!

Changing the internals of a class shouldn’t affect its consumers!

Encapsulating breaking changes within a class so that consumers of our class are not affected

Using Iterator Pattern - don’t need to know the internal structure of the class (array, stack, etc.)

hasNext ( ) - returns boolean

next ( ) - returns void

Telling history item - give me another item, give me another item

44
Q

What’s the problem with this class?

A

It breaks the single responsibility criteria

It’s responsible for history management

It’s responsible for iteration

45
Q

How do we solve the single responsibility program?

A

When createIterator ( ) is called, Iterator object is returned

Programmed to an interface!

46
Q

What is the iterator pattern?

A
47
Q

How can we change this code to implement the iterator pattern?

A
48
Q

What are the benefits of the iterator pattern?

A
  1. breaking changes are localized to one class - other classes do not break if changes are made to internal structure of class

Can change the internal iterator from List to ArrayIterator!

49
Q

What is an excercise?

A
50
Q

What are the issues with this code?

A
  1. Single Responsibility Principle Violation - this class is responsible for storing an image AND compressing an image/applying a filter!

As we support more image processing algorithms, if/else if statements will get very long and hard to maintain!

2. Difficult to extend number of filters - Have to make changes in different parts of class.

The more filters we support, the more bulky our class will become!

OOP SOLN:

Polymorphism!

51
Q

What is the Strategy Pattern?

A

Context maintains reference to strategy object

Strategy interface represents an algorithm

Different classes implements the algorithm

We use both patterns to change behavior of objects

Different behaviors are represented by different strategy objects vs. one state object (State Pattern)

52
Q

What is the difference between the Strategy Pattern and the State Pattern?

A

Strategy pattern - different behaviors are represented by different strategy objects

State pattern - All behaviors are represented by a subclass of state interface

53
Q

How can we apply the Strategy Pattern to make this design more extensible and maintainable?

A

This is open-closed principle!

54
Q

How can we save the same image in two different file formats?

A

Update the ImageStorage.store method to take Compressor and Filter Objects (code against interface)

Can pass alternative classes implementing the Interfaces to the method

55
Q

What is an excercise?

A

Instead of Filter / Compressor Interfaces create Encryptor Interface w/ multiple implementing classes (64-bit encryption, 32-bit encryption)

56
Q

What problem does the Template Method Pattern solve?

A

Code duplication

Solve How?

OOP Inheritance!

Ex. Everytime we create a new task, we have to instantiate a new auditTrail and call the .record method! This is repetative

57
Q

What is the Template Method Pattern?

A

Abstract class with a Template Method

or skeleton for an operation

Hooks = default method implementation, childClass can override if required (very common in Java, Javascript, etc.)

58
Q

Are private methods inherited by sub-classes?

A

No - private methods are not inherited by subclasses!

if we declare our abstract method as private we won’t be able to over-ride it!

Protected keyword - hides method but makes it available to subclass

59
Q

How do we implement the Template Method Pattern?

A

Create an Abstract Class

execute ( ) method - has under-the-hood logic executed everytime + .doExecute ( ) method call which can be polymorphed by each sub-class

60
Q

What is an excercise using the Template Method Pattern?

A
61
Q

What problem does the Command Pattern solve?

A

Coupling between a sending class and a recieving class

62
Q

What is the Command Pattern?

A

Decouple a sender to a reciever

Each request is a command object

63
Q

What are the benefits to the Command Pattern?

A

Decouple a sender to a reciever

Each request is a command object

  1. We can pass them around our code
  2. We can pass them as arguments to our methods
  3. We can add them to a list to keep track of all commands executed, redo them, undue
  4. Can store in a DB and run later
64
Q

How do we create a container for commands so that multiple commands are executed?

A

Composite Command

Each task is a command object

can combine them into a composite object

65
Q

How do we execute undoable commands?

A

Create Command interface

Create UndoableCommand interface (inherits command)

Implementing class - BoldCommand

Ex. Undo bold text, not save

66
Q

How can we create an undoable command for a text editor?

A

Commands - links between GUI and Business logic

HtmlDocument - implements business logic / makeBold ( )

History - keeps track of commands applied

67
Q

What is an excericise using the Command Pattern?

A
68
Q

When do we use the Observer Pattern?

A

State of object changes and we need to notify other objects of changes!

Used in a lot of frameworks!

Ex. values in excell sheet updated, pie chart and second sheet is automatically updated!

69
Q

What is the Observer Pattern?

A

aka“Publish Subscribe Pattern”

follows open-closed pattern! Can add more observers!

DataSource - maintain list of observer objects

update ( ) - polymorphic behavior, different update ( ) method for each concreate implementation

addObserver (obs) - add observer to list of observers

removeObserver (obs) - delete observer from list to dynamically add/remove

notifyObservers ( ) - iterate over observers, where polymorphic behavior, depending on type at runtime, single place to notify all observers about changes!

70
Q

How do we implement the basic Observer Pattern?

A
71
Q

What is the challenge of the push style of Observer Pattern?

A

Changes are pushed to observer!

Challenge:

It is not extensible without violating the open-closed principle!

If we introduce a new ConcreteObserver (future) may have to go to Observer interface to introduce new fields in class!

It’s not flexible!

Assumes Observers needs same values but some Observers could take different values!

72
Q

What is the pull style of observer communication?

A

Observers pull data they need when changes occur

It’s Flexible

Each concrete observer can get data they need

There is coupling, but it’s okay! No zero coupling in software.

Direction of coupling/relationship matters!

Direction of coupling makes it acceptable

73
Q

How do we implement push method of the Observer Pattern?

A
74
Q

How do we implement the pull method of the Observer Pattern?

A

Coupling but it’s okay!

Introduce a private field of type DataSource

Initialize in Constructor

In update method call .getValue( )

More Flexible!

75
Q

What is an excercise using the Observer Pattern?

A
76
Q

In what situation do we use the Mediator Pattern?

A

When we have a bunch of objects that need to talk to eachother!

77
Q

Why is using inheritance to solve the problem not a good solution?

A

Base classes = ListBox, TextBox, Button

Many dependencies

Logic spread all over the place (different classes)

understanding and maintaining becomes difficult with scale!

Soln:

DialogBox (abstract class) - how objects interact will change from one dialogue box to another

ArticlesDialogBox (concrete class) - where we implement all the logic for how objects interact

78
Q

What is the Mediator Pattern?

A

Meditator controls interaction between objects!

Mediator - like dialogue box

Concrete Mediator - articles dialogue box

Colleague - UI Control

ConcreteColleague - List, textbox, button

79
Q

How can we implement the mediator pattern using the observer pattern?

A

UIControlObjects (TextBox, Button, etc) = Subject class

ArticleDialogBox = Observer of changes in UIControlObjects

80
Q

How do we implement the meditator pattern using event handlers (observers)?

A
81
Q

What is an excercise using the mediator pattern implemented with the observer pattern?

A
82
Q

What is this code doing?

A

.attach ( ) method requires an implementation of the Observer Interface

Observer Interface is a functional Interface (single method)

The changed ( ) method is polymorphically implemented by each Concrete Observer

A concrete observer can be implemented using an annonymous inner class that implements the Observer Interface

in the anonymous inner classes @changed ( ) method, we can call articleSelected ( ) upon change

This can be replaced by a lambda

83
Q

When do we use Chain of Responsibility Pattern?

A

When we need a pipeline (chain of objects) to process a ANY request

Ex. Web server that need to authenticate, log and compress an HTTP request

Solution:

Build a pipline or chain or objects for process a request without having to make changes to single class

84
Q

Why don’t we want to implement the logic of authentication, logging and compression in the WebServer class?

A

It would violate the single responsibility principle

Solution:

Authentication Class - logic for authentication

Compressor Class - logic for compression

Logger Class - logic for logging

85
Q

What problem does the chain of responsibility solve?

A

Webserver.handle ( ) - logic is very fixed

If we want to change our data processing pipeline steps, have to change the .handle ( ) method!

To add a new step have to change implementation of .handle ( ) method - this violated the Open Closed Principle!

Open for extension, closed for modification!

Soln:

Pipeline of processing objects in a chain

Each object only knows about the next object in the chain

86
Q

What is the Chain of Command Pattern?

A

WebServer - talks to handler interface indirectly has reference to first handler in chain (authenticator)

87
Q

How can we implement Chain of Responsibility Pattern?

A

System is open to extension, closed to modification

Can add new step anywhere in the chain

Ex. add new Handler called Encryptor

Can build data processing pipelines that follow the open closed principle!

88
Q

What is an excercise?

A
89
Q

What problem does the Visitor Pattern solve?

A

Allows us to add new operations to an object structure without modifying the item

Ex. HtmlNode interface - if we add new methods to this interface, will then have to go to every concrete implementation and implement the new method! Additionally, in HtmlDocument (dependency on HtmlNode) have to iterate over all objects and call the .newMethod!

This violates Open-Closed Principle!

Also, logic is spread over multiple classes!

Why?

Everytime we want to introduce a new operation, have to make changes to every object in our structure

90
Q

VP What are the benefits of the Visitor Pattern?

A
  1. All logic for an operation is in a single place
  2. Follows Open-Closed Principle

Beware!

Only use if object structure is stable!

Why?

Everytime we create a new opeartion, have to update the Operation interface and all implementing classes

91
Q

What is the Visitor Pattern?

A

Names are Abstract

Key principle:

Allows us to add new operations without changing our object structure

How?

Create new class that implements Operation Interface

92
Q

How do we implement the Visitor Pattern?

A
93
Q

What is an excercise?

A
94
Q

How could I deconstruct a feature into design patterns? (Ex. TinderSwipe Feature)

A

Account Creation - Template Method Pattern

Match History - State Pattern

Matching - Observer Pattern

Messaging -

Data Structure - graph

Prediction - getKthNode

Geolocation - traverse, shortest Path Algorithm