0% found this document useful (0 votes)
25 views2 pages

SOLID

The document discusses the SOLID principles for object-oriented design: 1) Single Responsibility Principle - A class or method should have one responsibility to maintain low coupling and high cohesion. 2) Open/Closed Principle - Code should be open for extension but closed for modification to avoid breaking existing functionality when extending. 3) Liskov Substitution Principle - Derived classes must be substitutable for their base classes without altering functionality.

Uploaded by

sarpdoruk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

SOLID

The document discusses the SOLID principles for object-oriented design: 1) Single Responsibility Principle - A class or method should have one responsibility to maintain low coupling and high cohesion. 2) Open/Closed Principle - Code should be open for extension but closed for modification to avoid breaking existing functionality when extending. 3) Liskov Substitution Principle - Derived classes must be substitutable for their base classes without altering functionality.

Uploaded by

sarpdoruk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOLID

Purposes
+ To make the code more maintainable.
+ To make it easier to extend without breaking/affecting something else that
you've developed earlier.
+ To make the code easier to read and understand thus spending less time for
figuring out what the code does and more time developing the solution.

Principles
1- Single Responsibility Principle
+ A class or a method (basically an entity) should only be responsible for
one thing.
+ Considering method of a controller;
- A controller should only be responsible from the flow.
- You should put input validation, DB modifiers in other methods of
other classes.

- Logger class collects and saves the logs


+ Instead; Logger class collects and LogSaver saves

2- Open/Closed Principle
+ An entity should be open for extension but closed for modification.
+ Extend the functionality by adding new code instead of changing existing
code.

- if/else blocks checking type of the instance to take action


* There are 3 classes; dog, cat, duck.
* Dog has woof(); Cat has meow(); Duck has quack() methods.
* A class that allows animals to communicate() via communicate() method which
checks for the animal type and calls the appropriate bark(); quack(); moo();
* If you need to add another animal you have to modify the class.
* Instead each should be a different interface and communicate() should call
->speak() method of the interface.

3- Liskov's Substitution Principle


+ Any derived class should be able to substitute its parent class.
+ Every class that implements an interface, must be able able to substitute
any reference throughout the code that implements that same interface.

- square extends rectangle


- rectangle has calculateArea method
- constructor has width and height parameters
- ->setWidth(5) ->setHeight(4)
- 20 for rectangle
- 16 for square (but returns 20)

4- Interface Segregation Principle


+ Instead of depending on the object's class you should extract the behavior
into an interface and depend on that interface so that the method called is not
affected by the changes in the class as long as it implements the interface.
+ Replace large interfaces with many but small, specific interfaces.

* There are 3 classes; dog, fish, bird.


* Dog; Run & Swim.
* Fish; Swim.
* Bird; Fly;
- Animal interface { fly(); run(); swim(); }
+ Seperate interfaces { fly(); } interface { run(); } interface { swim(); }
5- Dependency Inversion Principle
+ High level modules should not depend on low level modules. They should
depend on abstractions.

- Class Email (method send Email) & SMS (send SMS)


- Class Notification { private SMS $sms; private EMAIL $email; }
+ sendableInterface { send(); } which is implemented by Email & SMS classes

You might also like