L8: Building on Established Solutions to Create Objects Flashcards

1
Q

Design patterns

A

23 design patterns
Not always applicable!
Structured along three families:
- Creational
- Structural
- Behavioral

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

What are the patterns covered in Software Design and what category do they belong to?

A

Creational:
- Abstract Factory
- Factory
- Singleton

Structural:
- Decorator
- Façade

Behavioral:
- Command
- Iterator
- Observer
- State
- Strategy
- Template Method

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

Implementing Creation

A

Patterns for creating objects
* Making object creation more flexible (or fixed) to match a use case
* Controlling who creates what, how and when

Often use design principles
* Encapsulate knowledge about what concrete classes the system uses
* Hide how these instances are created (data abstraction)

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

Implementing structure

A

Patterns for structuring units and their relationships
* Making independent units work together (integration)
* Defining how to compose objects with different functionalities at runtime

Often use design principles
* Separating distinct concerns into units
* Abstracting how these concerns interact
* Interfacing their relations

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

Implementing behavior

A

Patterns for implementing communication between units
* Making separate units responsible for a specific functionality
* Characterizing complex control flows at runtime

Often use design principles
* Separating concerns into distinct, communicating units
* Encapsulation of behavior
* Abstract (procedural) behavior through inheritance or composition

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

Singleton

A

It’s a simple pattern

Restrict the instantiation of a class to a single instance
(i.e., there can only be one object of that type)
* Ensure that only a single instance exists
* Define access to that instance
* Control the instantiation of the instance

Used for
* Storing and managing variables (instead of having them globally accessible)
* Implementing other design patterns
* Logging, configuring, file system, window manager etc.

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

Being careful with the singleton

A

A singleton is sometimes considered a bad practice
* Introduces (often unnecessary) global access
* Not really aligning to the single-responsibility principle
(responsible for ensuring own uniqueness and performing other functions)

This can lead to
* Additional (global) dependencies and increased coupling
* Reduced extensibility (what if multiple objects are needed?)
* Testing challenges when tested unit relies on singleton
* Abuse of the single, global access point for everything (instead of proper abstraction)

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

Factory

A

It’s more complex than a singleton

Define interface to create object, but defer instantiation to subclasses
* Decouple object creation from concrete class
(new couples to concrete class and violates dependency inversion principle)
* Enable instantiation of objects whose types are unknown when coding
* Allow subclasses to specify/decide what objects to create

Used for
* Creating user-interface or game-engine elements (e.g., Swing, JavaFX)
* Creating loggers with different configurations
* Loading plug-ins dynamically

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

Being careful with the factory

A

Naturally increases the complexity of the code by introducing abstractions

Easily overused, can be hard to judge whether a factory is useful

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

Generics

A

Using a list instead of an array to store multiple vehicles

Uses an ArrayList
* ArrayList<E>
* Implements interface List<E>
* E represents generic type to be replaced by concrete type
* Used to implement generic data structures/classes</E></E>

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

Why use generics?

A

Generics (can) improve readability, reusability, and robustness

Ensure type safety (compiler checks for type of data in data structure)

Avoid runtime errors

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