SlideShare a Scribd company logo
1 of 59
Download to read offline
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 Definition
kgolev.com@kotseto
Въпроси?
@kotseto
kgolev.com/talks/solid

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Writing SOLID Code