Midterm Flashcards

1
Q

What is a Flyweight?

A

A shared object used in multiple contexts simultaneously but acts as an independent object.

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

What are the participants of a Flyweight design pattern and what do they do?

A

Flyweight - Declares an interface through which states are received and acted on.
ConcreteFlyweight - Implements the interface
UnsharedConcreteFlyweight - Not all Flyweight subclasses need to be shared
FlyweightFactory - Creates and manages flyweight objects, ensures they are shared properly
Client - Maintains a reference to the flyweights, computes/stores the extrinsic state of flyweights

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

What is the intent of a Flyweight design pattern?

A

Object structural. To share large numbers of fine-grained objects efficiently (lots of objects holding very small amounts of data). Frequency of use is low.

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

What is the difference between intrinsic and extrinsic state?

A

Intrinsic = internal, state information is independent of the context, can be shared
Extrinsic = external, state depends on and varies with the context, can’t be shared

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

What is the intent of a Strategy design pattern?

A

To define and encapsulate a family of algorithms and make them interchangeable which lets the algorithm vary independently from clients that use it.

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

What are the participants of a Strategy design pattern and what do they do?

A

Strategy - Declares an interface common to all supported algorithms.
ConcreteStrategy - Implements the algorithm using the Strategy interface.
Context - Configured with ConcreteStrategy and maintains a reference to Strategy. May define an interface that lets Strategy access its data.

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

What is the intent of an Iterator design pattern?

A

To provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

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

What are the participants of an iterator design pattern and what do they do?

A

Iterator - Defines an interface for accessing and traversing elements
ConcreteIterator - Implements Iterator and keeps track of the current position in the traversal of the aggregate
Aggregate - Defines an interface for creating an Iterator
ConcreteAggregate - Implements the Iterator creation interface to return an instance of the proper ConcreteIterator

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

What is the intent of a Prototype design pattern?

A

To specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

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

What are the participants of a Prototype design pattern and what do they do?

A

Prototype – Declares an interface for cloning itself.
ConcretePrototype – Implements an operation for cloning itself.
Client – Creates a new object by asking a prototype to clone itself.

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

What is the intent of a Template Method?

A

To define the skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

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

What are the participants of a Template Method and what do they do?

A

AbstractClass - Defines abstract primitive operations implemented by concrete subclasses and implements a template method defining the skeleton of an algorithm. This method calls primitive operations as well as operations defined in AbstractClass or those of other objects.

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

When should you use a Flyweight?

A

When you have many objects and you want to reduce the storage costs in the application

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

When should you use a Strategy design pattern?

A

When you have algorithms that do the same task but vary in implementation

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

When should you use an Iterator design pattern?

A

When you have a variety of aggregates and you want to traverse them with a common interface

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

When should you use a Prototype design pattern?

A

When you don’t know what classes you will need until runtime

17
Q

When should you use a Template Method?

A

When you have an algorithm that has parts that may change based on specific use cases