pattern

package
v0.0.0-...-765d1ed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

设计模式

  • 创建型 C
  • 结构型 J
  • 行为型 X
   -AbstractFactory.go : 抽象工厂模式(C)
   -Adapter.go : 适配器模式(J)
   -Bridge.go : 桥接模式(J)
   -Builder.go : 建造者模式(C)
   -ChainOfResponsibility.go : 责任链模式(X)
   -Command.go : 命令模式(X)
   -Composite.go : 组合模式(J)
   -Decorator.go : 装饰器模式(J)
   -Facade.go : 外观模式(J)
   -Factory.go : 工厂模式(C)
   -Flyweight.go : 享元模式(J)
   -Interpreter.go : 解释器模式(X)
   -Iterator.go : 迭代器模式(X)
   -Mediator.go : 中介者模式(X)
   -Memento.go : 备忘录模式(X)
   -Observer.go : 观察者模式(X)
   -Prototype.go : 原型模式(C)
   -Proxy.go : 代理模式(J)
   -Singleton.go : 单例模式(C)
   -Singleton2.go : 单例模式(C)
   -State.go : 状态模式(X)
   -Strategy.go : 策略模式(X)
   -Template.go : 模板模式(X)
   -Visitor.go : 访问者模式(X)

参考资料

Documentation

Overview

开发模式代码实现包

设计模式

  • 创建型 C

  • 结构型 J

  • 行为型 X

    -AbstractFactory.go : 抽象工厂模式(C) -Adapter.go : 适配器模式(J) -Bridge.go : 桥接模式(J) -Builder.go : 建造者模式(C) -ChainOfResponsibility.go : 责任链模式(X) -Command.go : 命令模式(X) -Composite.go : 组合模式(J) -Decorator.go : 装饰器模式(J) -Facade.go : 外观模式(J) -Factory.go : 工厂模式(C) -Flyweight.go : 享元模式(J) -Interpreter.go : 解释器模式(X) -Iterator.go : 迭代器模式(X) -Mediator.go : 中介者模式(X) -Memento.go : 备忘录模式(X) -Observer.go : 观察者模式(X) -Prototype.go : 原型模式(C) -Proxy.go : 代理模式(J) -Singleton.go : 单例模式(C) -Singleton2.go : 单例模式(C) -State.go : 状态模式(X) -Strategy.go : 策略模式(X) -Template.go : 模板模式(X) -Visitor.go : 访问者模式(X)

Index

Constants

View Source
const (
	FOOD  = "food"
	DRINK = "drink"
)

Variables

This section is empty.

Functions

func AdapterTest

func AdapterTest()

test

func ChainOfResponsibilityTest

func ChainOfResponsibilityTest()

func CommandTest

func CommandTest()

func CompositeTest

func CompositeTest()

example

func DecoratorTest

func DecoratorTest()

调试

func Double

func Double(n int) int

func FacadeTest

func FacadeTest()

func FlyweightTest

func FlyweightTest()

func InterpreterTest

func InterpreterTest()

func IteratorTest

func IteratorTest()

func MediatorTest

func MediatorTest()

func MementoTest

func MementoTest()

func ObserverTest

func ObserverTest()

func ProxyTest

func ProxyTest()

func RunGame

func RunGame(g Game)

func Singleton2SaySomething

func Singleton2SaySomething()

func StateTest

func StateTest()

func StrategyTest

func StrategyTest()

func TemplateTest

func TemplateTest()

func TestBridge

func TestBridge()

func VisitorTest

func VisitorTest()

Types

type ABData

type ABData struct {
	A int
	B int
}

func (*ABData) Accept

func (as *ABData) Accept(vi Visitor)

type AObserver

type AObserver struct {
	Id string
}

func (*AObserver) Notify

func (ao *AObserver) Notify(sub interface{})

type AToB

type AToB struct {
	// 距离
	ABDistance float64
	// 到达方式策略
	Strategy Strategy
}

