デザインパターン構造モデルに行きます

Facadeパターン

1.定義:サブシステムは、サブシステムインターフェースのセットへの一貫したインターフェースを提供するために、統一されたオブジェクトによって実行される、外部と通信します。

コード例2:

// 定义对外API
type API interface {
    Test()
}

func NewAPI() API {
    return apiImpl{newMod()}
}

type apiImpl struct {
    m mod
}

func (a apiImpl) Test() {
    a.m.mod()
}

// 需要交互的内部模块
type mod interface {
    mod()
}

func newMod() mod {
    return modImpl{}
}

type modImpl struct {
}

func (m modImpl) mod() {

}

3.実装手順

  • 内部モジュールの定義
  • 外部インタフェースは、対話と実装を定義します

4.シーン

  • あなたがサブシステムにシンプルなインターフェイスを提供したい場合には、複雑なパターンの外観を使用することができます。インターフェイスは、ほとんどのユーザーのニーズを満たすために、しかし、ユーザーは、直接クラスの外観上のサブシステムにアクセスすることができます。
  • サブシステム間の複数の依存関係を持つ大きなクライアントがあります。顧客およびサブシステム切り離さ他のサブシステムを持つクラスの外観の導入は、サブシステムは、独立性と可搬性を向上させることができます。
  • 層の間の結合を低減するために、階層構造では、吸入システム、層の間に直接リンクの各層のパターンの外観を定義することができ、クラス出現によって関連付け。

利点

カスタマーシールドサブシステムコンポーネント

アダプタモード

1.定義:希望別のインターフェイスへのクライアントインターフェイス。

コード例2:

// 定义被适配的接口
type Adapter interface {
    Request() string
}

type adaptee struct {
}

func (adaptee) Request() string {
}

func NewAdapter() Adapter {
    return &adaptee{}
}

// 定义目标接口
type Target interface {
    TargetRequest() string
}

func New(adapter Adapter) Target {
    return &target{adapter}
}

type target struct {
    Adapter
}

func (t *target) TargetRequest() {
    t.Request()
}

3.実装手順

  • インタフェースと実装を定義するようになっています
  • ターゲット・インタフェースと実装を定義し、インターフェイスによって作成されたインタフェースが構成されている実装

4.シーン

システムは、システムのニーズを満たしていない既存のクラス、インタフェース、使用する必要があります。

利点

ターゲットクラスと既存のコードを変更することなく、いずれかを導入することによって、既存のフィッタアダプタクラスのクラスを再使用するために切り離さフィッタのクラス。

装飾的なパターン

1.定義:オブジェクトが動的にいくつかの追加の責任を追加します。

コード例2:

// 定义组件
type Component interface {
    Calc() int
}

type ConcreteComponent struct{}

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

// 定义装饰对象
type MulDecorator struct {
    Component
    num int
}

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

func (d *MulDecorator) Calc() int {
    return d.Component.Calc() * d.num
}

type AddDecorator struct {
    Component
    num int
}

func WarpAddDecorator(c Component, num int) Component {
    return &AddDecorator{
        Component: c,
        num:       num,
    }
}

func (d *AddDecorator) Calc() int {
    return d.Component.Calc() + d.num
}

3.実装手順

  • カスタムコンポーネント
  • 定義された装飾品
  • 装飾されたオブジェクト生成アセンブリ

4.シーン

他のオブジェクトに影響を与えずに、ダイナミックな、透明な方法は、個々のオブジェクトに責任を追加します。

利点

オブジェクトの動的な方法の機能によって拡張することができ、あなたは異なる動作を実現するために、異なる装飾ランタイム構成ファイルで選択することができます。

フライ級

1.定義:フライ級は、技術を共有することにより、再利用し、同一または類似の目的を達成します。

コード例2:

// 定义享元对象
type ImageFlyweight struct {
    data string
}

func NewImageFlyweight(filename string) *ImageFlyweight {
    // Load image file
    data := fmt.Sprintf("image data %s", filename)
    return &ImageFlyweight{
        data: data,
    }
}

