====== 13 - Principy SOLID, DRY ====== ==== Opakování - dědičnost a polymorfismus ==== === Co vypíše metoda f() a proč? === class Mac { private String getName() { return "Mac"; } public void printName() { System.out.println( getName()); } } class PC extends Mac { private String getName() { return "PC"; } static void f() { new PC().printName(); } } === Proč neuspějeme při implementaci třídy Pulkruh? === interface Hranaty { public int obvod(); } interface Kulaty { public double obvod(); } class Pulkruh implements Hranaty, Kulaty { /** zde má být implementace **/ } === Kde nastane chyba? === public class Test { class A { String str = "ab"; A() { printLength(); } void printLength() { System.out.println(str.length()); } } class B extends A { String str = "abc"; void printLength() { System.out.println(str.length()); } } public static void main(String[] args) { new Test().new B(); } } ==== SOLID ==== * **S** - Single responsibility principle * **O** - Open-closed principle * **L** - Liskov substitution principle * **I** - Interface segregation principle * **D** - Dependency inversion principle === Chybný kód === class Book { private String name; private String author; private String content; // ... getters // ... settters public void print() { // book printing code } public void read() { // book reading code } } public class Customer { public void add() { try { // Database code goes here } catch (Exception ex) { try (PrintWriter writer = new PrintWriter("Error.txt", "UTF-8")) { writer.println(ex.toString()); } catch (IOException e) { // do something } } } } class Payment { public void pay(Method method, Money amount) { if (method.isCash()) { confirmPaidUsingCash(amount); printReceipt(amount); dispatchGoods(); } else if (method.isTransfer()) { confirmPaidUsingTransfer(amount); printReceipt(amount); dispatchGoods(); } else { throw new IllegalArgumentException("Unknown payment option."); } } } public class Customer { private int custType; public int getCustType() { return custType; } public void setCustType(int type) { custType = type; } public double getDiscount(double totalSales) { if (custType == 1) { return totalSales - 100; } else { return totalSales - 50; } } public void add() { /*...*/ } } public class Enquiry extends Customer { @Override public double getDiscount(double totalSales) { return super.getDiscount(totalSales) - 5; } @Override public void add() { throw new UnsupportedOperationException("Not allowed"); } } interface Lifecycle { void start(); void stop(); } class Customer { private final FileLogger logger = new FileLogger(); public void add() { try { // Database code goes here } catch (Exception ex) { logger.handle(ex.toString()); } } } ==== DRY - Don’t Repeat Yourself ==== Pokud se v systému nějaká informace vyskytuje na různých úrovních abstrakce nebo v různých artefaktech mělo by jedno z těchto míst být jednoznačně určeno jako směrodatné a ostatní by se od něj měly odvozovat. * Princip DRY zahrnuje všechny zdroje informací v systému * DRY se vztahuje pouze na primární zdroje informací, které upravují lidé (investice do nástrojů) * Ne každá duplikace je porušením DRY {{:courses:a7b36omo:labs:solution:seminar13solution.zip|řešení}} Zdroj: \\ [1] [[http://voho.cz/wiki/princip-solid/|Princip SOLID]] \\ [2] [[https://www.codeproject.com/Articles/703634/SOLID-architecture-principles-using-simple-Csharp|SOLID architecture principles using simple C# examples]]