Wednesday, March 22, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

How to use the Command pattern in Java

learningcode_x1mckf by learningcode_x1mckf
January 19, 2023
in Java
0
How to use the Command pattern in Java
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


One among our largest challenges as software program builders is organizing our code in order that it’s simpler to increase and keep. The Command sample helps us try this by encapsulating all the info required to carry out an motion right into a single Command object.

You would possibly acknowledge the Command sample as a result of we use it on a regular basis in our on a regular basis lives. instance is utilizing a distant management gadget to activate a tv, change channels, flip up the amount, and so forth. Each one in all these actions is encapsulated within the distant management gadget.

One thing else to notice about all of those actions is that they’re reversible: you’ll be able to activate the TV, and you may also flip it off. Moreover, a number of the actions should be executed so as: you will need to activate the TV earlier than you’ll be able to flip up the amount.

On this Java code problem, you will be taught concerning the Command design sample and see a number of examples of the sample in apply. I may even talk about how the Command sample implements two core ideas of the SOLID mannequin. The 2 ideas are the single-responsibility precept, which states {that a} class ought to have just one job, and the open-closed precept, which states that objects or entities must be open for extension however closed for modification.

What’s the Command sample?

The Command sample is without doubt one of the 23 design patterns launched with the Gang of Four design patterns. Command is a behavioral design sample, that means that it goals to execute an motion in a selected code sample.

When it was first launched, the Command sample was generally defined as callbacks for Java. Whereas it began out as an object-oriented design sample, Java 8 launched lambda expressions, permitting for an object-functional implementation of the Command sample. This text consists of an instance utilizing a lambda expression within the Command sample.

As with all design patterns, it is essential to know when to use the Command sample, and when one other sample is perhaps higher. Utilizing the unsuitable design sample for a use case could make your code extra sophisticated, not much less.

The Command sample within the JDK

We will discover many examples of the Command sample within the Java Growth Package, and within the Java ecosystem. One standard instance is utilizing the Runnable purposeful interface with the Thread class. One other is dealing with occasions with an ActionListener. Let’s discover each of those examples.

The Command sample with Thread and Runnable

Runnable is an interface that features the run() technique. The next code snippet reveals the run() technique’s signature. As you’ll be able to see, it’s potential to move a command within the run() technique:


@FunctionalInterface
public interface Runnable 
   public summary void run();

Thread is the most-used class that receives a Runnable. Let’s have a look at how we are able to move a command to the Thread class:


Runnable command = () -> System.out.println("Executing command!");
Thread thread = new Thread(command);              // Setting command
thread.begin();                                                         

On this code, we implement the command habits within the run() technique with a lambda expression. As an alternative of the lambda, we might use an nameless interior class, which is an unnamed class that implements Runnable and the run() technique. However that method would make the code extra verbose. Utilizing the lambda is extra concise and simpler to learn.

We then move the command to the Thread class. Lastly, we execute the command by invoking the begin() technique.

Here is the output we are able to anticipate from this code:

You might also like

Oracle aims to sustain Java's 27-year franchise with version 20 rollout – SiliconANGLE News

Oracle releases Java 20 – iTWire

Oracle Aims To Sustain Java's 27-Year Franchise With v20 Rollout – Slashdot


Executing command!

The Command sample with ActionListener

One other good instance within the JDK is the ActionListener interface. I do know it is an older interface, however it’s appropriate for instance.

Within the following code, we create a JFrame and a JButton. We then set the motion within the button by invoking the addActionListener() technique. On this case, we’ll simply change the textual content from “Click on me” to “Clicked.” Then, we’ll add the button to the body, and present the body with the button:


JFrame body = new JFrame();
    
JButton button = new JButton("Click on Me");
button.addActionListener(e -> button.setText("Clicked!"));    // Command implementation
    
body.add(button);
body.pack();
body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
body.setVisible(true);

Determine 1 reveals the outcomes of this code after the button is clicked.

