设计模式的六大原则
开闭原则
一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。在软件的生命周期内,因变化、升级和维护对原有代码进行修改,可能会引入错误。所以当软件需要变化时,通过扩展软件实体的行为来实现变化,...
创建文件Base.go
package visitor
type Customer interface { // 接待者
Accept(Visitor)
}
type Visitor interface { // 访问者
Visit(Customer)
}
创建文件EnterpriseCustomer.go
package visitor
type E...
创建文件Command.go
package command
type Command interface {
Execute() // 执行
}
创建文件motherBoard.go
package command
import "fmt"
type MotherBoard struct {
}
func (*MotherBoard) WashClothes()...
创建文件MMFeel.go
package memento
import "fmt"
type MMFeel struct {
Tall int
Rich int
Handsome int
}
var States []MMFeel = make([]MMFeel, 0)
var StatesLength int = 0 // 记录轨迹
f...
创建文件MM.go
package strategy
import "fmt"
type MMContext struct {
Name string
Age int
paoStrategy MMStrategy
}
func NewMMContext(name string, age int, strategy MMStrategy) *M...
创建文件CPU.go
package mediator
import "fmt"
type CPU struct {
data string
}
func (c *CPU) Process(data string) {
fmt.Println("CPU is running", data)
}
创建文件GPU.go
package mediator
impo...
创建文件Downloader.go
package template
import "fmt"
type Downloader interface {
Download(url string)
}
type template struct {
implement
url string
}
type implement interface {
downlo...
创建文件Week.go
package state
// 每个星期行为
type Week interface {
Today()
Next(*DayContext)
}
// 星期数据结构
type DayContext struct {
today Week
}
func NewDayContext() *DayContext {
...
创建文件Node.go
package interpreter
type Node interface {
Interpreter() int
}
type ValNode struct {
val int
}
func (v *ValNode) Interpreter() int {
return v.val
}
创建AddNode.go
package...
创建文件Observer.go
package observer
type Observer interface {
Update(subject *Subject)
}
创建文件Reader.go
package observer
import "fmt"
// 读取类
type Reader struct {
Name string
}
func N...
创建文件Manger.go
package chain
// 主管 经理 总经理 董事长
// 20 100 150 200
type Manger interface {
HaveRight(money int) bool // 判断权限
HandleFeeRequest(name string, mon...
创建文件ImageFlyWeight.go
package FlyWeight
import "fmt"
type ImageFlyWeight struct {
data string
}
// 初始化
func NewImageFlyWeight(filename string) *ImageFlyWeight {
data := fmt.Sprintf("image ...
创建文件Component.go
package Deacorator
type Component interface {
Calc() int
}
type ConcreateComponent struct {
}
func (*ConcreateComponent) Calc() int {
return 0
}
创建文件AddComponent.go
...
创建文件Message.go
package bridge
// SNS短信
// Email
type AbstractMessage interface {
SendMessage(text, to string) // 发送快,普通发送
}
type MessageImlementer interface {
Send(text, to string) //...
package main
import "fmt"
// 测试
type API interface {
Test() string
}
type APICall struct {
// 接口
a AmoudleAPI
b BmoudleAPI
}
func (api *APICall) Test() string {
return fmt.S...