Loading, please wait ...
Open its course - Design Patterns
Command By Mukhtiar Zamin
Summary

Summary

3
Teachers with
Mukhtiar Zamin
229
Followers
For Learning
All
Visibility
Security Status
3
Contributions
By Teachers
Notes
  1. Students are auto-evaluated against related topics to ensure they learned it.

In this lecture we will learn about the Command design pattern.

Watch the video within interaction mode which is only visible when you are logged in.

Command Pattern Remote control light On/Off Example for All, By Ali hassan

HTML 5 Boilerplate

RemoteControl.java

    
   //Invoker

public class RemoteControl {

    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void pressButton() {
        command.execute();
    }

}
    

Command.java

    
   //Command Interface

public interface Command {

    public void execute();
}
    

LightOffCommand.java

    
   //Concrete Command

public class LightOffCommand implements Command {

    // reference to the light
    private Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.switchOff();
        System.out.println("Light is off");
    }

}
    

LightOnCommand.java

    
  //Concrete Command

public class LightOnCommand implements Command {

    // reference to the light
    private Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.switchOn();
        System.out.println("Light is on");
    }

}
    

Light.java

    
   //Receiver

public class Light {

    private boolean isOn;

    public void switchOn() {
        isOn = true;
    }

    public void switchOff() {
        isOn = false;
    }

}

    

Demo.java

    
   public class Demo {

    public static void main(String[] args) {

        RemoteControl control = new RemoteControl();
        Light light = new Light();
        Command lightsOn = new LightOnCommand(light);
        Command lightsOff = new LightOffCommand(light);

        // switch on
        control.setCommand(lightsOn);
        control.pressButton();

        // switch off
        control.setCommand(lightsOff);
        control.pressButton();

    }

}

    

Lab for All, By Mukhtiar Zamin


Watch the following examples and then perform the respective lab tasks:
Example 1

 

Example 2

Code: Click on refactoring.guru  

Lab Tasks for All, By Mukhtiar Zamin

For example 1 

  1. Call and debug the WriteFileCommand in main method. 
  2. Add a logic via a bool variable such that if the execute is called the boolean variable is set to true and the undo action set it to false. On exiting the application if the value is true then exit after confirmation.
For example 2
  1. Implement the replace command such that a selected text gets replaced with a value already in the clipboard  with label as Ctr+H. 

×

Command Evaluation

To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.

  • CAREFULLY READ THE FOLLOWING INSTRUCTIONS!
  • Make sure you have learned this lecture along with its topics and tutorials.
  • On first evaluation you get 100% marks on correct answer, then on 2nd you get 95% marks and so on.
  • Answer the questions with a gap NOT MORE THAN A MINUTE, Otherwise it will re-start.

I have read above instrucitons and ready for evaluation.
Your's
Status
Not Registered
Teacher
Mukhtiar Zamin
Alert
Your performance monitoring will start when you register in a class of this course.

Questions on

Contact Us

support@subexpert.com
Write to Us View Help
Subject Expert Logo

Subject Expert

Learn, Evaluate and Optimize

Follow Us
Facebook Switch Display Mode Enable Translation
© 2024 - Subject Expert