티스토리 뷰

Java

Command Pattern - serial running

techbard 2016.07.18 13:22

Command_serial_running.zip



# ICommand.java


public interface ICommand {

    public abstract void exeute();

}



# CommandManager.java


import java.util.ArrayList;


public class CommandManager implements ICommand {

    private ArrayList<ICommand> commands = new ArrayList<>();


    @Override

    public void exeute() {

for (ICommand command : commands)

   command.exeute();

    }


    public boolean add(ICommand command) {

if (command != this) {

   commands.add(command);

   return true;

}

return false;

    }


    public void remove(ICommand command) {

if (commands.size() != 0)

   commands.remove(command);

    }


    public void clear() {

commands.clear();

    }

}



# Command01.java


public class Command01 implements ICommand {


    @Override

    public void exeute() {

System.out.println("This is a command 01.");

    }

}



# Command02.java


public class Command02 implements ICommand {


    @Override

    public void exeute() {

System.out.println("This is a command 02.");

    }

}



# Main.java


public class Main {

    public static void main(String[] args) {

CommandManager commandManager = new CommandManager();

ICommand command01 = new Command01();

ICommand command02 = new Command02();

commandManager.add(command01);

commandManager.add(command02);

commandManager.exeute();

commandManager.remove(command02);

System.out.println("==============================");

commandManager.add(new ICommand() {

   

   @Override

   public void exeute() {

System.out.println("This is a new command that now added.");

   }

});

commandManager.exeute();

    }

}



# 결과


This is a command 01.

This is a command 02.

==============================

This is a command 01.

This is a new command that now added.


댓글
댓글쓰기 폼
공지사항
Total
394,934
Today
5
Yesterday
28
«   2018/11   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
글 보관함