Ultimate Design Patterns - Structural Flashcards

1
Q

What problem does the Composite Pattern solve?

A

Allows us to work with objects or groups as if they were the same

without having to use Object base class and explicitly cast objects to types

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

When do we use the Composite Pattern?

A

Want to represent a hierarchy and treat the objects in hieararchy in the same way (parts and containers)

Ex. Files and Folders

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

How can we implement the Component Pattern?

A

Allows us to represents object hierarchies where individual objects and compositions of objects are treated the same way

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

What’s an excercise using the Composite Pattern?

A

Represents object hierarchies where individual objects and compositions of objects are treated the same way.

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

AP What is the Adapter Pattern?

A

Allows converting the interface of a class into another interface that clients expect.

Just like an electricity adapter converts plug to format!

Ex. Apply various filters to a photo

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

What real world problem does the Adapter Pattern Solve?

A

We have a class that we want to use but the interface doesn’t match form we expect

Adapter pattern converts interface of the class to the proper form

We have a third party filter library that doesn’t implement Filter Interface

Our viewImage class only accepts filters that Implement Filter Interface

Adapter Pattern allows us to change third party filter Interface to match required form for viewImage (Filter Interface)

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

How do we implement the Adapter Pattern using composition?

A

Favor composition over inheritance!

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

How do we implement the Adapter Pattern using inheritance?

A

Not the favorable approach!

Why?

It is less flexible than composition

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

What is an excercise using the Adapter Pattern?

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

What problem does the Decorator Pattern solve?

A

Allows us to add additional behavior to an object dynamically

Why?

For every feature we are adding, have to create various classes to combine features.

With five features, would have to create many classes to combine features!

This isn’t extensible approach.

Ex. We can create a CloudStream object and decorate it with additional behavior

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

What is the Decorator Pattern?

A

Allows us to add additional behavior to an existing object

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

What is an old saying?

A

Favor composition over inheritence!

Why?

It doesn’t mean inheritance is bad, it’s a great solution to many problems but not this problem

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

What is similar and different between the Decorator Pattern and the Adapter Pattern?

A

Both pattern implementation uses composition relationship

Adapter Pattern - We change the interface to a different form

Decorater Pattern - We add additional behavior to an object

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

How do we implement the Decorator Pattern?

A

Allows us to add additional operations to an object

How?

Decorator - a wrapper class with additional operations composed of an object

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

What is an excercise using the Decorator Pattern?

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

What do we use the Facade Pattern to do?

A

Provide a simple interface to a complex system

17
Q

What is the problem with this implementation?

A
  1. Sending a notification is difficult - Everytime we want to send a push notification to a user we have to follow a number of steps! We have to create new notification server, connect, create message, send it!
  2. Coupling - Main class is coupled to four classes. If another class wants to send a message have to also couple to these four classes. If a future change, have to recompile them all!

Solution?

Facade Pattern

18
Q

How do we implement the Facade Pattern?

A
19
Q

What is an excercise using Facade Pattern?

A
20
Q

When do we use the Flyweight Pattern?

A

Reduce amount of memory consumed by objects

Ex. GoogleMaps - rending maps has cafes, restaurants, schools.

A mobile phone trying to render large point objects will run out of memory and crash!

21
Q

What is the problem with this implementation?

A

On some mobile devices application freezes or crashes

Why?

The point class consumes a fair amount of memory

We could have thousands of points

With this structure, Cafe Icon (20KB) will be stored 100 times (20MB)

We can store it only once in memory and use it across multiple point objects!

Ex. Each pt is 20MB * 100 pts = 20 KB memory!

Many users have Apps in background not closed, using memory!

22
Q

What is the flyweight pattern?

A

Allows objects to cache data that is shared across multiple objects to reduce memory!

Why?

Allows to reduce memory use for objects

How?

Separate the data we need to share

store it in Flyweight class (object we can share)

implement a factory for caching these objects

23
Q

What is an excercise using the Flyweight Pattern?

A
24
Q

What problem does the Bridge Pattern solve?

A

Building a simple, yet flexible hierarchy

for a hierarchy that can grow in 2 dimensions

25
Q

What’s the problem with this implementation?

A

It’s inflexible - not open to easy extension

Over time hierarchy will get complex and hard to maintain

Why?

Hierarchy is growing in two directions (1D - feature x 2D - implementation (brand))

Ex. LGRemotes - have to create LGRemot extends RemoteControl and LGAdvanceRemote extends AdvanceRemoteControl

26
Q

What is the Bridge Pattern?

A

Build flexible hierarchies that can grow independent of eachother

The composition relationship between the two hierarchies acts as a “bridge” between the two dimensions

When use?

Hierarchy that grows in two different dimensions

How?

split into two separate hierarchies and connect with a bridge

Why?

Make hierachy more extensible and maintainable

27
Q

How do we implement the Bridge Pattern?

A
28
Q

What’s the problem with this implementation?

A

Creating a new ebook object is costly

We have to load all ebooks to the desk and store in memory

User is not opening every ebook in memory

only interested in one ebook, but other ebooks loaded in memory

Soln?

Proxy - Load on demand, only when needed

29
Q

How do we implement the Proxy Pattern?

A
30
Q

What’s an excercise using Proxy Pattern?

A