Creational Patterns Flashcards

1
Q

Provide an interface that delegates creation calls to one or more concrete classes in order to deliver specific objects.

Use When

  1. The creation of objects should be independent of the system utilizing them.
  2. Systems should be capable of using multiple families of objects.
  3. Families of objects must be used together.
  4. Libraries must be published without exposing implementation details.
  5. Concrete classes should be decoupled from clients
A

ABSTRACT FACTORY

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

Exposes a method for creating objects, allowing subclasses to control the actual creation process.

Use When

  1. A class will not know what classes it will be required to create.
  2. Subclasses may specify what objects should be created.
  3. Parent classes wish to defer creation to their subclasses.
A

FACTORY METHOD

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

Allows for the dynamic creation of objects based upon easily interchangeable algorithms.

Use When

  1. Object creation algorithms should be decoupled from the system.
  2. Multiple representations of creation algorithms are required.
  3. The addition of new creation functionality without changing the core code is necessary.
  4. Runtime control over the creation process is required.
A

BUILDER

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

Create objects based upon a template of an existing objects through cloning.

Use When

  1. Composition, creation, and representation of objects should be decoupled from a system.
  2. Classes to be created are specified at runtime.
  3. A limited number of state combinations exist in an object.
  4. Objects or object structures are required that are identical or closely resemble other existing objects or object structures.
  5. The initial creation of each object is an expensive operation.
A

PROTOTYPE

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

Ensures that only one instance of a class is allowed within a system.

Use When

  1. Exactly one instance of a class is required.
  2. Controlled access to a single object is necessary.
A

SINGLETON

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

Email editors will allow for editing in multiple formats including plain text, rich text, and HTML. Depending on the format being used, different objects will need to be created. If the message is plain text then there could be a body object that represented just plain text and an attachment object that simply encrypted the attachment into Base64. If the message is HTML then the body object would represent HTML encoded text and the attachment object would allow for inline representation and a standard attachment. By utilizing an abstract factory for creation we can then ensure that the appropriate object sets are created based upon the style of email that is being sent.

A

ABSTRACT FACTORY

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

Many applications have some form of user and group structure for security. When the application needs to create a user it will typically delegate the creation of the user to multiple user implementations. The parent user object will handle most operations for each user but the subclasses will define the factory method that handles the distinctions in the creation of each type of user. A system may have AdminUser and StandardUser objects each of which extend the User object. The AdminUser object may perform some extra tasks to ensure access while the StandardUser may do the same to limit access.

A

FACTORY METHOD

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

A file transfer application could possibly use many different protocols to send files and the actual transfer object that will be created will be directly dependent on the chosen protocol. Using a builder we can determine the right builder to use to instantiate the right object. If the setting is FTP then the FTP builder would be used when creating the object

A

BUILDER

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

Rates processing engines often require the lookup of many different configuration values, making the initialization of the engine a relatively expensive process. When multiple instances of the engine is needed, say for importing data in a multi-threaded manner, the expense of initializing many engines is high. By utilizing the prototype pattern we can ensure that only a single copy of the engine has to be initialized then simply clone the engine to create a duplicate of the already initialized object. The added benefit of this is that the clones can be streamlined to only include relevant data for their situation.

A

PROTOTYPE

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

Most languages provide some sort of system or environment object that allows the language to interact with the native operating system. Since the application is physically running on only one operating system there is only ever a need for a single instance of this system object. The singleton pattern would be implemented by the language runtime to ensure that only a single copy of the system object is created and to ensure only appropriate processes are allowed access to it.

A

SINGLETON

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