结构性设计模式

适配器模式

将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作。


image.png
package adapter


//适配器接口
type Target interface {
    Request() string
}

//被适配的目标接口
type Adaptee interface {
    SpecificRequest() string
}

//
type adapter struct {
    Adaptee
}

//Request 实现Target接口
func (a *adapter) Request() string {
    return a.SpecificRequest()
}

//NewAdapter 是Adapter的工厂函数
func NewAdapter(adaptee Adaptee) Target {
    return &adapter{
        Adaptee: adaptee,
    }
}

桥接模式

设想如果要绘制矩形、圆形、椭圆、正方形,我们至少需要4个形状类,但是如果绘制的图形需要具有不同的颜色,如红色、绿色、蓝色等。那么有两种方案:1.为每一种形状设置一套颜色方案;2.根据实际需要对形状和颜色进行组合。第二种方案就是桥接模式,将抽象部分与它的实现部分分离,使它们都可以独立地变化。


image.png

优点

1.分离抽象接口及其实现部分。
2.桥接模式有时类似于多继承方案,但是多继承方案违背了类的单一职责原则

缺点

桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进

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

装饰器模式

动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活


image.png
package decorator

//原始接口
type Component interface {
    Calc() int
}
//原始类
type ConcreteComponent struct{}

func (*ConcreteComponent) Calc() int {
    return 0
}

//装饰类
type MulDecorator struct {
    Component
    num int
}
//装饰类实现了原始接口
func (d *MulDecorator) Calc() int {
    return d.Component.Calc() * d.num
}

func WarpMulDecorator(c Component, num int) Component {
    return &MulDecorator{
        Component: c,
        num:       num,
    }
}

外观模式

外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。


image.png
package facade

import "fmt"

type AModuleAPI interface {
    TestA() string
}

type BModuleAPI interface {
    TestB() string
}

//装饰接口
type API interface {
    Test() string
}

//装饰结构体
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)
}

享元模式

运用共享技术有效地支持大量细粒度对象的复用。系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用。

image.png
// 享元对象接口
type IFlyweight interface {
    Operation(int) //来自外部的状态
}

// 共享对象
type ConcreteFlyweight struct {
    name string
}

func (c *ConcreteFlyweight) Operation(outState int) {
    if c == nil {
        return
    }
    fmt.Println("共享对象响应外部状态", outState)
}

// 不共享对象
type UnsharedConcreteFlyweight struct {
    name string
}

func (c *UnsharedConcreteFlyweight) Operation(outState int) {
    if c == nil {
        return
    }
    fmt.Println("不共享对象响应外部状态", outState)
}

代理模式

给某一个对象提供一个代 理,并由代理对象控制对原对象的引用


image.png
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

    // 在调用真实对象之前的工作,检查缓存,判断权限,实例化真实对象等。。
    res += "pre:"

    // 调用真实对象
    res += p.real.Do()

    // 调用之后的操作,如缓存结果,对结果进行处理等。。
    res += ":after"

    return res
}

你可能感兴趣的:(结构性设计模式)