Midterm Review Flashcards

1
Q

What is the Singleton Pattern?

A

An object creational pattern that ensures a class only has one instance, used when only one instance is needed and must be accessible from a well-known point.

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

What are the pros and cons of the Singleton pattern?

A

Pros: controlled access to a single instance, very easy to alter to allow for a variable number of instances, more flexible than using static class methods
Cons: not much better than having a global variable

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

What are the known uses of the Singleton pattern?

A

Logging
Database access
Other shared resource

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

What is the motivation for a Singleton pattern?

A

Some classes should only have one instance, such as one print queue

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

What is the Factory pattern?

A

A class creational pattern which defines an interface for creating an object but defers instantiation to subclasses.

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

What is the motivation for a Factory pattern?

A

Some situations require the creation of similar objects with different structures depending on the environment.

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

When should you use a Factory pattern?

A
  • When a class can’t anticipate the class of objects it must create
  • When a class wants its subclasses to specify the objects it creates
  • When you want to localize the knowledge of which helper subclass is the delegate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the participants of the Factory pattern?

A
  • Product: defines the interface that the factory creates
  • ConcreteProduct: implements the interface
  • Creator: declares the factory method (possibly also defines default ConcreteProduct types)
  • ConcreteCreator: overrides the factory to return new types of Product
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the pros and cons of the Factory pattern?

A

Pros: decouples client code from having to know about different kinds of products or how to create them, makes subclassing easy and flexible, makes use of parallel class structures trivial
Cons: may need to subclass the creator class in client code if client-specific types are required which can lead to extra code

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

What are the known uses of the Factory pattern?

A

C# GetEnumerator: creates an enumerator for the type on which it is called
Javascript createElement: creates a new HTML element based on a passed HTML element type

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

What is the Abstract Factory pattern?

A

An object creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.

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

What is the motivation for an Abstract Factory pattern?

A

It can define the shared structure of related/connected objects and have the concrete implementation handle the details

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

When should you use an Abstract Factory pattern?

A
  • When a system should be independent of how its products are created, composed, and represented
  • When a system should be configured with one of multiple families of products
  • When a family of related product objects is designed to be used together, and you need to enforce this constraint
  • When you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the participants of the AbstractFactory?

A
  • AbstractFactory: Declares an interface for operations that create abstract product objects
  • ConcreteFactory: Implements the operations to create product objects
  • AbstractProduct: Declares an interface for a type of product object
  • ConcreteProduct: Defines a product object to be created by the corresponding concrete factory and implements AbstractProduct Interface
  • Client: Uses only interfaces declared by AbstractFactory and AbstractProduct
How well did you know this?
1
Not at all
2
3
4
5
Perfectly