Java design pattern-from the [Controller in a plane-hitting game] analysis Command pattern

Source: Internet
Author: User

First, let me speak a few times. Why do we emphasize the design pattern in the software design process? Adding a design pattern to the software will indeed increase Code complexity, but its benefits are endless. It makes the software easier to expand without changing the source code. Decoupling is a key word in the design model. For example, if an object obj is called. in method (), we want obj to be a base class or an interface, rather than a specific implementation class. In this way, decoupling is implemented.

Next, go to the topic.

Suppose I am playing a plane-hitting game, and I am writing a controller for the plane now. The aircraft's controller can operate on the aircraft's missile and bomb attacks and move in four directions. I will first give a piece of Java code for "command mode", and then explain what is "command mode.

The Java code is as follows:

Interface FighterCommand {void execute () ;}// receiver class Fighter {public void missile () {System. out. println ("attack with missiles! ");} Public void bomb () {System. out. println (" bomb attack! ");} Public void move (int direction) {switch (direction) {case 1: System. out. println (" move up! "); Break; case 2: System. out. println (" Move down! "); Break; case 3: System. out. println (" Move to the left! "); Break; case 4: System. out. println (" Move to the right! "); Break; default: System. out. println (" Do not move! "); Break ;}} class MissleCommand implements FighterCommand {Fighter fighter; public MissleCommand (Fighter fighter) {this. fighter = fighter;} public void execute () {fighter. missile () ;}} class BombCommand implements FighterCommand {Fighter fighter; public BombCommand (Fighter fighter) {this. fighter = fighter;} public void execute () {fighter. bomb () ;}} class MoveCommand implements FighterCommand {Fighter fighter; int direction; public MoveCommand (Fighter fighter, int direction) {this. fighter = fighter; this. direction = direction;} public void execute () {fighter. move (direction) ;}/// requester class Controller {private fig?command cmdMissile; private fig?command =bomb; private fig?command =moveleft; private fig?command =moveright; public Controller (fig?command missile, fig=command bomb, fig1_command left, fig1_command right) {cmdMissile = missile; required bomb = bomb; required moveleft = left; required moveright = right;} public void missile () {cmdMissile.exe cute ();} public void bomb () {export bomb.exe cute ();} public void moveLeft () {export moveleft.exe cute ();} public void moveRight () {export moveright.exe cute ();}} class Command {public static void main (String [] args) {Fighter fighter1 = new Fighter (); fig1_command cmdMissile = new MissleCommand (fig1_1); fig1_command empty bomb = new BombCommand (fig1_1 ); fig?command =moveleft = new MoveCommand (fig1_1, 3); fig?command =moveright = new MoveCommand (fig1_1, 4); Controller player1 = new Controller (average, minimum bomb, minimum moveleft, minimum moveright ); player1.bomb (); player1.missile (); player1.moveLeft (); player1.moveRight ();}}

The program running result is as follows:

Attack with a bomb!

Attack with missiles!

Move to left!

Move to the right

Please carefully understand the purpose of the above Code. The command mode consists of the command receiver (Fighter), Command Behavior (FighterCommand Interface), and command requestor (Controller. The intention is to encapsulate the request as an object (derived class of figdomaincommand), so that you can parameterize the customer with different requests; queue requests, record request logs, and revoke requests.

As shown in the code above, we have removed three different classes of aircraft behavior (left and right movement; launching missiles; launching bombs, the three categories contain references to the Fighter of a Fighter. As a receiving Class of an action, a Fighter can implement these actions. These actions are aggregated in the Controller class. The Controller class sends the request information, and the request passes through the figworker command, which is finally received by the Fighter class, and can know what actions should be performed.

Then I wondered why I had to create a Controller class. Why didn't I directly call methods such as fig1_1.bomb () and fig1_1.missile? The reason is that the two words are decoupled. As mentioned in the beginning of this article, when you call obj. in method (), we want obj to be a base class object or an interface object, because it can be applied to a class of methods rather than a specific type of methods. In this example, the advantage of using the command mode is that we decouple the Fighter behavior from its own object. Assume that one day, we want some changes to the Fighter bomb () method. For example, we need to call the missile method before dropping a bomb. If we do not need the command mode, we need to call the Fighter method at all times. fighter is called once before the bomb () method. missile (), How troublesome it is! If the command mode is used, we can create a new figworker command, write the execute method to call missile () and then call bomb, and finally modify the constructor parameter passed in by the Controller class. The purpose of the design model is to make the software easier to expand.

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.