func (i *ImageFlyweight) Data() string {
    return i.data
}

// 定义享元对象工厂
type ImageFlyweightFactory struct {
    maps map[string]*ImageFlyweight
}

var imageFactory *ImageFlyweightFactory

func GetImageFlyweightFactory() *ImageFlyweightFactory {
    if imageFactory == nil {
        imageFactory = &ImageFlyweightFactory{
            maps: make(map[string]*ImageFlyweight),
        }
    }
    return imageFactory
}

func (f *ImageFlyweightFactory) Get(filename string) *ImageFlyweight {
    image := f.maps[filename]
    if image == nil {
        image = NewImageFlyweight(filename)
        f.maps[filename] = image
    }

    return image
}

type ImageViewer struct {
    *ImageFlyweight
}

func NewImageViewer(filename string) *ImageViewer {
    image := GetImageFlyweightFactory().Get(filename)
    return &ImageViewer{
        ImageFlyweight: image,
    }
}

func (i *ImageViewer) Display() {
    fmt.Printf("Display: %s\n", i.Data())
}

3.実装手順

  • フライ級定義されたオブジェクト
  • 定義されたフライ級の工場
  • フライ級を作成するために、植物を使って

4.シーン

  • 大量のメモリを消費し、その結果オブジェクトのような多数の使用ので、同一又は類似のオブジェクトの数が多いシステム。
    オブジェクトのほとんどの状態は、これらの状態の外にすることができ、外部の着信オブジェクトにすることができます。
  • したがって、それは繰り返しフライ級のオブジェクトを使用する場合にのみフライ級を使用して価値がある必要があり、使用フライ級フライ級は、ストレージオブジェクトのフライ級のプールを維持する必要があり、これはリソースを要します。

利点

重複データオブジェクトから剥離フライ級は、メモリを節約し、オブジェクトの数を減少させる、別個のフライ級、共有複数のオブジェクトを変更し、複数のインスタンスが必要とされません。

プロキシモード

1.定義:元のオブジェクトへの制御プロキシオブジェクト参照によって、薬剤を提供することを目的。

コード例2:

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
}

3.実装手順

  • 定義されたインタフェース
  • 実装するオブジェクトの定義
  • その実装するプロキシオブジェクトを定義します。

4.シーン

  • 元のオブジェクトへのプロキシオブジェクト参照、注入ハイジャックを増加させる要求により制御

利点

発信者及び発信者を調整するプロキシモード、結合度をある程度低減されます。

ブリッジモード

1.定義:元のオブジェクトへの制御プロキシオブジェクト参照によって、薬剤を提供することを目的。

コード例2:

package bridge

import "fmt"

type AbstractMessage interface {
    SendMessage(text, to string)
}

type MessageImplementer interface {
    Send(text, to string)
}

type MessageSMS struct{}

func ViaSMS() MessageImplementer {
    return &MessageSMS{}
}

func (*MessageSMS) Send(text, to string) {
    fmt.Printf("send %s to %s via SMS", text, to)
}

type MessageEmail struct{}

func ViaEmail() MessageImplementer {
    return &MessageEmail{}
}

func (*MessageEmail) Send(text, to string) {
    fmt.Printf("send %s to %s via Email", text, to)
}

type CommonMessage struct {
    method MessageImplementer
}

func NewCommonMessage(method MessageImplementer) *CommonMessage {
    return &CommonMessage{
        method: method,
    }
}

func (m *CommonMessage) SendMessage(text, to string) {
    m.method.Send(text, to)
}

type UrgencyMessage struct {
    method MessageImplementer
}

func NewUrgencyMessage(method MessageImplementer) *UrgencyMessage {
    return &UrgencyMessage{
        method: method,
    }
}

func (m *UrgencyMessage) SendMessage(text, to string) {
    m.method.Send(fmt.Sprintf("[Urgency] %s", text), to)
}

3.実装手順

4.シーン

利点

おすすめ

転載: blog.51cto.com/14378068/2411614