三、大话设计模式 之 装饰模式

模式特点:动态地为对象增加额外的职责

程序实例:展示一个人一件一件穿衣服的过程。

package main

import (
"fmt"
)

type Person struct {
Name string
}

func (p *Person) show() {
fmt.Println("装扮的", p.Name)
}

type AbstractPerson interface {
show()
}

type Finery struct {
AbstractPerson
}

func (f *Finery) Decorate(component AbstractPerson) {
f.AbstractPerson = component
}

func (f *Finery) show() {
if f.AbstractPerson != nil {
f.AbstractPerson.show()
}
}

type TShirts struct {
Finery
}

func (t *TShirts) show() {
t.Finery.show()
fmt.Println("大T恤")
}

type BigTrouser struct {
Finery
}

func (b *BigTrouser) show() {
b.Finery.show()
fmt.Println("大裤衩")
}

type Sneakers struct {
Finery
}

func (s *Sneakers) show() {
s.Finery.show()
fmt.Println("破球鞋")
}

type LeatherShoes struct {
Finery
}

func (l *LeatherShoes) show() {
l.Finery.show()
fmt.Println("皮鞋")
}

type Suit struct {
Finery
}

func (s *Suit) show() {
s.Finery.show()
fmt.Println("西装")
}

type Tie struct {
Finery
}

func (t *Tie) show() {
t.Finery.show()
fmt.Println("领带")
}

func main() {
person := &(Person{"小菜"})
fmt.Println("第一种装扮:")
pqx := new(Sneakers)
kk := new(BigTrouser)
dtx := new(TShirts)
pqx.Decorate(person)
kk.Decorate(pqx)
dtx.Decorate(kk)
dtx.show()

fmt.Println("第二种装扮:")
px := new(LeatherShoes)
ld := new(Tie)
xz := new(Suit)
px.Decorate(person)
ld.Decorate(px)
xz.Decorate(ld)
xz.show()

fmt.Println("第三种装扮:")
pqx2 := new(Sneakers)
px2 := new(LeatherShoes)
kk2 := new(BigTrouser)
ld2 := new(Tie)
pqx2.Decorate(person)
px2.Decorate(pqx2)
kk2.Decorate(px2)
ld2.Decorate(kk2)
ld2.show()
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值