Command mode-java Implementation (example of general instructions)

Source: Internet
Author: User
Package ParttenDesign; // Command interface of the general public interface Command {void defend (); void attack ();}

The command action the General wants the soldiers to send

Package ParttenDesign; public class Commander implements Command {Command command Command; public Commander (command Command) {this. command = command ;}@ Overridepublic void attack () {System. out. println ("commander sends attack command"); command. attack () ;}@ Overridepublic void defend () {System. out. println ("the commander issued a defensive command"); command. defend ();}}

The soldiers receive the commands from the generals.

Package ParttenDesign; public class Privates implements Command {@ Overridepublic void attack () {System. out. println ("soldiers receive attack commands and launch attacks"); System. out. println ("-------------------------------------"); System. out. println ("") ;}@ Overridepublic void defend () {System. out. println ("soldiers receive defensive commands, retreat and defend"); System. out. println ("-------------------------------------"); System. out. println ("");}}

Test code:

Package ParttenDesign; public class MyMain {/*** command mode: * (Action sender) the commander sends (defense, attack) (command) commands by calling (Action transmission, (Action recipient) the soldier accepts the Command and then performs the corresponding action * @ param args */public static void main (String [] args) {Command privates = new Privates (); command commander = new Commander (privates); commander. attack (); commander. defend ();}}

Related Article

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.