Design patterns

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/62

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

63 Terms

1
New cards

Why use design patterns?

Capitalize on experience and knowledge of design

Increase reusability of software

improve flexibility so that evolution cost is reduced

standardized vocabulary with which to communicate

2
New cards

What are the components of the factory method?

  • Product: defines the interface of objects the factory method creates

  • ConcreteProduct: implements the Product interface

  • Creator: declares the factory method, which returns an object of type product

    • Will have a method that will return the product

    • May define a default implementation of the factory method that returns a default ConcreteProduct object

  • ConcreteCreator: overrides the factory method to return an instance of a ConcreteProduct

3
New cards

What is the structure of the factory method?

4
New cards

Where is the factory method applicable?

  • Used when a class cannot predict the type of objects it needs to create.

  • Delegates object creation to subclasses, allowing them to decide which specific class to instantiate.

  • Promotes flexibility by localizing the knowledge of which helper subclass is responsible for creating the object.

  • Involves a Product interface or abstract class, with multiple Concrete Products.

  • Includes a Creator class (abstract or base), and Concrete Creators that override the factory method to instantiate specific products.

  • Makes it easy to add new product types without modifying existing code.

5
New cards

What are pros of the factory method?

  • Avoid tight coupling between the creator and the concrete products

  • Single responsibility Principle: can move the product creation code into one place in the program, making the code easier to support 

  • Open/closed principle: can introduce new types of products into the program without breaking existing client code

6
New cards

what are the cons to factory method?

The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern

7
New cards

What are the components of the builder design pattern?

  • Builder: specifies an abstract interface for creating each part of the Product

  • ConcreteBuilder: constructs and assembles parts of the product

    • It must provide a mechanism to retrieve the product 

    • Responsible for building the product in a specific way

  • Director: utilized the builder interface to create the objects

  • Product:  the complex object that is being constructed

8
New cards

what is the structure of the builder design pattern

9
New cards

Where is the builder pattern applicable?

  • Refactoring/Avoiding the telescopic constructor anti-pattern

  • Constructing some complex objects needs to allow different representations for that object

10
New cards

What are pros to the builder design pattern?

  • Able to construct objects step-by-step, defer construction steps or run steps recursively

  • Facilitates reuse of construction code when building various representations of products

  • Single responsibility principle: complex construction code is separated from the business logic of the product

11
New cards

what are the cons to the builder pattern?

Increase in complexity of the system since it requires creating multiple new classes

12
New cards

What is the main difference between the builder and factory design patterns?

Main difference between the builder and factory, in the builder you have one product being customized whereas in the factory you have multiple products being customized.

13
New cards

What are the components of the prototype design pattern?

  • 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

14
New cards

What is the structure of the prototype design pattern?

15
New cards

where is the prototype pattern applicable?

  • Your code should not depend on the concrete classes of object that you need to copy

    • Think copying objects passed from a library

  • To reduce the number of subclasses that only differ in the way they initialize their respective objects 

    • Clone and initialize instead of subclassing

16
New cards

What are pros to the prototype design pattern?

  • Can clone objects without coupling to their concrete classes

  • Remove repeated initialization code in favor of cloning pre-built prototypes

  • Proceed complex objects more conveniently

  • An alternative to inheritance when dealing with configuration presets for complex objects

17
New cards

What are the cons to the prototype design pattern?

Cloning complex objects is difficult for objects with circular references or contain objects that do not support copying

18
New cards

What are the components of the singleton design pattern?

  • Private instance: ensure the class only has one instance

  • Private Constructor: prevents other classes from creating instances of the singleton

  • Public getter for the instance: responsible for checking whether an instance has been created

  • Serves as global access point

19
New cards

What is the structure of the singleton design pattern?

20
New cards

where is the singleton design pattern applicable?

  • There must be exactly one instance of a class

  • We have some global data that must be accessible to client in a defined manner

21
New cards

What are the pros to the singleton design pattern?

  • Can be sure that a class has only a single instance

  • Gain a global access point to that instance

  • The singleton object is initialized only when it is requested for the first time

22
New cards