从A到B

func (*AToB) Do

func (ab *AToB) Do()

type AbsGame

type AbsGame struct {
}

func (*AbsGame) End

func (ag *AbsGame) End()

func (*AbsGame) Playing

func (ag *AbsGame) Playing()

func (*AbsGame) Start

func (ag *AbsGame) Start()

type AbsScreenEventHandler

type AbsScreenEventHandler struct {
	NextHandler IScreenEventHandler
}

func (*AbsScreenEventHandler) Handle

func (ase *AbsScreenEventHandler) Handle(se *ScreenEvent) bool

func (*AbsScreenEventHandler) SetNextHandler

func (ase *AbsScreenEventHandler) SetNextHandler(ise IScreenEventHandler)

type AbstractFactory

type AbstractFactory interface {
	Produce() pen //生产笔
}

抽象工厂

type AdapterNonToYes

type AdapterNonToYes struct {
	NonRechargeableBattery
}

适配可充电电池使用接口

func (AdapterNonToYes) Charge

func (AdapterNonToYes) Charge()

type AddVisitor

type AddVisitor struct {
}

func (*AddVisitor) Visit

func (av *AddVisitor) Visit(dataS DataStruct)

type AirStrategy

type AirStrategy struct {
	Speed float64
}

func (*AirStrategy) Do

func (as *AirStrategy) Do(ab interface{})

type AndExpression

type AndExpression struct {
	A Expression
	B Expression
}

func (*AndExpression) Interpret

func (ae *AndExpression) Interpret(context string) bool

type ArrayContainer

type ArrayContainer struct {
	// contains filtered or unexported fields
}

数组集合

func (*ArrayContainer) GetIterator

func (ac *ArrayContainer) GetIterator() Iterator

type ArrayIterator

type ArrayIterator struct {
	// contains filtered or unexported fields
}

数组迭代器

func (*ArrayIterator) HasNext

func (ai *ArrayIterator) HasNext() bool

func (*ArrayIterator) Next

func (ai *ArrayIterator) Next() interface{}

type BaseMessageBuilder

type BaseMessageBuilder struct {
}

基本信息构造器

func (*BaseMessageBuilder) Build

func (b *BaseMessageBuilder) Build(messages ...string) string

type Beer

type Beer struct {
	Drink
}

啤酒

func (Beer) Name

func (Beer) Name() string

func (Beer) Price

func (Beer) Price() float32

type BikeStrategy

type BikeStrategy struct {
	Speed float64
}

func (*BikeStrategy) Do

func (bs *BikeStrategy) Do(ab interface{})

type BraceMessageBuilderDecorator

type BraceMessageBuilderDecorator struct {
	Builder MessageBuilder
}

大括号装饰器

func (*BraceMessageBuilderDecorator) Build

func (b *BraceMessageBuilderDecorator) Build(messages ...string) string

type BrushPen

type BrushPen struct {
}

func (BrushPen) Produce

func (BrushPen) Produce() pen

type BusStrategy

type BusStrategy struct {
	Speed float64
}

func (*BusStrategy) Do

func (bs *BusStrategy) Do(ab interface{})

type CdnRequest

type CdnRequest struct {
}

func (*CdnRequest) HttpRequest

func (cdn *CdnRequest) HttpRequest() (*http.Request, error)

type ChatRoom

type ChatRoom struct {
	// contains filtered or unexported fields
}

func (*ChatRoom) RegisterUser

func (cr *ChatRoom) RegisterUser(u *User)

func (*ChatRoom) SendMsg

func (cr *ChatRoom) SendMsg(msg string)

type Client

type Client struct {
	Client *http.Client
}

客户端

func (*Client) Query

func (c *Client) Query(req Request) (resp *http.Response, err error)

type Cloneable

type Cloneable interface {
	Clone() interface{}
}

type CmdContext

