객체지향 특강
실무를 관찰한 결과를 바탕으로 이론이 정립된다
출처 : 로버트 L.글래스 [소프트웨어 크리에이티비티 2.0]
Cinema의 의존도를 낮추는 방법을 알아보자
// 기존 코드
public class Staff
{
// 일하는 매표소 정보
private BoxOffice boxOffice;
// 매표소 정보 return (반환)
public BoxOffice GetBoxOffice()
{
return boxOffice;
}
// 스태프 생성자 (매표소 정보 초기화)
public Staff(BoxOffice boxOffice)
{
this.boxOffice = boxOffice;
}
}
internal class Cinema
{
// 영화관에 있는 스태프
private Staff staff;
// 영화관 생성자 (스태프 초기화)
public Cinema(Staff staff)
{
this.staff = staff;
}
static void Main(string[] args)
{
Console.WriteLine("WELCOME TO SPARTA CINEMA");
}
// 영화관 입장.
public void Enter(Customer customer)
{
// 초대장이 있을때
if (customer.GetBag().HasInvitation())
{
// 티켓을 한장 건낸다.
Ticket ticket = staff.GetBoxOffice().GetTicket();
customer.GetBag().SetTicket(ticket);
}
// 초대장이 없을 때
else
{
// 티켓을 한장 건낸다.
Ticket ticket = staff.GetBoxOffice().GetTicket();
// 고객은 돈을 낸다.
customer.GetBag().SpendMoney(ticket.GetPrice());
// 스탭은 돈을 받는다.
staff.GetBoxOffice().ReceiveMoney(ticket.GetPrice());
// 고객은 티켓을 받는다.
customer.GetBag().SetTicket(ticket);
}
}
}
// 수정 코드
public class Staff
{
// 일하는 매표소 정보
private BoxOffice boxOffice;
// 매표소 정보 return (반환)
public BoxOffice GetBoxOffice()
{
return boxOffice;
}
// 스태프 생성자 (매표소 정보 초기화)
public Staff(BoxOffice boxOffice)
{
this.boxOffice = boxOffice;
}
}
public void SellTicket(Customer customer)
{
// 초대장이 있을때
if (customer.GetBag().HasInvitation())
{
// 티켓을 한장 건낸다.
Ticket ticket = GetBoxOffice().GetTicket();
customer.GetBag().SetTicket(ticket);
}
// 초대장이 없을 때
else
{
// 티켓을 한장 건낸다.
Ticket ticket = GetBoxOffice().GetTicket();
// 고객은 돈을 낸다.
customer.GetBag().SpendMoney(ticket.GetPrice());
// 스탭은 돈을 받는다.
GetBoxOffice().ReceiveMoney(ticket.GetPrice());
// 고객은 티켓을 받는다.
customer.GetBag().SetTicket(ticket);
}
}
internal class Cinema
{
// 영화관에 있는 스태프
private Staff staff;
// 영화관 생성자 (스태프 초기화)
public Cinema(Staff staff)
{
this.staff = staff;
}
static void Main(string[] args)
{
Console.WriteLine("WELCOME TO SPARTA CINEMA");
}
// 영화관 입장.
public void Enter(Customer customer)
{
staff.SellTicket(customer);
}
}
Cinema의 의존도가 낮아졌지만, Staff의 의존도가 높아짐
Staff의 의존도를 낮추는 방법을 알아보자
// 기존 코드
public class Customer
{
// 고객이 들고 있는 가방
private Bag bag;
// 고객 생성자 (가방 초기화)
public Customer(Bag bag)
{
this.bag = bag;
}
// 고객의 가방 정보 return(반환)
public Bag GetBag()
{
return bag;
}
}
public class Staff
{
// 일하는 매표소 정보
private BoxOffice boxOffice;
// 매표소 정보 return (반환)
public BoxOffice GetBoxOffice()
{
return boxOffice;
}
// 스태프 생성자 (매표소 정보 초기화)
public Staff(BoxOffice boxOffice)
{
this.boxOffice = boxOffice;
}
}
public void SellTicket(Customer customer)
{
// 초대장이 있을때
if (customer.GetBag().HasInvitation())
{
// 티켓을 한장 건낸다.
Ticket ticket = GetBoxOffice().GetTicket();
customer.GetBag().SetTicket(ticket);
}
// 초대장이 없을 때
else
{
// 티켓을 한장 건낸다.
Ticket ticket = GetBoxOffice().GetTicket();
// 고객은 돈을 낸다.
customer.GetBag().SpendMoney(ticket.GetPrice());
// 스탭은 돈을 받는다.
GetBoxOffice().ReceiveMoney(ticket.GetPrice());
// 고객은 티켓을 받는다.
customer.GetBag().SetTicket(ticket);
}
}
// 수정 코드
public class Customer
{
// 고객이 들고 있는 가방
private Bag bag;
// 고객 생성자 (가방 초기화)
public Customer(Bag bag)
{
this.bag = bag;
}
// 고객의 가방 정보 return(반환)
public Bag GetBag()
{
return bag;
}
public float BuyTicket(Ticket ticket)
{
// 초대장이 있을때
if (GetBag().HasInvitation())
{
// 티켓을 한장 건낸다.
GetBag().SetTicket(ticket);
return 0;
}
// 초대장이 없을 때
else
{
// 고객은 돈을 낸다.
GetBag().SpendMoney(ticket.GetPrice());
// 고객은 티켓을 받는다.
GetBag().SetTicket(ticket);
return ticket.GetPrice();
}
}
}
public class Staff
{
// 일하는 매표소 정보
private BoxOffice boxOffice;
// 매표소 정보 return (반환)
public BoxOffice GetBoxOffice()
{
return boxOffice;
}
// 스태프 생성자 (매표소 정보 초기화)
public Staff(BoxOffice boxOffice)
{
this.boxOffice = boxOffice;
}
}
public void SellTicket(Customer customer)
{
Ticket ticket = GetBoxOffice().GetTicket();
// 스탭은 돈을 받는다.
GetBoxOffice().ReceiveMoney(customer.Buyticket(ticket));
}
이런식으로 반복적으로 클래스의 의존도를 낮출 수 있음
유지성과 안정성을 높일 수 있음