Search
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(); } }
interface Hranaty { public int obvod(); } interface Kulaty { public double obvod(); } class Pulkruh implements Hranaty, Kulaty { /** zde má být implementace **/ }
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(); } }
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()); } } }
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.
řešení
Zdroj: [1] Princip SOLID [2] SOLID architecture principles using simple C# examples