What are the cons to the singleton design pattern?

  • Violates the single responsibility principle: the pattern solves two problems at the time

  • Can mask bad design

  • Requires special treatment in a multithreaded environment so that multiple threads will not create a singleton object several times

  • It may be difficult to unit test the client code of the singleton

23
New cards

what are the components of the adapter design pattern?

  • Target: definees the domain-specific interface the client useless

  • Client: utilizes objects conforming to the Target interface

  • Adaptee: defines an existing interface that needs adaptin 

  • Adapter: adapts the interface of adaptee to the Target interface

24
New cards

What is the structure of the adapter pattern at the class level?

25
New cards

what is the structure of the adapter pattern at the object level?

26
New cards

where is the adapter pattern applicable?

  • Need to use some existing class, but its interface is not compatible with the one needed

  • Creating a reusable class that cooperates with unrelated or unforeseen classes

  • For the Object Adapter, you need to use several existing subclasses, but it would not be feasible to adapt their interface by subclassing each one 

27
New cards

what are the pros to the adapter pattern?

  • Single responsibility principle: can separate the interface or data conversion code from the primary business logic of the program

  • open/closed principle: can introduce new types of adapters into the program without breaking the existing client code

28
New cards

What are the cons to the adapter pattern?

The overall complexity of the code increases because you need to introduce a set of new interface classes

29
New cards

what are the components of the composite design pattern?

  • Component: declares the interface for objects in the composition and implements default behavior for the interface common to all classes

  • Leaf: represents leaf objects in the composition defining behavior for primitive objects in the composition

  • Composite: defines behavior for components having children and stores child components

Client: manipulates objects in the composition through the component interface

30
New cards

what is the structure of the composite design pattern?

31
New cards

where is the composite design pattern applicable?

  • Representing part-whole hierarchies of objects

  • Clients should be able to ignore the difference between composition of objects and individual objects

32
New cards

what are the pros of the composite pattern?

  • More efficient usage of complex tree structures by taking advantage of polymorphism and recursion

  • open/closed principle: new element types can be introduced without breaking the existing code

33
New cards

what are the cons of the composite pattern?

Difficult to provide a common interface for classes whose functionality differs too much

34
New cards

what are the components of the decorator design pattern?

  • Components: the interface for objects that can have responsibilities added to them dynamically 

  • ConcreteComponent: an object to which additional responsibilities can be attached

  • Decorator: defines the same interface as the component and maintains a reference to the components object

ConcreteDecorator: adds responsibilities to the component

35
New cards

what is the structure of the decorator pattern?

36
New cards

where is the decorator pattern applicable?

  • Need to add responsibilities to individual objects dynamically without affecting other objects

  • Need to be able to remove responsibilities of on object

  • When extension by subclassing is impractical

37
New cards

what are the pros of the decorator pattern?

  • You can extend an objects behavior without making a new subclass

  • Flexibility: you can add or remove responsibilities from an object at runtime

  • You can combine several behaviors by wrapping an object into multiple decorators

  • Single responsibility principle: a monolithic class can be divided into several smaller classes

38
New cards

what are the cons of the decorator pattern?

  • When wrappers are stacked it can be hard to remove a specific one from the stack

  • It can be difficult to implement the pattern such that its behavior does not depend on the order in the decorators stack

  • Instantiation is more complicated to include all of the decorators

39
New cards

what are the components of the facade design pattern?

  • Facade: knows which subsystem classes are responsible for a request

  • Subsystem classes: implements subsystem functionality

Client: communicate with the subsystem by sending requests to the Facade

40
New cards

what is the structure of the facade design pattern?

41
New cards

where is the facade pattern applicable?

  • Want to provide a simple interface to a complex subsystem

  • There are many dependencies between clients and the implementation classes of an abstraction

  • Layer your subsystems

42
New cards

what are the pros of the facade design pattern?

  • Manages complexity by separating your code from a subsystem

43
New cards

what are the cons of the facade design pattern?

  • Prone to becoming a god class antipattern by coupling to all of the classes of the application 

  • Adds more complexity than it helps to control in simple systems

44
New cards

