Design patterns Flashcards
What are the benefits of using design patterns?
Universality, communication, guaranteed solutions, readability, and reduced length of code
How are they categorized?
They are categorized by their purpose: creational, structural, and behavioral.
What do creational patterns do?
They provide flexibility in what gets created, who creates it and how, when it gets created.
What are the 5 design patterns in creational category?
Singleton, Factory method, Builder, Abstract Factory, Prototype
What does the builder pattern do, and what are the benefits of using it?
The builder pattern lets you create different representations of an object using the same construction. For a large class, and you don’t want to use all the class fields, the builder pattern is the choice.(an alternative is creating sublclasses or multiple constructors)
What does the factory method do and what are its benefits?
The factory method allows us to create objects without specifying the exact class to create, delegating the creation to another class(factory). This way, the internals of the class are hidden, good for code maintenance.
What does the singleton pattern and what are the benefits of using it?
The singleton pattnern restricts object creation for a class to only one instance. (you can not have different instances of the object). The constructor will be private.
What are the 7 design patterns in the structural category?
Composite, Adapter, Decorator, Facade, Proxy, Bridge, Flyweight
What do structural patterns do?
They are used to piece together multiple classes into bigger picture without affecting the flexibility of the individual classes.
What does the adapter pattern do and what are its benefits?
The adapter pattern allows classes with incompatible interfaces to communicate with one another by wrapping its own interface around that of an already existing class. With this pattern you can reuse existing code for different use cases. (it will implement a “middleman” that will convert/adapt the interface from one class so other one can comprehend
What does the decorator pattern do and what are its benefits?
It dynamically adds or overrides behavior in an existing method of an object by wrapping these objects in new objects that contain the desired behavior. It adds new functionality to existing objects without changing its structure.
What does the proxy pattern do and what are its benefits?
It creates expensive objects on demand (virtual proxy)/ representative of objects in a different address space (remote proxy)/ control access to an object (protection proxy)
What does the facade pattern do and what are its benefits?
It provides a simplified interface to a codebase, library, framework. It simplifies the integration of a complex codebase into other codebases. The programmer doesn’t need to know details of this complex codebase.
What are the 7 design patterns in the behavioral
category?
Iterator, observer, template method, strategy, state, command, chain of responsability.
What do the behavioral patterns do?
They facilitate complex control flow (interaction and distribution of responsability)