Last active
March 16, 2018 11:11
-
-
Save alexzhernovyi/28672d2e00f04f6f49df9474954270fa to your computer and use it in GitHub Desktop.
SolitPrinciples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// High-level modules shouldn’t depend on lower-level modules. All of them must depend on abstractions. | |
// Abstractions shouldn’t depend on the details. Details must depend on the abstractions. | |
class Michel { | |
private $foodProvider | |
public function (IFoodProvider $foodProvider){ | |
$this -> foodProvider = $foodProvider | |
} | |
public function eat () { | |
$food= $this -> foodProvider -> getFood(); | |
} | |
} | |
interface IFoodProvider{ | |
public function getFood(); | |
} | |
class Restaurant implements IFoodProvider{ | |
public function getFood{ | |
return $food; | |
} | |
} | |
class SomeRestaurant implements IFoodProvider { | |
public function getFood(){ | |
return $food; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If we have an interface and it contains methods that aren’t connected to each other then you need to split this interface into two parts | |
(no need to model all in one interface) | |
// We shouldn’t use unused methods. Our interfaces should provide the methods we will use | |
public class Main { | |
ITeacher iTeacher = new EnglishTeacher(); | |
ITeacher.teaches(); | |
ITeacher.eat(); | |
ITeacher.studyenglish(); | |
} | |
interface Teacher{ | |
void teaches(); | |
} | |
interface Eater{ | |
void eat(); | |
} | |
interface StudyerEnglish{ | |
void studyenglish(); | |
} | |
interface ITeacher exends Teacher, Eater, StudyerEnglish { | |
void teaches(); | |
void eat(); | |
void studyenglish(); | |
} | |
class EnglishTeacher implements ITeacher { | |
@Override | |
public void teaches() { | |
System.out.println("teaches"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("eat"); | |
} | |
@Override | |
public void studyenglish() { | |
System.out.println("studyenglish"); | |
} | |
class MathematicTeacher implements StudyerEnglish { | |
@Override | |
public void studyenglish() { | |
System.out.println("studyenglish"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this principle if we get the heir object in the base class, we must work with it as a base class. | |
class LapTop{ | |
void TurnOn(){ | |
System.out.println("I can turn on") | |
} | |
} | |
class Asus extends LapTop{ | |
void TurnOn(){ | |
System.out.println("I can turn on") | |
} | |
void TurnOff(){ | |
System.out.println("I can turn off") | |
} | |
} | |
class Lenovo extends LapTop{ | |
void TurnOn(){ | |
System.out.println("I can turn on") | |
} | |
void TurnOff(){ | |
System.out.println("I can turn off") | |
} | |
void TurnRestart(){ | |
System.out.println("I can turn restart") | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In this principle you need to design the system in such a way that if you want to add a new functionality to the system we have not to rewrite the old classes. | |
// use the interface | |
public class Main { | |
Mercedes mercedes = new Mercedes (); | |
Bentley bentley = new Bentley (); | |
Toyota toyota = new Toyota (); | |
goToHome(mersedes); | |
goToHome(bentley); | |
goToHome(toyota); | |
} | |
static void goToHome (Car car){ | |
car.goToHome (); | |
} | |
interface Car { | |
void goToHome(); | |
} | |
class Mercedes implements Car { | |
@Override | |
void goToHome() { | |
} | |
} | |
class Bentley implements Car { | |
@Override | |
void goToHome() { | |
} | |
} | |
class Toyota implements Car { | |
@Override | |
void goToHome() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this principle, one class should do one task. That is, one part solves one task. | |
For example: | |
public class Dog { | |
void voice (){ | |
System.out.println("Gaw") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment