글래스의 결론 - 이론보다 실무가 먼저이다.
WHY? 건축처럼 역사가 오래된 학문이 아닌, 짧은 역사를 가진 소프트웨어 학문은 실무를 관찰한 결과를 바탕으로 이론을 정립하는 것이 최선이기 때문이다.
그래서 앞으로 우리는 추상적인 개념과 이론을 앞세워 공부하기 보다는 코드를 통해 객체지향을 이해하자.
package com.object.ticket;
public class Theater {
private TicketSeller ticketSeller;
public Theater(TicketSeller ticketSeller) {
this.ticketSeller = ticketSeller;
}
public void enter(Audience audience){
if(audience.getBag().hasInvitation()){ // 초대장이 있으면
Ticket ticket = ticketSeller.getTicketOffice().getTicket(); // 티켓을 발급한다.
audience.getBag().setTicket(ticket); // 고객에게 티켓을 준다.
}else { // 초대장이 없으면
Ticket ticket = ticketSeller.getTicketOffice().getTicket(); // 티켓을 발급한다.
audience.getBag().minusAmount(ticket.getFee()); // 고객이 티켓값을 지불한다.
ticketSeller.getTicketOffice().plusAmount(ticket.getFee()); // 매표소에 돈이 증가한다.
audience.getBag().setTicket(ticket); // 고객에게 티켓을 준다.
}
}
}
public class Audience {
private Bag bag;
public Audience(Bag bag) {
this.bag = bag;
}
public Bag getBag() {
return bag;
}
}
package com.object.ticket;
public class Bag {
private Long amount;
private Invitation invitation;
private Ticket ticket;
public Bag(Long amount) {
this(null, amount);
}
public Bag(Invitation invitation, Long amount) {
this.invitation = invitation;
this.amount = amount;
}
public boolean hasInvitation() {
return invitation != null;
}
public boolean hasTicket() {
return ticket != null;
}
public void setTicket(Ticket ticket){
this.ticket = ticket;
}
public void minusAmount(Long amount){
this.amount -= amount;
}
public void plusAmount(Long Amount){
this.amount += amount;
}
}
package com.object.ticket;
public class TicketSeller {
private TicketOffice ticketOffice;
public TicketSeller(TicketOffice ticketOffice) {
this.ticketOffice = ticketOffice;
}
public TicketOffice getTicketOffice() {
return ticketOffice;
}
}
초대장이 있으면 티켓을 발급하여 고객에게 티켓을 주고
초대장이 없으면 고객이 티켓값을 지불하고 티켓을 발급한 다음, 매표소의 돈이 채워지고 고객에게 티켓을 준다.
하지만, 문제점을 가지고 있는 코드다.
로버트 마틴은 <클린 소프트웨어>에서 소프트웨어 모듈이 가져야 하는 세 가지 기능을 설명했다.