Command mode for Java design mode

Source: Internet
Author: User

There will always be a certain stratum in school, in life and in society, although many of us cannot recognize the existence of stratum. The word order is also born in the hierarchy. The parents ordered the children, the teacher ordered the students, and the leader ordered little bellow. These are things that exist in our lives and believe that this model will be easier to learn. This time we'll give an example of a leader ordering an employee to learn the command pattern.

At work the leader often calls you to do something, but the leader doesn't care what you do, just know what you're doing. How to do is your own things, do not be good to wait for overtime (joking, generally will say that their own voluntary, haha), do good credit is not good for you, haha seems a little dark. General leaders will be called small leaders to do things, small leadership is called Little Lou Lou. And then, you know, the credit. Not much to say, said more are tears. The code for the starting code.

Since it is imperative to have an order, define a command first.

 PackageCom.roc.command;/*** Command *@authorLIAOWP **/ Public Abstract classCommand {protectedCodeworkman codeworkman=NewCodeworkman ();//Define a programmer             /*** Execute Command*/      Public Abstract voidexecute ();}

Or need a little Lou Lou, referred to as staff, can write documents, can write code, but also need to be able to work overtime.

 PackageCom.roc.command; Public Abstract classWorkMan {/*** Write Documents*/      Public Abstract voidwritedocument (); /*** Work*/      Public Abstract voidWork (); /*** Overtime*/      Public Abstract voidoverwork ();}

Need specific personnel, or how to embody the value of leadership. Let's get a programmer.

 PackageCom.roc.command;/*** Programmer *@authorLIAOWP **/ Public classCodeworkmanextendsworkman{ Public voidwritedocument () {System.out.println ("Write the document ..... "); }     Public voidWork () {System.out.println ("Tell you to go to work, nonsense ... "); }     Public voidoverwork () {System.out.println ("Lead a sentence of overtime, overtime ...." "); }}

Need a small leader, or no one to manage programmers, then create a leader.

 PackageCom.roc.command;/*** Small leaders, general leaders will not directly give the staff orders. * @authorLIAOWP **/ Public classLittlelead {PrivateCommand command;  PublicCommand GetCommand () {returncommand; }     Public voidsetcommand (Command command) { This. Command =command; }           Public voidAction () {//Small leaders execute orders, and small leaders call on employees to performCommand.Execute (); }}

Need specific execution orders, or do not know what to do. Let's do a command.

 PackageCom.roc.command;/*** Programmers Execute commands *@authorLIAOWP **/ Public classCodecommandextendscommand{ Public voidExecute () {Super. Codeworkman.work ();//work        Super. Codeworkman.overwork ();//Overtime        Super. Codeworkman.writedocument ();//Write a document    }}

Such a good scene needs to have a scene simulation, the client

 PackageCom.roc.command;/*** Simulation of large-leader Command mode *@authorLIAOWP **/ Public classClient { Public Static voidMain (string[] args) {//The leader sends orders, does not care about your work, sends the order to be possible. What the hell are you doing?Command command=NewCodecommand ();//CommandLittlelead lead=NewLittlelead ();//Small Leaderslead.setcommand (command);    Lead.action (); }}

The whole command pattern is that the leader commands you to do things, you don't have to worry about what you do, you send out an order, the order needs to be thought, the command is made, and the leader thinks of the order, the leader orders, the programmer is the one doing the thing. Command mode, sure enough a bit of it.

Command mode for Java design mode

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.