際際滷

際際滷Share a Scribd company logo
kgolev.com@kotseto
Writing SOLID Code
Kostadin Golev
CTO @ Rewards Labs
亠舒亰从舒亰  亠仍亠仄亠仆亳 仆舒 舒亰亢亟亠仆亳亠
kgolev.com@kotseto
仂亞舒仄亳舒仄 > 10 亞仂亟亳仆亳
仂亠亳仂仆舒仍仆亳亠 仄亳 亳仆亠亠亳
于从仍ム于舒 gile, DevOps 亳 Test
automation. 仂亟 弍舒舒 仆舒 亟于亠 亟亠舒.
弍亳舒仄 亟舒 仗亳舒, 弍仍仂亞舒 仄亳 亠 仆舒仄亳舒 仆舒
kgolev.com
kgolev.com@kotseto
kgolev.com@kotseto
S ingle Responsibility Principle
O pen-Closed Principle
L iskov Substitution Principle
I nterface Segregation Principle
D ependency Inversion Principle
kgolev.com@kotseto
亠 舒 亰舒从仂仆亳
kgolev.com@kotseto
仆仄亠仆亳 亳仍亳 亳亟亠亳
仂仄舒亞舒 亟舒 于亰亳仄舒仄亠 仗仂-亟仂弍亳 亠亠仆亳
亳仍仂亢亳仄亳 舒 于 仄仆仂亞仂 亳 舒亰仍亳仆亳 亳舒亳亳
仂仄舒亞舒 亟舒 从仂仄仆亳从亳舒仄亠 仗仂-亟仂弍亠
从仂 亳仄舒 仆亠仂, 从仂亠仂 仆亳 舒亠于舒 亳仍亳 仆亠 仆亳 舒亠于舒 于
仆舒亳 亟亳亰舒亶仆, 仄仆仂亞仂 亠仂 仄仂亢亠仄 亟舒 仆舒仄亠亳仄
仗亳仆亳仗,  从仂亶仂 亟舒 亞仂 仂弍仆亳仄
kgolev.com@kotseto
舒亶 于舒仆, 从从舒舒 亳
于亠仆亳仍舒仂舒
kgolev.com@kotseto
kgolev.com@kotseto
磻舒 舒 仂 于舒亳 从仂亟
磡于舒 亟舒 仗舒于亳 亠亟仆仂 仆亠仂 亳
亟舒 亞仂 仗舒于亳 亟仂弍亠
kgolev.com@kotseto
仂亟舒 磡于舒 亟舒 亳仄舒 舒仄仂 亠亟仆舒
仗亳亳仆舒 亟舒 亠 仗仂仄亠仆亳
kgolev.com@kotseto
class TaxiService {
void orderTaxi(String phone, String address) {
if (phone.length() < 7) {
throw new BadPhoneNumberException
(Phone not valid!");
}
taxiPool.sendTaxi(address);
smsClient.sendMessage(phone,
"Your taxi is on the way!");
}
kgolev.com@kotseto
class TaxiService_V2 {
void orderTaxi(String phone, String address) {
if (phone.length() < 7
&& phone.length() > 12) { // <-- mistake
throw new BadPhoneNumberException
(Phone not valid!");
}
taxiPool.sendTaxi(address);
smsClient.sendMessage(phone,
Your taxi is on the way!");
}
kgolev.com@kotseto
boolean phoneIsNotValid(String phone) {
return phone.length() < 7
|| phone.length() > 12;
}
kgolev.com@kotseto
public class  {
public boolean phoneIsNotValid(String phone) {
return phone.length() < 7
|| phone.length() > 12;
}
}
kgolev.com@kotseto
class TaxiService_V3 {
void orderTaxi(String phone, String address) {
if (smsService.phoneIsNotValid(phone)) {
throw new BadPhoneNumberException
("Phone not valid!");
}
taxiPool.sendTaxi(address);
smsService.sendMessage(phone,
Your taxi is on the way!");
}
kgolev.com@kotseto
 仂亟舒 于亳 亠 亟仍亞 亳 仗舒于亳 仄仆仂亞仂 仆亠舒
 丐亟仆仂 于亳 亠 亟舒 仂弍仆亳亠 从舒从于仂 仗舒于亳 亠亟仆仂
仗舒亠 从仂亟
 仄亠仂 仆舒 仄亠仂亟舒/从仍舒舒 从仂亶仂 仗亳亠亠 仆礆舒
仂弍仂  从仂亟舒, 从仂亶仂 舒亰舒弍仂于舒亠
kgolev.com@kotseto
弌舒亳 亠仍亠于亳亰仂
kgolev.com@kotseto
kgolev.com@kotseto
弌仂亠舒 磡于舒 亟舒 弍亟亠
亰舒于仂亠仆 亰舒 仄仂亟亳亳从舒亳, 仆仂
仂于仂亠仆 亰舒 仆舒亟亞舒亢亟舒仆亠
kgolev.com@kotseto
Customer silverCustomer = new Customer(SILVER);
getDiscountAmount(100, silverCustomer); // 10
Customer goldenCustomer = new Customer(GOLD);
getDiscountAmount(100, silverCustomer); // 20
kgolev.com@kotseto
double getDiscountAmount (int purchaseTotal,
Customer customer) {
double discount = 0;
switch (customer.type()) {
case SILVER:
discount = 0.1;
break;
case GOLD:
discount = 0.2;
break;
}
return discount * purchaseTotal;
}
kgolev.com@kotseto
class Customer {
double getDiscount() {
return 0;
}
}
kgolev.com@kotseto
class SilverCustomer extends Customer {
@Override
double getDiscount() {
return 0.1;
}
}
class GoldenCustomer extends Customer {
@Override
double getDiscount() {
return 0.2;
}
}
kgolev.com@kotseto
double getDiscountAmount_V2 (int purchaseTotal,
Customer customer) {
return customer.getDiscount() * purchaseTotal;
}
kgolev.com@kotseto
仂弍舒于礌亠仂 仆舒 仆仂于舒 仆从亳仂仆舒仍仆仂 亳亰亳从于舒
仄仆仂亢亠于仂 仗仂仄亠仆亳 于 舒 从仂亟, 于仄亠仂 仗亠亟亳仄仆仂
仗亳舒仆亠 仆舒 仆仂于
kgolev.com@kotseto
弌舒仍舒舒  从仂亰亠 亳亠仆亠
kgolev.com@kotseto
kgolev.com@kotseto
亠从亳 从仍舒, 从仂亶仂 仗亳亠仄舒 X 从舒仂
仗舒舒仄亠, 磡于舒 亟舒 仄仂亢亠 亟舒
舒弍仂亳  于亠从亳 仗仂亟从仍舒 仆舒 X
kgolev.com@kotseto
kgolev.com@kotseto
interface Duck {
void quack();
}
kgolev.com@kotseto
class RealDuck implements Duck {
@Override
public void quack() {
//quack code goes here!
}
}
kgolev.com@kotseto
Duck realDuck = new RealDuck();
realDuck.quack(); //works!
kgolev.com@kotseto
Duck duck = new ElectricDuck();
duck.quack(); //doesn't work?
kgolev.com@kotseto
class ElectricDuck implements Duck {
boolean turnedOn = false;
public void quack() {
if (turnedOn) {
//quack code goes here!
}
}
public void turnOn() {  }
}
kgolev.com@kotseto
if (duck instanceof ElectricDuck) {
((ElectricDuck)duck).turnOn();
}
duck.quack(); //now works!
kgolev.com@kotseto
class ElectricDuck_V2 implements Duck {
boolean turnedOn = false;
public void quack() {
if (!turnedOn) {
turnOn();
}
//quack code goes here!
}
public void turnOn() {  }
}
kgolev.com@kotseto
Duck anyDuck = new AnyKindOfDuck();
anyDuck.quack(); //always works
kgolev.com@kotseto
舒从舒从仂: 仆亠 磡于舒 从仂亟舒 亟舒 于亳
亳亰仆亠仆舒亟舒
kgolev.com@kotseto
 舒仄礌舒舒 仆舒 亳仗舒 仆舒 亠亟亳仆 从仍舒 仗仂仄亠仆
仗仂于亠亟亠仆亳亠仂 仄
 舒仍舒亞舒 亠 亟舒 仗仂于亠磦舒仄亠 亳仗舒, 亰舒 亟舒 亳亰弍亠亞仆亠仄
仆亠亢亠仍舒仆仂 仗仂于亠亟亠仆亳亠
kgolev.com@kotseto
仗舒舒 仗仂 亳仆亠亠亳
kgolev.com@kotseto
kgolev.com@kotseto
亠 仗亳仆亢亟舒于舒亶亠 仆亳从仂亶 亳 仆亳仂 亟舒 亰舒于亳亳 仂
亳仆亠亠亶 从仂亶仂 仆亠 仄 亠 仆亢亠仆
kgolev.com@kotseto
interface Parent {
void takeCareOfChildren();
}
interface Programmer {
void code();
}
interface Driver {
void drive();
}
kgolev.com@kotseto
class Kostadin implements Parent, Programmer,  {
void takeCareOfChildren();
void code();

}
kgolev.com@kotseto
kgolev.com@kotseto
 仂仍礆 亳仆亠亠亶  仄仆仂亞仂 仄亠仂亟亳
 舒亰仆舒 亳仄仗仍亠仄亠仆舒亳 仆舒 仄亠仂亟 仂 亳仆亠亠亶,
从仂亶仂 亳亰仗仂仍亰于舒仄亠
kgolev.com@kotseto
The Open Source Library
kgolev.com@kotseto
kgolev.com@kotseto
弌仄礌舒舒 仆舒 亟于亳亞舒亠仍 仆亠 磡于舒 亟舒 于仂亟亳 亟仂 仄礌舒
仆舒 舒于仂仄仂弍亳仍舒
Depend upon abstractions
Do not depend on concretions
kgolev.com@kotseto
class OpelDiesel {
OpelDieselEngine engine;
OpelDiesel() {
engine = new OpelDieselEngine();
}
void start() {
engine.ignition();
}
}
kgolev.com@kotseto
class OpelGasoline {
OpelGasolineEngine engine;
OpelGasoline() {
engine = new OpelGasolineEngine();
}
void start() {
engine.ignition();
}
}
kgolev.com@kotseto
OpelDiesel car = new OpelDiesel();
car.start();
OpelGasoline carGasoline = new OpelGasoline();
carGasoline.start();
kgolev.com@kotseto
class Opel {
Engine engine;
Opel(Engine engine) {
this.engine = engine;
}
void start() {
engine.ignition();
}
}
kgolev.com@kotseto
class OpelDieselEngine implements Engine {
@Override
public void ignition() {
}
}
kgolev.com@kotseto
Opel dieselCar = new Opel(new OpelDieselEngine());
dieselCar.start();
Opel gasolineCar = new Opel(new OpelGasolineEngine());
dieselCar.start();
kgolev.com@kotseto
Opel oldOpel = new Opel(new VolgaEngine(),
new MoskvitchBrakes());
kgolev.com@kotseto
 仂仄礌舒舒 亳仍亳 亟仂弍舒于礌亠 仆舒 仄仂亟仍 于仂亟亳 亟仂
仗仂仄亠仆亳 仆舒 仄仂亟仍亳亠, 从仂亳仂 亠亟 仆舒亟 仆亠亞仂
(仄礌舒 仆舒 从仂仍舒舒 亰舒舒亟亳 仆仂于 舒从仄仍舒仂)
 仂亟仍亳 仆舒 仗仂-于亳仂从仂 仆亳于仂 (从仂仍舒), 亰舒于亳 仂
仂仆仂 仂仗亠亟亠仍亠仆亳 仄仂亟仍亳 仆舒 仆亳从仂 仆亳于仂
(亟于亳亞舒亠仍/舒从仄仍舒仂)
kgolev.com@kotseto
Software should be soft!
By De鍖nition
kgolev.com@kotseto
仗仂亳?
@kotseto
kgolev.com/talks/solid

More Related Content

Writing SOLID Code