Command pattern with ActionListener.IDG

Determine 1. ActionListener in motion.

Drive my bike! The Command sample in a Automobile interface

Now that you have seen examples of the Command sample within the JDK, let’s create our personal. First, check out the category diagram in Determine 2.

Command pattern for a Vehicle interface.IDG

Determine 2. A diagram of the Command sample for a Automobile interface.

There are three components to the diagram, which I am going to clarify.

Command

The inspiration class for the Command sample is the Command interface. We use this interface anytime we need to execute or revert a command:


public interface Command 
  
  void execute();

  void revert();


Receiver

Subsequent, we have to create the category that has the habits to execute the command. We begin with the Automobile interface, then create the Bike and Truck lessons to implement it:


public interface Automobile 

  void begin();

  void cease();

  void speed up();



public class Bike implements Automobile 

  @Override
  public void begin() 
    System.out.println("Beginning bike...");
  

  @Override
  public void cease() 
    System.out.println("Stopping bike...");
  

  @Override
  public void speed up() 
    System.out.println("Accelerating bike...");
  



public class Truck implements Automobile 

  @Override
  public void begin() 
    System.out.println("Beginning truck...");
  

  @Override
  public void cease() 
    System.out.println("Stopping truck...");
  

  @Override
  public void speed up() 
    System.out.println("Accelerating truck...");
  

  @Override
  public void decelerate() 
    System.out.println("Decelerating truck...");
  


Additionally discover that the Automobile interface makes the code extra versatile and simpler to alter: we might simply add one other car comparable to Automotive that implements the Automobile interface. This a part of the Command sample is a superb instance of the open-closed SOLID precept. (Keep in mind that this precept states that objects or entities must be extensible.)

Invoker

Now, we’ve the Bike and Truck habits however we’d like a category to execute it. In our case, this class would be the GhostRider. GhostRider will drive the Bike and Truck lessons.

GhostRider receives the command within the constructor and invokes the execute() technique from the command into the takeAction() and revertAction() strategies:


public class GhostRider 
  Command command;

  public GhostRider(Command command)
    this.command = command;
  

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

  public void takeAction()
    command.execute();
  

  public void revertAction() 
    command.revert();
  

Implementing instructions within the Command sample

Now, let’s create the StartMotorcycle, AccelerateMotorcycle, and StartAllVehicles instructions. Every command implements the Command interface and receives Automobile within the constructor. Then, it invokes the tactic that corresponds to every command class from Automobile into the execute() technique:


public class StartMotorcycle implements Command 

  Automobile car;

  public StartMotorcycle(Automobile car) 
    this.car = car;
  

  public void execute() 
    car.begin();
  

  @Override
  public void revert() 
    car.cease();
  



public class AccelerateMotorcycle implements Command 

  Automobile car;

  public AccelerateMotorcycle(Automobile car)
    this.car = car;
  

  public void execute() 
    car.speed up();
  

  @Override
  public void revert() 
    car.decelerate();
  




import java.util.Record;

public class StartAllVehicles implements Command 

  Record<Automobile> autos;

  public StartAllVehicles(Record<Automobile> autos) 
    this.autos = autos;
  

  public void execute() 
    autos.forEach(car -> car.begin());
  

  @Override
  public void revert() 
    autos.forEach(car -> car.cease());
  


Run the instructions

It is time to run our instructions! For this, we first instantiate the Bike class that has the Command habits, then move it into every Command implementation.

Discover that we’re additionally utilizing the StartAllVehicles command to start out (and cease) a number of autos directly.

Then, we instantiate the GhostRider class that may execute every command. Lastly, we invoke the takeAction() and revertAction() strategies:


