Design Patterns Flashcards

1
Q

What is a design pattern, and why do we use design patterns?

A
  • A general solution to common problems. Aren’t finished code, more a abstract template. Should be viewed as best practice
  • Help solve common problems. Created by people with experience, thus should not be discounted. Makes it easier to follow general principles
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe Factory Pattern? When to use? Which Compontents? What problems are solved?

A
  • Creates objects without exposing instantiation to the client, refers to the newly created object through a common interface
  • Used when one wants to hide implementations(what constructors and specific classes should be used) and allows subclasses to decide which class to instantiate.
  • Applied by defining a factory class, which consists of factory methods. The factory methods create a specific predefined object. These are often returned as a collection(in a list, array)
  • Listed above. As well as creating objects without specifying the class of the object that will be created, looser coupling. Adhere to SoC and SRP. OCP, new objects can be added
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe Observer Pattern? When to use? Which Compontents? What problems are solved?

A
-Used when there is a one-to-many relationship between objects, such as when the modification of one object requires its several dependant objects to be notified automatically. 
An object (named the subject) maintains a list of its dependents (called observers) and notifies them automatically if any state changes, usually by calling one of their methods
  • Applied which has “one-to-many relationships” is Often used in an MVC pattern, when changes to the position of an object require the object to be redrawn. Or when the View needs to know of changes that occurred in Model
  • The observer is an interface with a method, called “update” or similar. Each component that implements this interface will then have an update method which can be called by other parts of the program just by iterating through a collection of these components, by applying the DIP
  • Adherence to OCP(easy to add more views), fewer dependencies therefore lower coupling.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe Template Method Pattern? When to use? Which Compontents? What problems are solved?

A
  • A method in a (usually abstract) superclass and defines the skeleton of an operation in terms of a number of high-level steps. The method uses an abstract method that allows the subclasses to define the part of the method that differs
  • Useful when subclasses need a method that almost does the same thing, though with a small difference between the subclasses.
  • Uses an abstract method that subclasses can use to define a specific(not shared) behavior. The superclasses also contain a non-abstract method that uses the abstract one.
  • Code reuse is more prevalent, reducing duplicated code. Boosts readability, more expandable(OCP)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe State Pattern? When to use? Which Compontents? What problems are solved?

A
  • Objects are allowed to alter their behavior when their internal state changes. Objects will appear to change their class.
  • Applicable when you want to use the same object but with different behavior. (For example a Media Player)
  • An interface is needed(to define the states) and classes (to define the behavior of an object in these different states) that are delegated onto the object. Finally, one needs methods contained in the object’s class that call upon the methods defined in the state classes
  • Reduces duplicate code, follows SRP, OCP.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe Strategy Pattern? When to use? Which Compontents? What problems are solved?

A
  • If a class has a responsibility to perform an algorithm in several different ways, these algorithms should be extracted into separate classes called Strategies
  • When there are multiple ways to perform a single responsibility
  • One needs a class (that handles what to with the algorithms), an interface (in which the different classes containing strategies implement), and the classes which contain the concrete strategies.
  • Adherence to SoC, dynamical switching during runtime, simplified code, and adherence to OCP. (OCP as new strategies can be added without modifying existing code)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe Model-View-Controller(MVC)? When to use? Which Compontents? What problems are solved?

A
  • Used creating user interfaces, is divided into three interconnected elements/modules:
  • Model is the part responsible for the data, logic, and “rules” of the UI. Should never depend on either V or C. Should be “smart”
  • View is responsible for the output, being the representation of information, for example, what a player sees in a game. Should be “dumb”
  • Controller can be seen as the input, such as key bindings that trigger commands or events contained within the Model. Should be “thin”
  • Useful where one needs a UI that has both a form of input and output which users can interact with
  • Different components are listed above :)
  • Aids(chef, FuckIt’22) codes adherence to high cohesion and low coupling principle, since MVC work in their own area of responsibility. Greater adherence to SoC
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe Module Pattern? When to use? Which Compontents? What problems are solved?(Ingen verkar förstå???)

A
  • Used to implement the concept of software modules, linked to SoC principle and information hiding
  • No strict way to implement it. One interpretation is that it’s only a Module pattern if there is a class that represents the module(i.e a class that gathers behavior defined several patterns)
  • To implement, all code that works to solve a concern should be grouped together in one module. Code that only needs to be accessed within a certain class should ideally be made private, meanwhile, code that is used in multiple classes within the same module should be made protected or private and then accessed and manipulated through getters and setters
  • Clearer structure to the code, and implement SoC. Implement information hiding, less susceptible to bugs. Making code more abstract, therefore adherence to the DIP.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe Bridge Pattern? When to use? Which Compontents? What problems are solved?

A
  • Dictates what an object is.
    A certain dimension of an object can be extracted into a separate abstraction with defined characteristics, allowing the object to decide which to use.
  • Is applicable when an object can possess one of the multiple dimensions
  • Need two separate hierarchies. For example, “Shape” and “Color”. The “Shape” is inherited by “Circle” and “Square”, while “Color” is inherited by “Red” and “Blue”. The “Shape” class is then composed of the “Color” class(Går inte att lägga till bilder så titta bara på bilderna som finns :P)
  • Allows changing the classes in each hierarchy, thus increasing flexibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe Facade Pattern? When to use? Which Compontents? What problems are solved?

A
  • A facade is an object that serves as a front-facing “interface”, used to hide complex underlying code. This pattern involves a single class, which provides simplified methods required by clients, and delegates call to methods of existing system classes
  • Is applicable in one of two scenarios:
  • Want to make a complex subsystem easier to use
  • The dependencies on a subsystem should be minimized
  • To implement, one must employ a class(the facade) that delegates the client’s requests to the necessary class(es)
  • Improves readability and usability of the code by masking interaction with more complex components, and serves as a launching point for a broader refactor of monolithic or tightly-coupled systems, in order to promote looser coupling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe Adapter Pattern? When to use? Which Compontents? What problems are solved?

A
  • Objects with incompatible interfaces to collaborate
  • When two objects have incompatible interfaces. One operates in a certain way, the other in another way, for example, km vs miles. Wraps one of the objects with an adapter that converts to the other object.
- Define an interface that describes a protocol (a condition) that other classes must follow to be able to collaborate with the client code ( the code that will use the adapted object). 
Then one must create an adapter class that implements the client interface while wrapping the service object. The adapter receives calls from the client, via the adapter interface, and translates them into calls to the wrapped service object in a format it can understand
  • Adherence to SRP and OCP.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly