Command of design patterns-TV (original)

Source: Internet
Author: User

Command of design patterns-TV

 

Every night, TV remote control is a retained program in our house. My daughter prefers to watch cartoons, my wife prefers to watch TV series, and I prefer to watch football. Therefore, whoever controls the remote control is equal to realizing their own program dream. Hey, in fact, every time my daughter succeeded, and she said for a while, "Should you make the big one small ?", You don't know who you learned from this kid. Then the remote control was my wife's, and it was my turn. When I happily pressed the football screen, the announcer said, "Today's show is here. Please watch it tomorrow! ", I went viral.

As we all know, the program Panel (programpan) of the TV remote control is composed of program buttons. By selecting the corresponding program button, you can switch to the corresponding program screen.

Let's see how to select a program screen through the remote control button.

 

1. Here, define the remotecontrolbutton interface:

Public interface remotecontrolbutton {

Public abstract void selectprogram (); // select the program dashboard

}

2. Define the remotecontrolbutton interface implementation class:

A: cartoon program buttons (cartonprogrambutton) class:

Public class cartonprogrambutton implements remotecontrolbutton {

Public void selectprogram (){

System. Out. println ("selected cartoon channel! ");

}

}

B: TV series program buttons (tvplanprogrambutton) class:

Public class tvplanprogrambutton implements remotecontrolbutton {

Public void selectprogram (){

System. Out. println ("selected TV series screen! ");

}

}

C: football program buttons (footprogrambutton) class:

Public class footprogrambutton implements remotecontrolbutton {

Public void selectprogram (){

System. Out. println ("Football track selected! ");

}

}

3. Remote Control Program Panel (programpan): used to control program buttons and display programs

Public class programpan {

Public static list programlist (){

List list = new arraylist (); // list of program channel buttons

List. Add (New cartonprogrambutton (); // cartoon channel button

List. Add (New tvplanprogrambutton (); // TV channel button

List. Add (New footprogrambutton (); // football track button

Return list;

}

}

4. Compile the test class:

Public class testcommand {

Public static void main (string [] ARGs ){

List list = programpan. programlist (); // obtain the program dashboard button

For (iterator it = List. iterator (); it. hasnext ();)

(Remotecontrolbutton) it. Next (). selectprogram (); // select the corresponding program in the program dashboard

}

}

5. Description:

A: To put it bluntly, you can select commands and then execute the corresponding actions.

B: Command is a typical mode of encapsulation of behaviors. In this example, we use the programpan encapsulation class to watch TV programs.

C: The command mode and facade mode seem to be similar. All are accessed through encapsulation classes. I am also confused about how to differentiate.
D: In the command mode, the collection object container class is used to put some other classes in it to implement a collective operation for encapsulation. In the facade mode, operations of a specific function are put together to use a unified and external interface, such as encapsulating database operations and sending emails.
6. Thank you for the following:
I would like to thank changlich for their explanation of the difference between the command mode and the facade mode. I would like to add this explanation to the description, hoping to help you. Thanks again for your support.

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.