Skip to content

Commit 3744902

Browse files
committed
design patterns - decorator pattern
1 parent b4dd24d commit 3744902

File tree

14 files changed

+397
-5
lines changed

14 files changed

+397
-5
lines changed

design-patterns/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
## 목차
88

9-
| Chapter | Pattern | Description | Directory | Classification |
10-
|:-------:|:--------:|:-------------------------------|:------------------------------:|:---------------:|
11-
| 1 | 전략(Stratergy) |동일 계열의 알고리즘군을 정의하고 캡슐화하여 상호 교환이 가능하게 함| [ch1-strategy](./ch1-strategy) |행동(행위 패턴)|
12-
| 2 |옵저버(Observer)|객체 상태가 변할 때 관련 객체들이 그 변화를 전달받아 자동으로 캐싱| [ch2-observer](./ch2-observer) |행동(행위 패턴)|
9+
| Chapter | Pattern | Description | Directory | Classification |
10+
|:-------:|:----------------:|:-------------------------------|:------------------------------:|:--------------:|
11+
| 1 | 전략(Stratergy) |동일 계열의 알고리즘군을 정의하고 캡슐화하여 상호 교환이 가능하게 함| [ch1-strategy](./ch1-strategy) | 행동(행위) 패턴 |
12+
| 2 | 옵저버(Observer) |객체 상태가 변할 때 관련 객체들이 그 변화를 전달받아 자동으로 캐싱| [ch2-observer](./ch2-observer) | 행동(행위) 패턴 |
13+
| 3 | 데코레이터(Decorator) |주어진 상황에 따라 객체에 다른 객체를 덧붙임| [ch3-decorator](./ch3-decorator) | 구조 패턴 |
1314

1415
## GoF 디자인 패턴
1516
#### 목적에 따른 분류

design-patterns/ch2-observer/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Observer pattern
2-
- 한 객체의 상태가 바귀면 그 객체에 의존하는 다른 객체들한테 연락이 가고 자동으로 내용이 갱신되는 방식으로 일대다(one to many) 의존성을 정의
2+
- 한 객체의 상태가 바뀌면 그 객체에 의존하는 다른 객체들한테 연락이 가고 자동으로 내용이 갱신되는 방식으로 일대다(one to many) 의존성을 정의
33
- 일대다 관계는 주제와 옵저버에 의해 정의하고, 옵저버는 주제에 의존
44

