Golang and design pattern-Adapter adapter pattern

The adapter pattern is a structural design pattern that we often use in our work. For example, while upgrading the interface, it is also necessary to ensure compatibility with the old excuse.

Scenes

We will encounter this kind of situation in our life. The size of the three-jack (10A) we commonly use is different from that of the air conditioner plug (16A), although they are all three-jack. When we bought an air conditioner and found that the plug of the air conditioner cannot be inserted into our original (10A) jack during installation, we can buy a plug converter, as shown in the figure below.

 We can insert the 10A reverse plug into our normal 10A three-jack, and then insert the plug of the air conditioner into the front 16A three-jack. Here, our plug converter plays the role of adapter (Adapter).

Next, I will take this plug converter application as an example to express the adapter mode in the form of code. Although there are a lot of demo codes, they are very representative, please read carefully

basic interface

Here we first define three interfaces

Electrical equipment (Appliances) interface

Defines a method GetName() that returns the name of the appliance, and all household appliances need to implement this interface

// 电器设备接口
type Appliances interface {
	GetName() string
}

10A plug electrical equipment (Appliances10A) interface 

Combination (Golang does not have the concept of inheritance) Appliances interface, and defines a method Plug10A() to implement this interface. All electrical appliances with 10A plugs need to implement this interface

// 10A插头电器接口,所有10A插头的电器设备都要实现该接口
type Appliances10A interface {
	// 组合电器设备接口
	Appliances
	// 插头是10A的,实现该接口
	Plug10A()
}

16A plug electrical equipment (Appliances16A) interface

Basically the same as Appliances10A, combine the Appliances interface, and define the method Plug16A() to implement the interface. All electrical appliances with 16A plugs need to implement this interface

// 16A插头电器接口,所有16A插头的电器设备都要实现该接口
type Appliances16A interface {
	Appliances
	// 插头是16A的,实现该接口
	Plug16A()
}

Realization of electrical equipment

Electrical appliances with 10A plug-television (TV)

TV is an electrical device with a 10A plug, so the TV type needs to implement the GetName() method of Appliances and the Plug10A() method

// 电视机
type TV struct {
	// 电器名称,比如: 空调,电视,冰箱
	Name string
}

func (tv *TV) GetName() string {
	return tv.Name
}

// 电视机是10A的插头
func (tv *TV) Plug10A() {}

Electrical appliances with 16A plug - air conditioner (HVAC)

HVAC is an electrical device with a 16A plug, so it also implements the GetName() method and Plug16A() method of Appliances

// 空调
type HVAC struct {
	Name string
}

func (hvac *HVAC) GetName() string {
	return hvac.Name
}

// 空调是16A的插头
func (hvac *HVAC) Plug16A() {}

infrastructure

Standard 10A socket plug board (PlugBoard)

Because it is a 10A jack, a method Insert10A() for inserting a 10A plug appliance is defined. The input parameter must be a 10A plug appliance type.

// 插线板(10A)
type PlugBoard struct {
	// 电器集合
	Appliances []Appliances
}

// 插入 10A插头的电器设备
func (p *PlugBoard) Insert10A(app Appliances10A) {
	p.Appliances = append(p.Appliances, app)
}

// 插线板通电,那么在插线板上的电器都通上电了
func (p *PlugBoard) TransferCurrent() {
	for _, app := range p.Appliances {
		fmt.Printf("%s 已通电 \n", app.GetName())
	}
}

Adapter

Adapter is an electrical device that can be considered as a 10A plug, so Plug10A() is implemented, but because it is a converter, there is a 16A jack on the back, so a method for plugging in a 16A plug is determined

// 适配器
type Adapter struct {
	// 正面(10A)插入的插线板
	PB PlugBoard
	// 反面(16A)接入的电器设备
	APP Appliances16A
}
// 适配器能够插入插线板,且能通电,所以本身也是一个电器设备
func (a *Adapter) GetName() string{
	return a.APP.GetName()
}

// 正面是 10A的插头
func (a *Adapter) Plug10A() {}

// 背面有 16A的插孔,所以可以插入一个16A插头的设备
func (a *Adapter) Insert16A(app Appliances16A) {
	a.APP = app
	a.PB.Insert10A(a)
}

test

func main() {
	// 电视机
	tv := TV{Name: "电视机"}
	// 空调
	hvac := HVAC{Name: "空调"}
	//插线板
	pb := PlugBoard{}

	// 插线板上插入 电视机插头
	pb.Insert10A(&tv)

	// 适配器
	adapter := Adapter{}

	//插线板上插入 适配器插头
	pb.Insert10A(&adapter)

	// 适配器上插入 空调插头
	adapter.Insert16A(&hvac)

	// 给插线板通电
	pb.TransferCurrent()
}

operation result

The code has been uploaded to Github: LyonNee/design_patterns_with_go: Golang-based design pattern code (github.com)

Guess you like

Origin blog.csdn.net/Lyon_Nee/article/details/119494748