type CmdContext struct {
	CmdType string
	Args    interface{}
}

上下文

type Coder

type Coder struct {
}

func (*Coder) Coding

func (c *Coder) Coding()

type Cola

type Cola struct {
	Drink
}

可乐

func (Cola) Name

func (Cola) Name() string

func (Cola) Price

func (Cola) Price() float32

type Command

type Command interface {
	Do(args interface{}) (interface{}, error)
}

命令接口

type CommandHandler

type CommandHandler struct {
	CmdMap map[string]Command
}

命令管理者

func (*CommandHandler) Handle

func (ch *CommandHandler) Handle(ctx *CmdContext) (interface{}, error)

处理命令

func (*CommandHandler) Register

func (ch *CommandHandler) Register(cmdType string, cmd Command)

注册命令

type Company

type Company struct {
	ProductPlanner
	Coder
	Tester
	MaintenancePeople
}

公司

拥有产品策划、程序员、测试人员,运维人员
通过公司这个外观对外提供服务,
而不是直接通过某个类型人员对外服务(虽然最终提供服务的也是某个类型的人员)

func (*Company) Producing

func (com *Company) Producing()

对外提供生产产品服务 需要各个类型人员合作,但细节不对外公开的

type Container

type Container interface {
	GetIterator() Iterator
}

集合接口

type Context

type Context struct {
}

func (*Context) Clone

func (this *Context) Clone() interface{}

type Context1

type Context1 struct {
	Uri string
	Context
}

func (*Context1) SetUri

func (this *Context1) SetUri(uri string)

func (*Context1) String

func (this *Context1) String() (str string)

type Context2

type Context2 struct {
	Context
	Echo string
}

func (*Context2) SetEcho

func (this *Context2) SetEcho(echo string)

func (*Context2) String

func (this *Context2) String() (str string)

type ContextPool

type ContextPool map[string]*Context

原型池

func (*ContextPool) AddContext

func (this *ContextPool) AddContext(key string, val *Context)

func (*ContextPool) GetContext

func (this *ContextPool) GetContext(key string) *Context

type Cricket

type Cricket struct {
	*AbsGame
}

func (*Cricket) Playing

func (fb *Cricket) Playing()

type DataStruct

type DataStruct interface {
	Accept(Visitor)
}

type DbConnect

type DbConnect struct {
}

数据库连接

func (*DbConnect) Do

func (*DbConnect) Do()

type DbConnectPool

type DbConnectPool struct {
	ConnChan chan *DbConnect
}

数据库连接池

func NewDbConnectPool

func NewDbConnectPool(chanLen int) *DbConnectPool

func (*DbConnectPool) Get

func (dc *DbConnectPool) Get() *DbConnect

func (*DbConnectPool) Put

func (dc *DbConnectPool) Put(conn *DbConnect)

type Department

type Department struct {
	List   map[string]*Department
	Parent *Department
	Name   string
	Id     string
}

func (*Department) Add

func (this *Department) Add(departments ...*Department)

批量添加子节点

func (*Department) AddTo

func (this *Department) AddTo(parent *Department)

添加到对应的父节点

func (*Department) Find

func (this *Department) Find(id string) (department *Department)

查找子节点

func (*Department) ReadList

func (this *Department) ReadList()

遍历

func (*Department) Remove

func (this *Department) Remove(id string) (department *Department)

移除子节点

type Device

type Device interface {
	Read() ([]byte, error)
	Write(word []byte) error
}

type Drink

type Drink struct {
}

饮料

func (Drink) Category

func (Drink) Category() string

func (Drink) Name

func (Drink) Name() string

func (Drink) Price

func (Drink) Price() float32

type Expression

type Expression interface {
	Interpret(context string) bool
}

type Food

type Food struct {
}

食物

func (Food) Category

func (Food) Category() string

func (Food) Name

func (Food) Name() string

func (Food) Price

func (Food) Price() float32

