適配器模式
將一個(gè)接口轉(zhuǎn)換成客戶希望的另一個(gè)接口,適配器模式使接口不兼容的那些類可以一起工作。

package adapter
//適配器接口
type Target interface {
Request() string
}
//被適配的目標(biāo)接口
type Adaptee interface {
SpecificRequest() string
}
//
type adapter struct {
Adaptee
}
//Request 實(shí)現(xiàn)Target接口
func (a *adapter) Request() string {
return a.SpecificRequest()
}
//NewAdapter 是Adapter的工廠函數(shù)
func NewAdapter(adaptee Adaptee) Target {
return &adapter{
Adaptee: adaptee,
}
}
橋接模式
設(shè)想如果要繪制矩形、圓形、橢圓、正方形,我們至少需要4個(gè)形狀類,但是如果繪制的圖形需要具有不同的顏色,如紅色、綠色、藍(lán)色等。那么有兩種方案:1.為每一種形狀設(shè)置一套顏色方案;2.根據(jù)實(shí)際需要對(duì)形狀和顏色進(jìn)行組合。第二種方案就是橋接模式,將抽象部分與它的實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化。

優(yōu)點(diǎn)
1.分離抽象接口及其實(shí)現(xiàn)部分。
2.橋接模式有時(shí)類似于多繼承方案,但是多繼承方案違背了類的單一職責(zé)原則
缺點(diǎn)
橋接模式的引入會(huì)增加系統(tǒng)的理解與設(shè)計(jì)難度,由于聚合關(guān)聯(lián)關(guān)系建立在抽象層,要求開發(fā)者針對(duì)抽象進(jìn)
package bridge
import "fmt"
type Color interface {
Use()
}
type Red struct{}
func (r Red) Use() {
fmt.Println("Use Red color")
}
type Green struct{}
func (g Green) Use() {
fmt.Println("Use Green color")
}
type Yellow struct{}
func (y Yellow) Use() {
fmt.Println("Use Yellow color")
}
type BrushPen interface {
DrawPicture()
}
type BigBrushPen struct {
Color
}
func (bbp BigBrushPen) DrawPicture() {
fmt.Println("Draw picture with big brush pen")
bbp.Use()
}
裝飾器模式
動(dòng)態(tài)地給一個(gè)對(duì)象增加一些額外的職責(zé)(Responsibility),就增加對(duì)象功能來說,裝飾模式比生成子類實(shí)現(xiàn)更為靈活

package decorator
//原始接口
type Component interface {
Calc() int
}
//原始類
type ConcreteComponent struct{}
func (*ConcreteComponent) Calc() int {
return 0
}
//裝飾類
type MulDecorator struct {
Component
num int
}
//裝飾類實(shí)現(xiàn)了原始接口
func (d *MulDecorator) Calc() int {
return d.Component.Calc() * d.num
}
func WarpMulDecorator(c Component, num int) Component {
return &MulDecorator{
Component: c,
num: num,
}
}
外觀模式
外部與一個(gè)子系統(tǒng)的通信必須通過一個(gè)統(tǒng)一的外觀對(duì)象進(jìn)行,為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,外觀模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。

package facade
import "fmt"
type AModuleAPI interface {
TestA() string
}
type BModuleAPI interface {
TestB() string
}
//裝飾接口
type API interface {
Test() string
}
//裝飾結(jié)構(gòu)體
type apiImpl struct {
a AModuleAPI
b BModuleAPI
}
func (a *apiImpl) Test() string {
aRet := a.a.TestA()
bRet := a.b.TestB()
return fmt.Sprintf("%s\n%s", aRet, bRet)
}
享元模式
運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度對(duì)象的復(fù)用。系統(tǒng)只使用少量的對(duì)象,而這些對(duì)象都很相似,狀態(tài)變化很小,可以實(shí)現(xiàn)對(duì)象的多次復(fù)用。

// 享元對(duì)象接口
type IFlyweight interface {
Operation(int) //來自外部的狀態(tài)
}
// 共享對(duì)象
type ConcreteFlyweight struct {
name string
}
func (c *ConcreteFlyweight) Operation(outState int) {
if c == nil {
return
}
fmt.Println("共享對(duì)象響應(yīng)外部狀態(tài)", outState)
}
// 不共享對(duì)象
type UnsharedConcreteFlyweight struct {
name string
}
func (c *UnsharedConcreteFlyweight) Operation(outState int) {
if c == nil {
return
}
fmt.Println("不共享對(duì)象響應(yīng)外部狀態(tài)", outState)
}
代理模式
給某一個(gè)對(duì)象提供一個(gè)代 理,并由代理對(duì)象控制對(duì)原對(duì)象的引用

package proxy
type Subject interface {
Do() string
}
type RealSubject struct{}
func (RealSubject) Do() string {
return "real"
}
type Proxy struct {
real RealSubject
}
func (p Proxy) Do() string {
var res string
// 在調(diào)用真實(shí)對(duì)象之前的工作,檢查緩存,判斷權(quán)限,實(shí)例化真實(shí)對(duì)象等。。
res += "pre:"
// 調(diào)用真實(shí)對(duì)象
res += p.real.Do()
// 調(diào)用之后的操作,如緩存結(jié)果,對(duì)結(jié)果進(jìn)行處理等。。
res += ":after"
return res
}