what are the components of the template method?

  • Abstract class: defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm

    • Implements a template method defining the skeleton an algorithm 

Concreteclass: implements the primitive operations to carryout subclass-specific steps of the algorithm

45
New cards

what is the structure of the template method?

46
New cards

where is the template method applicable?

  • Implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behaviors that can vary

  • A common behavior among subclasses should be factored and localized in a common class to avoid code duplication

  • Control subclasses extensions by defining a template method that class hook operations to specify points of extension

47
New cards

what are the pros of the template method?

  • Lets clients override certain parts of a large algorithm, making them less affected by changes that happen to other parts of the algorithm

  • Can pull the duplicate code into a superclass

48
New cards

what are the cons of the template method?

  • clients are limited by the provided skeleton of an algorithm

  • Maintenance effort typically increases with the number of steps.

49
New cards

what are the components of the iterator design pattern?

  • Iterator: defines an interface for accessing and traversing elements

  • concreteIterator: implements the iterator interface, keeping track of the current position in the traversal of the aggregate

  • Aggregate: defines an interface for creating an iterator object

  • ConcreteAggregate: implements the iterator creation interface to return an instance of the proper ConcreteIterator

50
New cards

what is the structure of the iterator pattern?

51
New cards

where is the iterator pattern applicable?

  • Accessing an aggregate objects contents without exposing its internal representation

  • Supports multiple traversals of aggregate objects

  • Provide a uniform interface for traversing different aggregate structures

52
New cards

what are the pros to the iterator pattern?

  • Single responsibility principle: cleans up the client code and the collections by extracting bulky traversal algorithms into separate classes

  • Open/Closed principle: implement new types of collections and iterators and pass them to existing code without breaking anything 

  • Can iterate over the same collection in parallel

  • Can delay an iteration and continue it when needed

53
New cards

what are the cons to the iterator pattern?

  • Applying the pattern can be an overkill if your app only works with simple collections

  • Using an iterator may be less efficient than going through elements of some specialized collections directly

54
New cards

What are the components of the state pattern?

  • Context: defines the interface of interest to clients and maintains an instance of a ConcreteState subclass that defines the current state

  • State:  defines an interface for encapsulating the behavior associated with a particular state of the Context

ConcreteState subclasses: each subclass implements a behavior associated with a state of the Context

55
New cards

what is the structure of the state pattern?

56
New cards

where is the state pattern applicable?

  • An object's behavior depends on its state, and it must change its behavior at run-time depending on that state

  • Operations have large, multipart conditional statements that depend on the object’s state

57
New cards

what are the pros to the state pattern?

  • Single responsibility: organizes the code related to particular states into separate classes

  • open/closed principle: introduce new states without changing existing state classes or the context

  • Simplifies the code of the context by eliminating state machine conditionals

58
New cards

what are the cons of the state pattern?

  • Can be overkill if a state machine has only a few states or rarely changes states

59
New cards

what are the components of the strategy pattern?

  • Strategy: declares an interface common to all supported algorithms

    • Context relies on this to call the algorithm defined by a ConcreteStrategy

  • ConcreteStrategy: implements the algorithm using strategy interface

Context: is configured with a ConcreteStrategy object, maintaining a reference to a strategy object

60
New cards

what is the structure of the strategy pattern?

61
New cards

where is the strategy pattern applicable?

  • Many related classes differ only in their behavior then Strategies provide a way to configure a class with one of many behaviors 

  • Need different variants of an algorithm 

  • An algorithm uses data that clients should not know about 

  • A class defines many behaviors, and these appear as multiple conditional statements in its operations

62
New cards

what are the pros of the strategy design pattern?

  • Can swap algorithms used inside an object at runtime

  • Can isolate the implementation details of an algorithm from the code that uses it

  • Can replace inheritance with composition 

  • Open/closed principle: introduce new strategies without having to change the context

63
New cards

what are the cons of the strategy pattern?

  • Overkill if you only have a couple of algorithms and they rarely change

  • Clients must be aware of the differences between strategies to select a proper one

  • Some languages have functional type support which facilitates different implementations of an algorithm