type FootBall

type FootBall struct {
	*AbsGame
}

func (*FootBall) Playing

func (fb *FootBall) Playing()

type FriedChicken

type FriedChicken struct {
	Food
}

炸鸡

func (FriedChicken) Name

func (FriedChicken) Name() string

func (FriedChicken) Price

func (FriedChicken) Price() float32

type Game

type Game interface {
	Start()
	Playing()
	End()
}

type GetCommand

type GetCommand struct {
}

Get 命令

func (*GetCommand) Do

func (gc *GetCommand) Do(args interface{}) (interface{}, error)

type Hamburger

type Hamburger struct {
	Food
}

汉堡

func (Hamburger) Name

func (Hamburger) Name() string

func (Hamburger) Price

func (Hamburger) Price() float32

type HardDisk

type HardDisk struct {
	// contains filtered or unexported fields
}

func (*HardDisk) Read

func (h *HardDisk) Read() ([]byte, error)

func (*HardDisk) Write

func (h *HardDisk) Write(word []byte) error

type HardDiskProxy

type HardDiskProxy struct {
	OpId string
	// contains filtered or unexported fields
}

func (*HardDiskProxy) Read

func (h *HardDiskProxy) Read() ([]byte, error)

func (*HardDiskProxy) Write

func (h *HardDiskProxy) Write(word []byte) error

type HomeScreenEventHandler

type HomeScreenEventHandler struct {
	AbsScreenEventHandler
}

func (*HomeScreenEventHandler) Handle

func (hse *HomeScreenEventHandler) Handle(se *ScreenEvent) bool

type IScreenEventHandler

type IScreenEventHandler interface {
	Handle(*ScreenEvent) bool
	SetNextHandler(IScreenEventHandler)
}

type Item

type Item interface {
	Price() float32   //价钱
	Name() string     //名称
	Category() string //类别
}

type Iterator

type Iterator interface {
	HasNext() bool
	Next() interface{}
}

迭代器接口

type Light

type Light struct {
	State LightState
}

func (*Light) PressSwitch

func (l *Light) PressSwitch()

type LightState

type LightState interface {
	PressSwitch(light *Light)
}

灯状态

type LiveRequest

type LiveRequest struct {
}

func (*LiveRequest) HttpRequest

func (cdn *LiveRequest) HttpRequest() (*http.Request, error)

type MaintenancePeople

type MaintenancePeople struct {
}

func (*MaintenancePeople) Releasing

func (m *MaintenancePeople) Releasing()

type Meal

type Meal []Item

func (*Meal) AddItem

func (this *Meal) AddItem(item ...Item)

func (Meal) GetCost

func (this Meal) GetCost() (cost float32)

func (Meal) ShowItems

func (this Meal) ShowItems() (msg string)

type MealBuilder

type MealBuilder struct {
}

建造者

func (MealBuilder) MealOne

func (MealBuilder) MealOne() (meal *Meal)

type Memento

type Memento struct {
	Value string
}

备忘结构

type MessageBuilder

type MessageBuilder interface {
	Build(messages ...string) string
}

接口

type NonRechargeableA

type NonRechargeableA struct {
}

不可充电电池A

func (NonRechargeableA) Use

func (NonRechargeableA) Use()

type NonRechargeableB

type NonRechargeableB struct {
	RechargeableBatteryAbstract
}

func (NonRechargeableB) Use

func (NonRechargeableB) Use()

type NonRechargeableBattery

type NonRechargeableBattery interface {
	Use()
}

不可充电电池使用接口

type Object

type Object func(int) int

func LogDecorate

func LogDecorate(fn Object) Object

type Observer

type Observer interface {
	Notify(interface{})
}

type OffLightState

type OffLightState struct {
}

func (*OffLightState) PressSwitch

func (ols *OffLightState) PressSwitch(light *Light)

关灯

type OnLightState

type OnLightState struct {
}

