1. Tasks to complete:
Objective: To design an API for a home Appliance automation remote control, so that each slot can be controlled by one or a group of devices. It is important to be able to control the current device and any future devices that may occur.
Known conditions:
(1) This remote control has 7 programmable slots (each can be assigned to a different appliance unit), each slot has a corresponding switch button. The remote also has an overall undo button.
(2) provide a CD-ROM, which is a manufacturer class, used to control home appliance automation devices, such as lights, fans, water heaters and other controllable devices. The sections below are as follows, with many classes and different interfaces.
1 classtv{2 On () {}3 off () {}4 Setinputchannel () {}5 SetVolume () {}6 }7 8 classceilinglight{9 On () {}Ten off () {} One Dim () {} A } - - classsprinkler{ the Wateron () {} - Wateroff () {} - } - + classsecuritycontrol{ - arm () {} + disarm () {} A } at -...
2. Thinking about the task:
(1) cannot let the remote control know too many vendor class details, cannot let it have the following statement. In this case, as long as there is a new vendor class coming in, we will have to modify the original code, endless, and may cause potential errors.
1 if (slot1 = = Light ) {2 light.on (); 3 }elseif(slot1 = = hottub) {4 Hottob.jetson (); 5 } ...
(2) that is to say "the requestor of the action" is decoupled from the "Performer of the Action" Object . Here, the action of the requestor is the remote control, the action of the performer is a variety of manufacturers of one instance. You can take command mode: The controller does not need to know what the job is, as long as a command object can communicate with the correct object.
(a) Then, in the object village (face-facing) restaurant, the application of this model is obvious: you (customer) write the order to the waitress---> waitress get the order to the chef---> Cook according to the order.
Each order is treated as an object, delivered from the customer to the waitress, and then delivered to the chef. There is only one method in the order: OrderUp (), which encapsulates the action required to prepare the order. and the waitress doesn't need to know what's inside, she just takes the order, and shouts the order (called the OrderUp () method). Then OrderUp will have a reference to the chef, calling the chef's Makeburger () method. Createorder ()--Takeorder ()--OrderUp ()--Makeburger () --Output
1 Public void OrderUp () {2 Cook.makeburger (); 3 Cook.makeshake (); 4 }
(b) Let's jump out of the restaurant and abstract the above question.
Customer creates an order: client Createcommandobject () and the OrderUp method in the order, encapsulates the action to prepare the Order: command has execute ()--Waitress take order: Invoker SetCommand ()--and the waitress shouted an order came: Call Command.Execute ()--chef orders to start working: Execute inside Call Receiver.action ()
3. Complete the first Command object: Suppose our remote control has only one button and corresponding slot.
1 Public Interface Command () {2 Public void Excute (); 3 }
1 Public classLightoncommandImplementscommand{2 Light light ;3 PublicLightoncommand (light) {4 This. Light =Light ;5 }6 Public voidExecute () {7 Light.on ();8 }9}
1 Public classsimpleremotecontrol{2 Command slot;3 PublicSimplerremotecontrol () {}4 5 Public voidsetcommand (Command command) {6Slot =command;7 }8 Public voidbuttonwaspressed () {9 Slot.execute ();Ten } One}
1 Public classremotecontroltest{2 Public voidMain (string[] args) {3 //The lamp is receiver receiver, which executes the command to turn the light on and off.4Light light =NewLight ();5Lightoncommand Lighton =NewLightoncommand (light);6 //The remote control is the caller Invoker, passing in a Command object SetCommand, or making a request buttonwaspressed7Simpleremotecontrol remote =NewSimpleremotecontrol ();8 Remote.setcommand (Lighton);9 remote. Buttonwaspressed ();Ten } One}
4. Seven-slot remote control:
Headfirst Study Note six: Command mode (4.30)