S - Single responsibility principle
O - Open/Close principle
L - Liskov substitution principle
I - Interface segregation principle
D - Dependency inversion principle
class Book {
private Name name;
private Author author;
private Content content;
// ... getters
// ... settters
public void print() {
// book printing code
}
public void read() {
// book reading code
}
}
class Book {
private Name name;
private Author author;
private Content content;
// ... getters
// ... settters
}
class Printer {
public void print(Book book) {
// book printing code
}
}
class Reader {
public void read(Book book) {
// book reading code
}
}
Zajistíme především pomocí abstrakce a polymorfismu
class Payment {
public void pay(Method method, Money amount) {
if (method.isCash()) {
//do something
} else if (method.isTransfer()) {
//do something else
} else {
throw new IllegalArgumentException("Unknown payment option.");
}
}
}
interface Method {
void confirmPaymentReceived(Money amount);
}
class TransferMethod implements Method { /* ...confirmPaymentReceived... */ }
class CashMethod implements Method { /* ...confirmPaymentReceived... */ }
class Payment {
public void pay(Method method, Money amount) {
method.confirmPaymentReceived(amount);
printReceipt(amount);
dispatchGoods();
}
}
V podtřídách:
class Shape {}
class Rectangle extends shape {}
class Circle extends shape {}
class Drawer {
void draw(Shape shape) {
if (shape instanceof Rectangle) {
//draw
} else if (shape instanceof Circle) {
//draw
}
}
}
class Shape {
abstract void draw();
}
class Rectangle extends shape {
@Override
void draw() {
//draw
}
}
class Circle extends shape {
@Override
void draw() {
//draw
}
}
class Drawer {
void draw(Shape shape) {
shape.draw();
}
}
class Rectangle {
int width;
int height;
int getArea() {
return width*height;
}
//set & get
}
class Square extends Rectangle {
void setWidth(int width) {
this.width = width;
this.height = height;
}
void setHeight(int height) {
setWidth(height);
}
}
class Test {
void test() {
Rectangle rectangle =
SomeFactory.getRectangle();
rectangle.setWidth(5);
rectangle.setHeight(10);
rectangle.getArea();
//4 Rectangle get 50
//4 Squere get 100
}
}
interface Lifecycle {
void start();
void stop();
}
interface Startable {
void start();
}
interface Stoppable{
void stop();
}
class Worker {
void work(){
//working
}
}
class Manager {
Worker worker;
void manage(){
worker.work();
}
//get & set
}
class SuperWorker {
void work(){
//working much more
}
}
interface Worker {
void work();
}
class LazyWorker implements Worker {
void work(){
//working
}
}
class Manager {
Worker worker;
void manage(){
worker.work();
}
//get & set
}
class SuperWorker implements Worker{
void work(){
//working much more
}
}
(rozmlouvej s přáteli, ne s cizinci)
Metoda f třídy C smí volat jen:
value = object.getX().getY().getValue();
x = object.getX();
y = x.getY();
value = y.getValue();
public class Customer {
private String name;
private Wallet myWallet;
//get & set
}
public class Wallet {
private float value;
public void addMoney(float deposit) {
value += deposit;
}
public void subtractMoney(float debit) {
value -= debit;
}
//get & set
}
payment = 2.00;
Wallet wallet = myCustomer.getWallet();
if (wallet.getTotalMoney() > payment) {
wallet.subtractMoney(payment);
//OK
} else {
//NOK
}
public class Customer {
private String name;
private Wallet myWallet;
public String getName(){
return name;
}
public bool getPayment(float bill) {
if (myWallet != null && myWallet.getTotalMoney() > bill) {
myWallet.subtractMoney(payment);
return true;
} else {
return false;
}
}
payment = 2.00;
if (myCustomer.getPayment(payment)) {
// OK
} else {
// NOK
}
+ snižování provázanosti tříd
+ třídá má kontrolu svého vnitřního stavu
+ snažší orientace ve třídách
- třída musí poskytovat metody pro manipulaci se svou vnitřní strukturou