. Net: A Brief Introduction to the design mode (command mode)

Source: Internet
Author: User

Continue LearningDesign Pattern SeriesArticle.

This article focuses on the command mode. In fact, the command mode is also easy to understand. It does not use any advanced technology or complex abstraction. We only need to have a general prototype in our mind. When we encounter similar problems, we can solve them in a clever way. Most of our applications are engaged in some models, frameworks, ideas, and so on. Unlike low-layer development, most of their research may be about technical implementation, we are learning the implementation methodology. Application Layer development is limited in technical complexity and complicated in the implementation of combination technology. So there are some. net architects are really amazing. They can expect any problems long ago. They have a strong architecture idea, only by understanding design patterns, architecture design ideas, agility, limits, and so on can we design a very beautiful development architecture.[Wang qingpei has all rights reserved. For more information, please sign it.]

[Net architect is also one of our netProgramMembers dream of belonging. Let's work toward this goal. Come on!]

Command mode: encapsulate a call as a command object and call it through the command object. This effectively eliminates the coupling between the caller and the called.

This is my understanding of the command mode. There are many definitions and different focuses. Just understand it. We introduced the problem and learned how to "digest and absorb" with the problem ".

Nowadays, mobile terminals are becoming more and more popular. iPad, iPhone, and home devices are a trend of computer development and a major improvement in the quality of human life, everything on the computer is information, digital, and scientific. You don't have to go out to buy things. You don't have to go home to automatically open the water heater and boil water. You don't have to eat it. It seems that we are right to choose software development. At least we can improve our living environment.

We can improve our home environment, use full digitalization, and use a mobile terminal to control all the electronic devices in the home, such as microwave ovens, water heaters, televisions, and refrigerators. We can control it as long as it is a smart device.

[Software is a hardware programming, using software to write hardware processing logic]

We assume that this terminal device is called the Home Device Control Center. We need to develop such a device to control all these household appliances, but these household electronics are various, whether in terms of shape or internal implementation, it is not easy to use this terminal for unified control. This terminal is not dead. household electronics may be damaged over time. When we replace new household appliances, our terminals must work the same way, you only need a simple configuration to control new appliances.[Wang qingpei has all rights reserved. For more information, please sign it.]

Therefore, two problems are introduced here. The first is that the control terminal cannot be changed with the change of household appliances. The second is that it is impossible for each household appliance to be manufactured according to the regulations of the terminal. We need a solution to this conflict. We can use the "command mode" to solve this conflict.

Let's take a look at the general flowchart of a command mode.

1:

In this case, the caller and the called do not need to be changed. You only need to add a command object between the two to convey the command that the caller directs.

The home terminal may have multiple buttons, and each button controls different appliances. We just need to bring the principle into the terminal.

2:

In this way, you can use a remote control to control any electrical device. Let's take a look.CodeOf course, there are many implementation methods, each of which has its own advantages and disadvantages. This article focuses on getting started.

TV code:

 
Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // TV // </Summary> public class television {// <summary> // The mode is disabled /// </Summary> bool isopen = false; /// <summary> /// switch off, open /// </Summary> Public String switch () {If (isopen) {isopen = false; return "turn on TV" ;}else {isopen = true; Return "turn on TV ";}}}}

Code of the TV command object:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // TV command object /// </Summary> public class teleisioncommand: commandinterface {/// <summary> /// TV Object // </Summary> Private Television TV = new television (); # region commandinterface // <summary> /// execute the command // </Summary> Public String execute () {return TV. switch () ;}# endregion }}

Command interface code:

 
Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // command interface // </Summary> Public interface commandinterface {// <summary> // execute the command /Summary> string execute ();}}

Control center code:

 
Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // Control Center // </Summary> Public static class controlcontent {Private Static commandinterface command; public static void setcontrolobjectcommand (commandinterface cobject) {command = cobject;} public static string executecontrolobject () {return command. execute ();}}}

Simulation code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {class program {static void main (string [] ARGs) {teleisioncommand tvcommand = new teleisioncommand (); controlcontent. setcontrolobjectcommand (tvcommand); console. writeline (controlcontent. executecontrolobject (); console. readline ();}}}

Conclusion: The command mode is over. The general theory is to set up a command object in the middle to separate the caller from the called to solve the coupling between the two.

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.