Java design mode (6) command mode

Source: Internet
Author: User

[Command mode] encapsulates a request as an object to parameterize other objects using different requests, queues, or logs. The command mode also supports unrecoverable operations. 1. Define the command interface [html] package com. pattern. command; public interface Command {public void execute () ;}2, interface implementation class [html] package com. pattern. command; public class LightOnCommand implements Command {Light light Light; public LightOnCommand (light Light light) {this. light = light;}/*** @ see com. pattern. command. command # execute () */@ Override public void execute () {light. on () ;}} 3, the specific executor of the command [html] package com.pattern.com Mand; public class Light {public void on () {System. out. println ("turn on the light! ") ;}} 4, controller class [html] package com. pattern. command; public class SimpleRemoteControl {Command slot; public SimpleRemoteControl () {} public void setCommand (Command command) {slot = command;} public void buttonWasPressed () {slot.exe cute () ;}} 5, test class [html] package com. pattern. command; public class RemoteControlTest {public static void main (String [] args) {SimpleRemoteControl remote = new SimpleRemote Control (); Light light = new Light (); LightOnCommand lightOn = new LightOnCommand (light); remote. setCommand (lightOn); remote. buttonWasPressed () ;}} output: turn on the light!

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.