func (*OnLightState) PressSwitch

func (ols *OnLightState) PressSwitch(light *Light)

开灯

type OrExpression

type OrExpression struct {
	A Expression
	B Expression
}

func (*OrExpression) Interpret

func (oe *OrExpression) Interpret(context string) bool

type PenFactory

type PenFactory struct {
}

工厂

func (PenFactory) Produce

func (this PenFactory) Produce(typ string) pen

func (PenFactory) ProduceBrushPen

func (PenFactory) ProduceBrushPen() pen

func (PenFactory) ProducePencil

func (PenFactory) ProducePencil() pen

type PencilFactory

type PencilFactory struct {
}

func (PencilFactory) Produce

func (PencilFactory) Produce() pen

type PostCommand

type PostCommand struct {
}

post 命令

func (*PostCommand) Do

func (pc *PostCommand) Do(args interface{}) (interface{}, error)

type ProductPlanner

type ProductPlanner struct {
}

func (*ProductPlanner) Planing

func (p *ProductPlanner) Planing()

type QuoteMessageBuilderDecorator

type QuoteMessageBuilderDecorator struct {
	Builder MessageBuilder
}

引号装饰器

func (*QuoteMessageBuilderDecorator) Build

func (q *QuoteMessageBuilderDecorator) Build(messages ...string) string

type RechargeableBattery

type RechargeableBattery interface {
	Use()
	Charge()
}

可充电电池使用接口

type RechargeableBatteryAbstract

type RechargeableBatteryAbstract struct {
}

func (RechargeableBatteryAbstract) Charge

func (RechargeableBatteryAbstract) Charge()

func (RechargeableBatteryAbstract) Use

type Request

type Request interface {
	HttpRequest() (*http.Request, error)
}

请求接口

type ScreenEvent

type ScreenEvent struct {
	Type    string
	Comment string
}

type Singleton

type Singleton interface {
	SaySomething()
}

func NewSingletonInstance

func NewSingletonInstance() Singleton

type Singleton2

type Singleton2 interface {
	SaySomething()
}

type Storage

type Storage struct {
	*list.List
}

管理备忘记录

func (*Storage) RPop

func (s *Storage) RPop() *list.Element

Back returns the last element of list l or nil. and remove form list

type Strategy

type Strategy interface {
	Do(interface{})
}

type SubVisitor

type SubVisitor struct {
}

func (*SubVisitor) Visit

func (sv *SubVisitor) Visit(dataS DataStruct)

type Subject

type Subject struct {
	// contains filtered or unexported fields
}

func (*Subject) Attach

func (s *Subject) Attach(observer ...Observer)

func (*Subject) NotifyAllObservers

func (s *Subject) NotifyAllObservers()

func (*Subject) SetState

func (s *Subject) SetState(state string)

type TerminalExpression

type TerminalExpression struct {
	Word string
}

func (*TerminalExpression) Interpret

func (te *TerminalExpression) Interpret(context string) bool

终结符

type Tester

type Tester struct {
}

func (*Tester) Testing

func (t *Tester) Testing()

type Text

type Text struct {
	Value string
}

文本编辑

func (*Text) Read

func (t *Text) Read() string

读取

func (*Text) RestoreFromMemento

func (t *Text) RestoreFromMemento(m *Memento)

从备忘恢复

func (*Text) SaveToMemento

func (t *Text) SaveToMemento() *Memento

备忘

func (*Text) Write

func (t *Text) Write(value string)

type User

type User struct {
	// contains filtered or unexported fields
}

func (*User) SendMsg

func (u *User) SendMsg(msg string)

type UserScreenEventHandler

type UserScreenEventHandler struct {
	AbsScreenEventHandler
}

func (*UserScreenEventHandler) Handle

func (use *UserScreenEventHandler) Handle(se *ScreenEvent) bool

type Visitor

type Visitor interface {
	Visit(DataStruct)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL