Qualified
- Abstraction
- Encapsulation
- Inheritance vs. Aggregation
- Modularity
- Polymorphism
- Types vs. Classes
- Abstraction Qualities (cohesion, coupling, etc)
- Separation of concerns principle
- Single responsibility principle
OOP
TIP
Object-oriented programming is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods
OOP principles:
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
Abstraction
Abstraction
This is a way of creating a simple model of a more complex real-world entities, which contains the only important properties from the perspective of the context of an application.

Abstraction manages complexity of a system by hiding internal details and composing it in several smaller systems.
The main idea of abstraction is to define real-life components in different complex data types.
ES6: class & extends
class is an implementation of a concrete data structure with functionality implemented in methods.
Class is like a template for creating multiple objects with same list of properties but different data values saved in it.
for example: Person with firstName, lastName, skills, job, but we can skip age, weight, ...
class Person {
// ...
}
class Job {
// ...
}
const john = new Person({
firstName: 'John',
lastName: 'Doe',
job: new Job('Youtube', 'developer', 200000)
});
Encapsulation
Encapsulation is a concept of bundling of data related variables, properties and method in one class.
Encapsulation is approach for restricting direct access to some of the data structure elements (fields, properties, methods, etc)
Inheritance vs. Aggregation
Inheritance
Inheritance is an approach of sharing common functionality within a collection of classes. It provides an ability to avoid code duplication in a class that needs the same data and functions which another class already has. At the same time, it allows us to override or extend functionality that should have a different behavior.
- every object in JavaScript has an internal hidden property [[Prototype]].
- under the hood class is still a function
class Admin extends User {
// ...
}
// Admin instanceof User (true)
JS inheritance are based on prototype chain
Aggregation
Aggregation - when an object is formed from an enumerable collection of subobjects. In other words, an object which contains other objects. Each subobject retains its own reference identity, such that it could be destructured from the aggregation without information loss, e.g., arrays, Set, Map, trees, etc.
“Favor object composition over class inheritance.” - GoF
Modularity
::: Модульность Модульность - это разбиение программы на индивидуальные блоки(компоненты), которая уменьшает сложность :::
модуль, класс, функция
Polymorphism
Polimoprphism
Polimoprphism is the ability to create a property, a function, or an object that has more than one realization.
Polymorphism is an ability to substitute classes that have common functionality in sense of methods and data
for example: Shape with area method, + Triangle, Circle, Rectangle
Types vs. Classes
A type summarizes the common features of a set of objects with the same characteristics. We may say that a type is an abstract interface that specifies how an object can be used.
A class represents an implementation of the type. It is a concrete data structure and collection of methods.
Abstraction Qualities (cohesion, coupling, etc)
Coupling (связанность)
модули должны быть максимально общими, не завязанными на другие.
The measure of the strength of association established by a connection from one module to another. Strong coupling complicates a system since a module is harder to understand, change, or correct by itself if it is highly interrelated with other modules. Complexity can be reduced by designing systems with the weakest possible coupling between modules
Cohesion (сплоченность)
измеряет степень связанности между элементами одного модуля. Необходимо стремиться к функциональной сплоченности, когда все функции модуля работают вместе, чтобы обеспечить некое ограниченное поведение
Cohesion measures the degree of connectivity among the elements of a single module (and for object-oriented design, a single class or object). The least desirable form of cohesion is coincidental cohesion, in which entirely unrelated abstractions are thrown into the same class or module
sufficient (достаточность):
class or module captures enough characteristics of the abstraction to permit meaningful and efficient interaction. To do otherwise renders the component useless. For example, if we are designing the class Set, it is wise to include an operation that removes an item from the set, but our wisdom is futile if we neglect an operation that adds an item. In practice, violations of this characteristic are detected very early; such shortcomings rise up almost every time we build a client that must use this abstraction.
complete:
the interface of the class or module captures all of the meaningful characteristics of the abstraction. Whereas sufficiency implies a minimal interface, a complete interface is one that covers all aspects of the abstraction. A complete class or module is thus one whose interface is general enough to be commonly usable to any client. Completeness is a subjective matter, and it can be overdone. Providing all meaningful operations for a particular abstraction overwhelms the user and is generally unnecessary since many high-level operations can be composed from low-level ones. For this reason, we also suggest that classes and modules be primitive.
Primitive operations are those that can be efficiently implemented only if given access to the underlying representation of the abstraction. Thus, adding an item to a set is primitive because to implement this operation Add, the underlying representation must be visible. On the other hand, an operation that adds four items to a set is not primitive because it can be implemented just as efficiently on the more primitive Add operation, without having access to the underlying representation.
Separation of concerns principle
SoC - is a design principle
MVC (Model - View - Controller)
- separation of functions (try not to write giant functions)
- separation of modules
Single responsibility principle
"Существует лишь одна причина, приводящая к изменению класса (одну ответственность)"
Объект должен иметь связанный (cohesive) набор поведения, относящийся к одной ответственности.
Изменение этой ответственности должно приводить к изменению объекта.Когда объект имеет несколько ответственностей, его изменение, связанное с одной из ответственностей, может побочно изменить логику, связанную с другой ответственностью. Разделение ответственностей как раз и делает код более устойчивым к изменениям.
TIP
Повар(готовит) <=> Оффициант (прием заказов, вынос блюд) <=> Клиент(ест)