Design and Development Principles Flashcards

1
Q

Explain SOLID principle

A
  • SINGLE RESPONSIBILITY: high cohesion => “Each module should do one thing and do it well.”
  • OPEN CLOSE: “You should be able to use and add to a module without rewriting it.”
  • Liskov substitution: “You should be able to substitute one thing for another if those things are declared to behave the same way.” => Basically, you can change for example different props, but if they are the same type, it shouldn’t affect the behavior of the function.
  • Interface Segregation: “Many client-specific interfaces are better than one general-purpose interface.” => “Don’t show your clients more than they need to see”.
  • Dependency inversion: “Depend upon abstractions, not concretions.” => EXAMPLE: If you’re writing code that needs a logger, you don’t want to limit yourself to writing to files, because you don’t care. You just call the write method and let the concrete class sort it out (in this case, you have 2 types of Loggers, one for files and the other one a customized one).
    You can also apply abstraction to the microservice world. For example, you can replace direct communication between services with a message bus or queue platforms such as Kafka or RabbitMQ. Doing this allows the services to send messages to a single generic place, without caring which specific service will pick those messages up and perform its task.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain Singleton pattern

A

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine.

It is from the Creational Design Pattern category.

It is used for logging, drivers objects, caching and thread pool.

– singleton criticism => it introduces a global state into an application, often unnecessary

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

Explain the Factory pattern

A

The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class.

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

Explain the composite pattern

A

When we need to create a structure in a way that the objects in the structure has to be treated the same way, we can apply composite design pattern. Let’s understand it with a real-life example - A diagram is a structure that consists of Objects such as Circle, Lines, Triangle etc. When we fill the drawing with color (say Red), the same color also gets applied to the Objects in the drawing. Here drawing is made up of different parts and they all have same operations. Composite Pattern consists of following objects.

From the “Structural Design Patterns”

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

Explain decorator pattern

A

Decorator design pattern is used to modify the functionality of an object at runtime. At the same time other instances of the same class will not be affected by this, so individual object gets the modified behavior.

From the “Structural Design Patterns”

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

Explain strategy pattern

A

Strategy pattern is also known as Policy Pattern. We define multiple algorithms and let client application pass the algorithm to be used as a parameter.

Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.

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

Explain some object design patterns that are used in JS nowadays

A
  • Singleton (creation pattern): Could be settings data. In JS is global
  • Prototype (creation pattern): clone. When developers use Object.create(oldObject, {newField: { value: ‘pepe’ } }), they are making a Prototype of an existing object.
  • Builder (creation pattern): it add properties calling to method in an object instantiation. This was pretty common in libraries like JQuery.
  • Factory (creation pattern): instead of instantiating an object yourself, you call a function or method to do it for you. It gave the example of showing the Android or IOS button, just calling a component that is going to show the correct element accordingly.
  • Facade (structural pattern): simplified API to hide low level details in your codebase. Most libraries use it, as Jquery ($(‘.pepe’).addClass(‘newClass’), behind this it has the document.getElementByClassName, the assignation of the class to that element, etc.
  • Iterator (behavioural pattern): when you use the for(item of cart) you are using the iterator pattern.
  • Observer: in Redux is used.
  • Mediator: middleware in express (node).
  • State: an object behaves differently depending on his state.
    EXAMPLE: an Object Human, has another object State to know how to return the feeling string. The state has several children, that is happy, sad, etc.

https://www.youtube.com/watch?v=tv-_1er1mWI&ab_channel=Fireship

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