[JAVA] Command Pattern

Command Pattern Create a class to save the class instance (command.class) and treat the method of the saved class instance like the command.class method.

On this page -Simple Command pattern -Addition of a function to display the number of method executions I will describe about

Design Pattarm MENU

Simple Command pattern

It has the following configuration

class Explanation
command.interface Make the registered instance common
samCommand1.class Implement command and have one method
samCommand2.class Implement command and have one method
commandPool.class Save instances of samCommand1 and samCommand2
user(Main.class) Command.Check the operation of class
  • User Includes the meaning that other developers use this pattern

command.interface


interface command{
  void s();
}

samCommand1.class


class samCommand1 implements command{
  public void s(){System.out.println("command1");}
}

samCommand2.class


class samCommand2 implements command{
  public void s(){System.out.println("command2");}
}

commandPool.class


class commandPool{
  command[] pool=new command[2];  // command.Stores a class instance that implements interface
  void set(int key,command com){
    if(pool[key]==null){
       pool[key]=com;
    }
  }
  void exec(int key){
    pool[key].s();
  }
}

user(Main.class)


public static void main(String[] args){
  samCommand1 sc1 = new samCommand1();
  samCommand2 sc2 = new samCommand2();
  commandPool cop = new commandPool();
  cop.set(0,sc1);
  cop.set(1,sc2);
  cop.exec(1);
}}

Addition of function to display method execution count

Modify commandPool.class to below

commandPool.class


class commandPool{
  command[] pool = new command[4];
  int[] count = new int[2];
  void  set(int key,command com){
    if(pool [key]==null){
       pool [key]=com;
       count[key]=0;
    }
  }
  void exec(int key){
    pool [key].s();
    count[key]++;
    System.out.println(count[key]+"Time");
  }
}

Recommended Posts

Command Pattern
Design pattern ~ Command ~
Java Lambda Command Pattern
Prototype pattern
Memento Pattern
Mediator pattern
Composite pattern
Observer Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Docker command
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Command docker-compose
Bridge pattern
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Memorandum docker command
Design pattern ~ Visitor ~
Java design pattern
Docker command list
java callback pattern
Heroku command summary
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Docker command memo
docker basic command
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Docker command summary
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Docker-compose command memo
nginx start command