public class RideVehicle 

  public static void foremost(String[] args) 
    Automobile bike = new Bike();

    StartMotorcycle startCommand = new StartMotorcycle(bike);
    GhostRider ghostRider = new GhostRider(startCommand);
    ghostRider.takeAction();

    AccelerateMotorcycle accelerateCommand = new AccelerateMotorcycle(bike);
    ghostRider.setCommand(accelerateCommand);
    ghostRider.takeAction();
    ghostRider.revertAction();

    Automobile truck = new Truck();
    Record<Automobile> autos = Record.of(bike, truck);
    StartAllVehicles startAllVehicles = new StartAllVehicles(autos);
    startAllVehicles.execute();
    startAllVehicles.revert();
  


Right here is the output from this code:


Beginning bike...
Accelerating bike...
Decelerating bike...
Beginning bike...
Beginning truck...
Stopping bike...
Stopping truck…

When to make use of the Command sample

A vital rule for design patterns is to know when to make use of them. Irrespective of how nice a sample is, implementing it for the unsuitable use case will make your code a lot worse. Listed here are some pointers for utilizing the Command sample:

  1. You could have a number of instructions that must be applied individually based mostly on the SOLID ideas of single-responsibility and open-closed design.
  2. It’s essential create reversible instructions, comparable to including and eradicating an merchandise from a procuring cart.
  3. You want to have the ability to create logs every time a command is executed. Every command within the Command sample is encapsulated, so creating logs is simple.
  4. You want to have the ability to execute a number of instructions directly. You possibly can simply add a Queue, Record, or Set right into a command’s implementation and execute them.

What to recollect concerning the Command sample

To summarize, keep in mind the next concerning the Command sample:

  • It applies the SOLID ideas of single-responsibility and open-closed design.
  • It encapsulates and decouples the habits of instructions, which makes your code extra extensible.
  • It is used within the JDK with the Thread class and Runnable and ActionListener interfaces.
  • It encapsulates the habits of instructions inside a single Command implementation.
  • It enables you to execute and revert single instructions.
  • It enables you to execute and revert a number of instructions collectively.

Copyright © 2022 IDG Communications, Inc.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Oracle aims to sustain Java's 27-year franchise with version 20 rollout – SiliconANGLE News

by learningcode_x1mckf
March 22, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle aims to sustain Java's 27-year franchise with version 20 rollout  SiliconANGLE Information Source link

Read more

Oracle releases Java 20 – iTWire

by learningcode_x1mckf
March 22, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle releases Java 20  iTWire Source link

Read more

Oracle Aims To Sustain Java's 27-Year Franchise With v20 Rollout – Slashdot

by learningcode_x1mckf
March 22, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle Aims To Sustain Java's 27-Year Franchise With v20 Rollout  Slashdot Source link

Read more

OpenJDK Java 20 Released With Latest Vector API, Scoped Values – Phoronix

by learningcode_x1mckf
March 21, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

OpenJDK Java 20 Released With Latest Vector API, Scoped Values  Phoronix Source link

Read more

Accelerating The Digital Transformation in West Java, Digiasia Bios … – PR Newswire

by learningcode_x1mckf
March 21, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Accelerating The Digital Transformation in West Java, Digiasia Bios ...  PR Newswire Source link

Read more
Next Post
Senior Software Developer – C# / C++ / Programming / Windows (m/w/d) in Berlin

Senior Software Developer - C# / C++ / Programming / Windows (m/w/d) in Berlin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Java Software Developer and Cloud Specialist – R1401

Java Software Developer and Cloud Specialist – R1401

December 23, 2022
Introducing – Swift cheatsheet – The.Swift.Dev.

Introducing – Swift cheatsheet – The.Swift.Dev.

November 10, 2022
Swift enum all values – The.Swift.Dev.

Swift enum all values – The.Swift.Dev.

October 11, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Introducing Carbon, Google's Experimental Successor for C++ – MUO – MakeUseOf
  • Oracle aims to sustain Java's 27-year franchise with version 20 rollout – SiliconANGLE News
  • How to learn JavaScript? – JavaScript – SitePoint

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?