S.O.L.I.D: Implementations - Eg
S.O.L.I.D: Implementations - Eg
D
is an acronym for the first five object-oriented design (OOD) principles. are the core principles
for designing a software using any programming language.
The goal is to build software that is easy to read, maintain and extend. Avoiding code smells
(functional but posssible indicates deeper problem - duplicated code, comments) , easily
refactor code, and are also a part of the agile or adaptive software development.
S - Single-responsiblity. A class should have only one responsability, well defined purpose,
just a few of related tasks.
O - Open-closed. A class should be open for extension, but closed for modification. We can
add more functionality by inheritance for example.
Higher level-components are protected from changes to lower level components.
L - Liskov substitution. By using inhericante, objects of a superclass should be replaceable
with objects of its subclasses without breaking the application. What we want is to have the
objects of our subclasses behaving the same way as the objects of our superclass.
I - Interface segregation. Use many smaller and specific interfaces, instead one general
D - Dependency Inversion. Should depend on abstractions not on concrete
implementations.Eg. DI use an interface
Separation of Concerns
used for separating a computer program into distinct sections such that each section addresses
a separate concern. Angular already implements this pattern on file structure level as there are
separate files for Html, CSS, and ts but we can go in much deeper by using decorators that
separate the class, property, or method concerns.
Decorators
Used to configure and customize a class without modifying the original source code.
They are just functions that can be used to add meta-data, properties, or functions to the class.
Adapter
convert the interface of a class into another interface clients expect. Adapter lets classes work
together that couldn’t otherwise because of incompatible interfaces.
Observer
the subject object has a list of its observers, and notifies them automatically when state of this
subject changes.
In Angular you declare an observable, but that observable is not executed until someone
subscribes to it. then it start receiveing notifications until the function completes, or until they
unsubscribe.
Singleton
A singleton service is a service for which only one instance exists in an application:
Dependecy Injection
DI is a way for classes to declare and have access to services they depends on.
Components needs a service to do some stuff, in its constructor you specify an argument with
the dependency type, and that gives the component access to that service class because
Angular injects a dependency.
A provider is an instruction to the Dependency Injection system on how to obtain a value for a
dependency. Most of the time, these dependencies are services that you create and provide.
Functional programming
Means developing software by composing pure functions (same inputs, same output), there is
no shared state, or side-effects
Functional programming is declarative rather than imperative, and application state flows
through pure functions. More concise, more predictable, and easier to test
Contrast with object oriented programming, where application state is usually shared and
colocated with methods in objects.
Object Oriented progamming is a programing paradigm that uses objects to represent things of
the real word.
A Class represents a specific entity and defines its characteristics using properties and what
that entity can do, its behaviour using methods.
A class is basically a template with which objects can be created
So when an object is created, it automatically takes the properties and methods that are defined
in the class
For example, A BankAccount entity can be organized by a class 'BankAccount'.
Inheritance?
means that a class can “receive common properties or behavior from other classes.”
An object or class (referred to as a child) is created using the definition of another object or
class (referred to as a parent). Inheritance not only helps to keep the implementation simpler
but also helps to facilitate code reuse.
Class Inheritance: instances inherit from classes (like a blueprint — a description of the
class). Instances are typically instantiated via constructor functions with the new keyword.
Prototypal Inheritance: instances inherit directly from other objects via factory functions
or Object.create() . Instances may be composed from many different objects, allowing
for easy selective inheritance. In simple words: this type of style allows the creation of an
object without first defining its class.
Polymorphism
refers to the way that object behaves differently under different contexts.
For example, if you have a class named Vehicle, it can have a method named speed but you
cannot define it because different vehicles have different speed. This method will be defined in
the subclasses with different definitions for different vehicles.
Encapsulation
allows hiding data and behavior specified in one class from other classes
Data abstraction?
allows displaying only the important information and hiding the implementation details (not
necessary).
An abstract class is a class that consists of abstract methods. These methods are basically
declared but not defined. If these methods are to be used in some subclass, they need to be
exclusively defined in the subclass.
What is a constructor?
Constructors are special methods whose name is the same as the class name. The
constructors serve the special purpose of initializing the objects.
Interface
its like a special type of class, contains methods (declare them), but not their definition. It not a
blueprint, you cannot create objects. Instead, you need to implement that interface and define
the methods of the interface.