{TIL} 객체지향 특강 1부

Kwaksang·2024년 5월 16일

TIL

목록 보기
19/37
post-thumbnail

객체지향 특강

Chapter .1 객체지향

Content .1 무엇이 먼저인가?

  1. 실무 (프로젝트 진행)
  2. 이론

실무를 관찰한 결과를 바탕으로 이론이 정립된다

출처 : 로버트 L.글래스 [소프트웨어 크리에이티비티 2.0]

Content .2 실습

  • 화살표를 그려보자
  • Cinema에서 거의 모두 호출하게 됨
  • 실선은 직접 호출
  • 점선은 간접 호출
  • Cinema와 다른 클래스 사이에 의존도가 높다. (=결합도가 높다)

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));
}

이런식으로 반복적으로 클래스의 의존도를 낮출 수 있음
유지성과 안정성을 높일 수 있음

Content .3 Cohesion

  • 의존과 반대로 호출을 많이 받을 수록 응집력이 높아짐
  • 객체지향은 의존은 하향, 응집은 상향 시키는 것을 지향함
  • 의존성과 반비례함

Content .4 Object Oriented Programming

  • 의존성은 낮추면서 응집성은 높이는 프로그래밍을 의미함
  • 기능에는 책임이 따름

Content .5 객체지향의 특징

  1. 캡슐화를 통해서 보호 (ex> public field x 보단 private field with get; set 이용)
  2. 추상화 (프로퍼티)
  3. 상속
  4. 다형성 (상속된 클래스도 각자만의 특성을 가짐)
profile
게임은 재미와 기능!

0개의 댓글