Command mode 1

Source: Internet
Author: User

Requirement: there is a remote control with seven programmable slots. Each slot corresponds to a switch button, and the remote control also has an overall cancel button.

Controllable device: electric lights, fans, water heaters, audio equipment and other similar controllable equipment. Requires the ability to control the current device and any device that may occur in the future.

We think of this process as a restaurant meal process: the customer orders according to the menu, and then gives the order to the waiter. The waiter puts the order on the kitchen cabinet and the cook gets the order for execution, make delicious meals.

Here, the remote control is a remote control, which has a series of command interfaces. When we press the remote control button, the corresponding commands are issued for execution, the corresponding command object receives the command and sends the command to the specific executor. The executor receives the command and runs the corresponding text. This achieves the goal of decoupling the requester from the specific executor.

First, let's simplify the problem. Suppose we only have one remote control, which has only one button and corresponding slot, and can control one device.

The design diagram is as follows:

The implementation code is as follows:

Remote control class simpleremotecontrol:

Public class simpleremotecontrol {
Command slot;

Public simpleremotecontrol (){}

Public command getslot (){
Return slot;
}

Public void setslot (command slot ){
This. Slot = slot;
}

Public void buttonwaspressed ()
{
Slot.exe cute ();
}

}

 

Command interface:

Public interface command {
Public void execute ();
}

Specific command class:

Lightoncommand:

Public class lightoncommand implements command {
Light light;
Public lightoncommand (light)
{
This. Light = light;
}
@ Override
Public void execute (){
// Todo auto-generated method stub
Light. On ();
}

}

Lightbulb off command class:

Public class lightoffcommand implements command {
Light light;
Public lightoffcommand (light)
{
This. Light = light;
}
@ Override
Public void execute (){
// Todo auto-generated method stub
Light. Off ();
}

}

Door Opening command class:

Public class gargedooropencommand implements command {
Gargedoor;
Public gargedooropencommand (gargedoor)
{
This. gargedoor = gargedoor;
}
@ Override
Public void execute (){
// Todo auto-generated method stub
Gargedoor. open ();
}

}

Door close command class:

Public class gargedoorclosecommand implements command {
Gargedoor;
Public gargedoorclosecommand (gargedoor)
{
This. gargedoor = gargedoor;
}
@ Override
Public void execute (){
// Todo auto-generated method stub
Gargedoor. Close ();
}

}

Finally, the receiver executor class:

Light bulb:

Public class light {
Public void on ()
{
System. Out. println ("light is on ");
}
Public void off ()
{
System. Out. println ("light is close ");
}
}

Door:

Public class gargedoor {
Public void open ()
{
System. Out. println ("door is open ");
}
Public void close ()
{
System. Out. println ("door is close ");
}
}

Test class:

Public class remotecontroltest {
Public static void main (string [] ARGs)
{
Simpleremotecontrol remote = new simpleremotecontrol ();
Light = new light ();
Lightoncommand lighton = new lightoncommand (light );
Remote. setslot (lighton );
Remote. buttonwaspressed ();
Gargedoor = new gargedoor ();
Gargedooropencommand gargedooropen = new gargedooropencommand (gargedoor );
Remote. setslot (gargedooropen );
Remote. buttonwaspressed ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.