_1_command

package
v0.0.0-...-eb36113 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0, MIT Imports: 1 Imported by: 0

README

命令模式

命令模式本质是把某个对象的方法调用封装到对象中,方便传递、存储、调用。

示例中把主板单中的启动(start)方法和重启(reboot)方法封装为命令对象,再传递到主机(box)对象中。于两个按钮进行绑定:

  • 第一个机箱(box1)设置按钮1(button1) 为开机按钮2(button2)为重启。
  • 第二个机箱(box1)设置按钮2(button2) 为开机按钮1(button1)为重启。

从而得到配置灵活性。

除了配置灵活外,使用命令模式还可以用作:

  • 批处理
  • 任务队列
  • undo, redo

等把具体命令封装到对象中使用的场合

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

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

func NewBox

func NewBox(button1, button2 Command) *Box

func (*Box) PressButton1

func (b *Box) PressButton1()

func (*Box) PressButton2

func (b *Box) PressButton2()

type Command

type Command interface {
	Execute()
}
Example
mb := &MotherBoard{}
startCommand := NewStartCommand(mb)
rebootCommand := NewRebootCommand(mb)

box1 := NewBox(startCommand, rebootCommand)
box1.PressButton1()
box1.PressButton2()

box2 := NewBox(rebootCommand, startCommand)
box2.PressButton1()
box2.PressButton2()
Output:

system starting
system rebooting
system rebooting
system starting

type MotherBoard

type MotherBoard struct{}

func (*MotherBoard) Reboot

func (*MotherBoard) Reboot()

func (*MotherBoard) Start

func (*MotherBoard) Start()

type RebootCommand

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

func NewRebootCommand

func NewRebootCommand(mb *MotherBoard) *RebootCommand

func (*RebootCommand) Execute

func (c *RebootCommand) Execute()

type StartCommand

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

func NewStartCommand

func NewStartCommand(mb *MotherBoard) *StartCommand

func (*StartCommand) Execute

func (c *StartCommand) Execute()

Jump to

Keyboard shortcuts

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