Skip to content

Commit a8d9e2f

Browse files
committed
Refactor: 객체 간의 협력 방식 개선
- TicketSeller가 Audience에게 티켓을 판매하도록 변경 - Audience가 Bag을 통해 티켓을 구매하도록 변경 - Bag이 초대장 소지 여부에 따라 티켓 구매 로직을 처리하도록 변경 - TicketOffice의 getTicket 메서드를 통해 티켓을 발급하도록 변경 - 관련 클래스들의 생성자를 간소화하고 접근 제어자 변경 - 이번 리팩토링을 통해 객체 간의 협력 방식을 개선하고, 불필요한 메서드 호출을 줄여 코드의 가독성과 유지보수성을 높였습니다.
1 parent c5562f3 commit a8d9e2f

5 files changed

Lines changed: 24 additions & 39 deletions

File tree

src/ticket/audience.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Bag } from './bag';
2+
import { Ticket } from './ticket';
23

34
export class Audience {
45
private readonly bag: Bag;
@@ -7,7 +8,7 @@ export class Audience {
78
this.bag = bag;
89
}
910

10-
get getBag(): Bag {
11-
return this.bag;
11+
buy(ticket: Ticket): number {
12+
return this.bag.hold(ticket);
1213
}
1314
}

src/ticket/bag.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@ import { Invitation } from './invitation';
22
import { Ticket } from './ticket';
33

44
export class Bag {
5-
private amount: number;
6-
7-
private invitation: Invitation;
8-
95
private ticket: Ticket;
106

11-
constructor(invitation: Invitation, amount: number) {
12-
this.invitation = invitation;
13-
this.amount = amount;
14-
}
7+
constructor(private invitation: Invitation, private amount: number) {}
158

169
public hasInvitation(): boolean {
1710
return this.ticket != null;
@@ -28,4 +21,17 @@ export class Bag {
2821
public plusAmount(amount: number) {
2922
this.amount += amount;
3023
}
24+
25+
public hold(ticket: Ticket) {
26+
if (this.hasInvitation()) {
27+
this.setTicket(ticket);
28+
return 0;
29+
}
30+
31+
this.setTicket(ticket);
32+
this.minusAmount(ticket.getFee);
33+
this.setTicket(ticket);
34+
35+
return ticket.getFee;
36+
}
3137
}

src/ticket/theater.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ import { Audience } from './audience';
22
import { TicketSeller } from './ticketSeller';
33

44
export class Theater {
5-
private ticketSeller: TicketSeller;
6-
7-
constructor(ticketSeller: TicketSeller) {
8-
this.ticketSeller = ticketSeller;
9-
}
5+
constructor(private ticketSeller: TicketSeller) {}
106

117
public enter(audience: Audience): void {
12-
if (audience.getBag.hasInvitation()) {
13-
const ticket = this.ticketSeller.getTicketOffice.getTicket;
14-
audience.getBag.setTicket(ticket);
15-
} else {
16-
const ticket = this.ticketSeller.getTicketOffice.getTicket;
17-
audience.getBag.minusAmount(ticket.getFee);
18-
this.ticketSeller.getTicketOffice.plusAmount(ticket.getFee);
19-
audience.getBag.setTicket(ticket);
20-
}
8+
this.ticketSeller.sellTo(audience);
219
}
2210
}

src/ticket/ticketOffice.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { Ticket } from './ticket';
22

33
export class TicketOffice {
4-
private amount: number;
5-
6-
private tickets: Array<Ticket>;
7-
8-
constructor(amount: number, tickets: Array<Ticket>) {
9-
this.amount = amount;
10-
this.tickets = tickets;
11-
}
4+
constructor(private amount: number, private tickets: Array<Ticket>) {}
125

136
get getTicket(): Ticket {
147
return this.tickets.pop();

src/ticket/ticketSeller.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { TicketOffice } from './ticketOffice';
2+
import { Audience } from './audience';
23

34
export class TicketSeller {
4-
private readonly ticketOffice: TicketOffice;
5+
constructor(private ticketOffice: TicketOffice) {}
56

6-
constructor(ticketOffice: TicketOffice) {
7-
this.ticketOffice = ticketOffice;
8-
}
9-
10-
get getTicketOffice(): TicketOffice {
11-
return this.ticketOffice;
7+
sellTo(audience: Audience) {
8+
this.ticketOffice.plusAmount(audience.buy(this.ticketOffice.getTicket));
129
}
1310
}

0 commit comments

Comments
 (0)