55
## 방법
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package component
2+
3+
const (
4+
TallSize = iota
5+
GrandeSize
6+
VentiSize
7+
)
8+
9+
var BeverageSizeMap = map[int]string{
10+
TallSize: "TALL",
11+
GrandeSize: "GRANDE",
12+
VentiSize: "VENTI",
13+
}
14+
15+
type Beverage interface {
16+
GetDescription() string
17+
Cost() float64
18+
GetSize() string
19+
SetSize(size int)
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package component
2+
3+
import "fmt"
4+
5+
type DarkRoast struct {
6+
description string
7+
size int
8+
}
9+
10+
func CreateDarkRoast() *DarkRoast {
11+
return &DarkRoast{
12+
description: "다크 로스트 커피",
13+
size: 0,
14+
}
15+
}
16+
17+
func (b *DarkRoast) GetDescription() string {
18+
return b.description + fmt.Sprintf("[%v]", b.GetSize())
19+
}
20+
21+
func (b *DarkRoast) Cost() float64 {
22+
return 0.99
23+
}
24+
25+
func (b *DarkRoast) GetSize() string {
26+
return BeverageSizeMap[b.size]
27+
}
28+
29+
func (b *DarkRoast) SetSize(size int) {
30+
b.size = size
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package component
2+
3+
import "fmt"
4+
5+
type Decaf struct {
6+
description string
7+
size int
8+
}
9+
10+
func CreateDecaf() *Decaf {
11+
return &Decaf{
12+
description: "디카페인 커피",
13+
size: 0,
14+
}
15+
}
16+
17+
func (b *Decaf) GetDescription() string {
18+
return b.description + fmt.Sprintf("[%v]", b.GetSize())
19+
}
20+
21+
func (b *Decaf) Cost() float64 {
22+
return 1.05
23+
}
24+
25+
func (b *Decaf) GetSize() string {
26+
return BeverageSizeMap[b.size]
27+
}
28+
29+
func (b *Decaf) SetSize(size int) {
30+
b.size = size
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package component
2+
3+
import "fmt"
4+
5+
type Espresso struct {
6+
description string
7+
size int
8+
}
9+
10+
func CreateEspresso() *Espresso {
11+
return &Espresso{
12+
description: "에스프레소",
13+
size: 0,
14+
}
15+
}
16+
17+
func (b *Espresso) GetDescription() string {
18+
return b.description + fmt.Sprintf("[%v]", b.GetSize())
19+
}
20+
21+
func (b *Espresso) Cost() float64 {
22+
return 1.99
23+
}
24+
25+
func (b *Espresso) GetSize() string {
26+
return BeverageSizeMap[b.size]
27+
}
28+
29+
func (b *Espresso) SetSize(size int) {
30+
b.size = size
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package component
2+
3+
import "fmt"
4+
5+
type HouseBlend struct {
6+
description string
7+
size int
8+
}
9+
10+
func CreateHouseBlend() *HouseBlend {
11+
return &HouseBlend{
12+
description: "하우스 블렌드 커피",
13+
size: 0,
14+
}
15+
}
16+
17+
func (b *HouseBlend) GetDescription() string {
18+
return b.description + fmt.Sprintf("[%v]", b.GetSize())
19+
}
20+
21+
func (b *HouseBlend) Cost() float64 {
22+
return 0.89
23+
}
24+
25+
func (b *HouseBlend) GetSize() string {
26+
return BeverageSizeMap[b.size]
27+
}
28+
29+
func (b *HouseBlend) SetSize(size int) {
30+
b.size = size
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package decorator
2+
3+
import "decorator/component"
4+
5+
type Milk struct {
6+
beverage component.Beverage
7+
}
8+
9+
func AddMilk(b component.Beverage) *Milk {
10+
return &Milk{
11+
beverage: b,
12+
}
13+
}
14+
15+
func (m *Milk) GetDescription() string {
16+
return m.beverage.GetDescription() + ", 우유"
17+
}
18+
19+
func (m *Milk) Cost() float64 {
20+
cost := m.beverage.Cost()
21+
switch m.beverage.GetSize() {
22+
case component.BeverageSizeMap[component.TallSize]:
23+
cost += 0.1
24+
break
25+
case component.BeverageSizeMap[component.GrandeSize]:
26+
cost += 0.15
27+
break
28+
case component.BeverageSizeMap[component.VentiSize]:
29+
cost += 0.2
30+
break
31+
}
32+
return cost
33+
}
34+
35+
func (m *Milk) GetSize() string {
36+
return m.beverage.GetSize()
37+
}
38+
39+
func (m *Milk) SetSize(size int) {
40+
m.beverage.SetSize(size)
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package decorator
2+
3+
import (
4+
"decorator/component"
5+
)
6+
7+
type Mocha struct {
8+
beverage component.Beverage
9+
}
10+
11+
func AddMocha(b component.Beverage) *Mocha {
12+
return &Mocha{
13+
beverage: b,
14+
}
15+
}
16+
17+
func (m *Mocha) GetDescription() string {
18+
return m.beverage.GetDescription() + ", 모카"
19+
}
20+
21+
func (m *Mocha) Cost() float64 {
22+
cost := m.beverage.Cost()
23+
switch m.beverage.GetSize() {
24+
case component.BeverageSizeMap[component.TallSize]:
25+
cost += 0.2
26+
break
27+
case component.BeverageSizeMap[component.GrandeSize]:
28+
cost += 0.25
29+
break
30+
case component.BeverageSizeMap[component.VentiSize]:
31+
cost += 0.3
32+
break
33+
}
34+
return cost
35+
}
36+
37+
func (m *Mocha) GetSize() string {
38+
return m.beverage.GetSize()
39+
}
40+
41+
func (m *Mocha) SetSize(size int) {
42+
//TODO implement me
43+
panic("implement me")
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package decorator
2+
3+
import "decorator/component"
4+
5+
type Soy struct {
6+
beverage component.Beverage
7+
}
8+
9+
func AddSoy(b component.Beverage) *Soy {
10+
return &Soy{
11+
beverage: b,
12+
}
13+
}
14+
15+
func (s *Soy) GetDescription() string {
16+
return s.beverage.GetDescription() + ", 두유"
17+
}
18+
19+
func (s *Soy) Cost() float64 {
20+
cost := s.beverage.Cost()
21+
switch s.beverage.GetSize() {
22+
case component.BeverageSizeMap[component.TallSize]:
23+
cost += 0.15
24+
break
25+
case component.BeverageSizeMap[component.GrandeSize]:
26+
cost += 0.2
27+
break
28+
case component.BeverageSizeMap[component.VentiSize]:
29+
cost += 0.25
30+
break
31+
}
32+
return cost
33+
}
34+
35+
func (s *Soy) GetSize() string {
36+
return s.beverage.GetSize()
37+
}
38+
39+
func (s *Soy) SetSize(size int) {
40+
//TODO implement me
41+
panic("implement me")
42+
}

0 commit comments

Comments
 (0)