[Design Pattern] Command Bus pattern ์ด๋ž€?

2022. 8. 17. 02:51ยทProgrammings/Design Pattern
๋ชฉ์ฐจ
  1. Given problem
  2. Solution with Command Bus pattern
  3. Command class
  4. CommandHandler class
  5. CommandBus class
  6. Source code
  7. Benefits and Drawbacks
  8. Some problems with Command Bus pattern
  9. The relationship with other patterns
  10. Wrapping up

Given problem

โ€ŽCommandBus ํŒจํ„ด์˜ ๋ฌธ์ œ๋Š” CQRS ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์˜ ์ž์‹ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์œ ์ผํ•œ ์งˆ๋ฌธ์€ ํ•ญ์ƒ ์šฐ๋ฆฌ ๋จธ๋ฆฌ ์†์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค : ์šฐ๋ฆฌ ์‹œ์Šคํ…œ์—์„œ ๋ช…๋ น๊ณผ ์ฟผ๋ฆฌ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?โ€Ž

โ€Ž์šฐ๋ฆฌ ์‹œ์Šคํ…œ์ด ๋” ๋ณต์žก ํ•ด์ง€๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๋ด‡ํ‹€๋„ฅ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ž ๊ธˆ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋™์‹œ์„ฑ ์•ก์„ธ์Šค์˜ ์ผ๋ถ€ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ๊ฒƒ์€ ๋˜ํ•œ ์„ฑ๋Šฅ์— ๊ด€ํ•œ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.โ€Ž

โ€Ž๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ช…๋ น ๋ฐ ์ฟผ๋ฆฌ ๊ฐœ๋…์„ CQRS ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์˜ ๋‘ ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.โ€Ž

โ€Ž๊ทธ๋ ‡๋‹ค๋ฉด ๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๊นŒ?โ€Ž

 

Solution with Command Bus pattern

โ€Ž๋‹ค์Œ์€ ๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด์— ๋Œ€ํ•œ ๋‹ค์ด์–ด๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.โ€Ž

โ€Ž์ด ํŒจํ„ด์—๋Š” ์„ธ ๊ฐ€์ง€ ํด๋ž˜์Šค๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.โ€Ž

 

Command class

โ€Ž๋ช…๋ น ํด๋ž˜์Šค๋Š” ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ ๋œ ํด๋ž˜์Šค ์ผ๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐœ์ฒด ํŒจํ„ด๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.โ€Ž

โ€Ž๋ช…๋ น๊ณผ ๋ช…๋ น ์ฒ˜๋ฆฌ๊ธฐ ๊ฐ„์˜ ๊ด€๊ณ„๋Š” ์ผ๋Œ€์ผ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ•˜๋‚˜์˜ ๋ช…๋ น์€ ํ•˜๋‚˜์˜ CommandHandler์— ์˜ํ•ด์„œ๋งŒ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.โ€Ž

โ€ŽCommand ํด๋ž˜์Šค์—์„œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ Command ๊ฐ์ฒด๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜์—†๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.โ€Ž

 

CommandHandler class

โ€ŽCommandHandler์˜ ์ฑ…์ž„์€ ํŠน์ • Command ๊ฐ์ฒด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์ ์ ˆํ•œ ๋„๋ฉ”์ธ ๋™์ž‘์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. CommandHandler๋Š” ๋„๋ฉ”์ธ ๋…ผ๋ฆฌ ์ž์ฒด๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค. ์ด ์ด์ƒ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋…ผ๋ฆฌ๋ฅผ ๋ž˜ํ•‘ํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.โ€Ž

โ€Ž๋„๋ฉ”์ธ ๋™์ž‘์€ AggregateRoot์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ CommandHandler ํด๋ž˜์Šค๋Š” ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ AggregateRoot๋ฅผ๋กœ๋“œํ•˜๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ AggregateRoot์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.โ€Ž

 

CommandBus class

โ€ŽCommand ๊ฐœ์ฒด๋ฅผ ๋ฐ›์€ ํ›„ CommandBus๋Š” ์ ์ ˆํ•œ CommandHandler๋กœ ๋ผ์šฐํŒ…ํ•ฉ๋‹ˆ๋‹ค.โ€Ž

โ€ŽCommandBus ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๋ ค๋ฉด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด ๋˜๋Š” ํ”„๋ก์‹œ ํŒจํ„ด์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด์˜ ๊ฒฝ์šฐ CommandBus์˜ ํ™•์žฅ์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ƒ˜ํ”Œ์ด ์žˆ์Šต๋‹ˆ๋‹ค.โ€Ž

  • โ€Ž๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜์—์„œ CommandBus๋ฅผ ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค.โ€Ž
  • โ€Ž๋กœ๊น…์œผ๋กœ CommandBus๋ฅผ ๋ž˜ํ•‘ํ•˜์—ฌ CommandHandler์˜ ์‹œ๊ฐ„์„ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.โ€Ž

โ€Žํ”„๋ก์‹œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ „์— ๊ถŒํ•œ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด CommandBus๋ฅผ ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค.โ€Ž

Source code

โ€Ž์ผ๋ฐ˜์ ์œผ๋กœ CommandBus์—์„œ HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ Command ์™€ CommandHandler ๊ฐ„์˜ ์—ฐ๊ฒฐ์„ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ Guice์˜ DI ์›์น™ ๋˜๋Š” IoC ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ™œ์šฉํ•˜์—ฌ Command ๋ฐ CommandHandler๋ฅผ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.โ€Ž

โ€Ž๋‹ค์Œ์€ ๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด์—์„œ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.โ€Ž

 

 

โ€Ž์ผ๋ฐ˜ ์ธํ„ฐํŽ˜์ด์Šค โ€Žโ€ŽICommand์™€โ€Žโ€Ž ํ•ด๋‹น ๊ตฌํ˜„์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.โ€Ž

 public interface ICommand<R> {
     // nothing to do
 }

 @Getter
 @AllArgsConstructor
 @NoArgsConstructor
 public class WithdrawMoneyCommand implements ICommand<Void> {
     private String username;
     private String amount;
     private String account;
 }

โ€Ž์ผ๋ฐ˜ ์ธํ„ฐํŽ˜์ด์Šค โ€Žโ€ŽICommandHandlerโ€Žโ€Ž ๋ฐ ํ•ด๋‹น ๊ตฌํ˜„์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.โ€Ž

 public interface ICommandHandler<C, R> {
     R handle(C command);
 }

 public class WithdrawCommandHandler implements ICommandHandler<WithdrawCommand, Void> {
     @Override
     public Void handle(WithdrawCommand command) {
         System.out.println(WithdrawCommandHandler.class.getName() + " handled.");
         return null;
     }
 }

โ€ŽGuiceโ€Žโ€Ž์—์„œ ๊ตฌ์ฒด์ ์ธ ๋ชจ๋“ˆ์„ ์ •์˜ํ•˜์—ฌ Command ๋ฐ โ€Žโ€ŽCommandHandlerโ€Žโ€Ž ํด๋ž˜์Šค๋ฅผ ๊ฒฐํ•ฉ โ€Žโ€Ž ํ•˜์‹ญ์‹œ์˜ค.โ€Ž

โ€ŽICommandHandlerโ€Ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ช…๋ น ์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— โ€Žโ€ŽMultibinderโ€Ž โ€Ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ โ€Žโ€ŽICommandHandlerโ€Ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ˆ˜๋ฝํ•ฉ๋‹ˆ๋‹ค.โ€Ž

 public class CommandHandlerModule extends AbstractModule {
     @Override
     protected void configure() {
         Multibinder<ICommandHandler> commandHandlerBinder = Multibinder.newSetBinder(binder(), ICommandHandler.class);
         commandHandlerBinder.addBinding().to(RegisterCommandHandler.class);

         // define the other derived class of ICommandHandler interface
     }
 }

 public class MainModule extends AbstractModule {
     @Override
     protected void configure() {
         this.install(new CommandHandlerModule());
         this.bind(ICommandBus.class).to(CommandBusImpl.class);
     }
 }

โ€ŽICommandBusโ€Žโ€Ž ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ๊ตฌํ˜„ ๋งŒ๋“ค๊ธฐโ€Ž.โ€Ž

 public class CommandBusImpl implements ICommandBus {
     private Set<ICommandHandler> commandHandlers;

     @Inject
     public CommandBusImpl(Set<ICommandHandler> commandHandlers) {
         this.commandHandlers = commandHandlers;
     }

     @Override
     public <C> void execute(C command) {
         return (ICommandHandler<C, Void>) findCommandHandler(command);
     }

     @Override
     public <C extends ICommand<R>, R> R execute(C command) {
         return (ICommandHandler<C, R>) findCommandHandler(command);
     }

     private <C> ICommandHandler<C, ?> findCommandHandler(C command) {
         Class<?> commandClazz = command.getClass();
         return this.commandHandlers.stream()
                                    .filter(handler -> this.canHandleCommand(handler.getClass(), commandClazz))
                                    .findFirst()
                                    .orElseThrow(() -> new RuntimeException("Do not handle " + commandClazz.getName()));
     }

     /**
      * check the parameters's type that is corresponding to the type of Command class
      *
      */
     private boolean canHandleCommand(Class<?> handlerClazz, Class<?> commandClazz) {
         Type[] genericInterfaces = handlerClazz.getGenericInterfaces();
         ParameterizedType handlerIntefaceType = null;

         for (Type type : genericInterfaces) {
             if (type instanceof ParameterizedType) {
                 handlerIntefaceType = (ParameterizedType) type;
                 break;
             }
         }

         Class<?> acceptableParameterClass = (Class<?>) handlerIntefaceType.getActualTypeArguments()[0];
         return acceptableParameterClass.equals(commandClazz);
     }
 }

โ€ŽCommandBusImplโ€Žโ€Ž ํด๋ž˜์Šค์˜ ์ •์˜ :โ€Ž

 public interface ICommandBus {
     <C> void execute(C command);

     <C extends ICommand<R>, R> R execute(C command);
 }

โ€Ž๊ทธ๋ž˜์„œ, ์šฐ๋ฆฌ๋Š” ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค :โ€Ž

 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.manhpd.bus.ICommandBus;
 import com.manhpd.command.WithdrawCommand;

 public class Application {
     public static void main(String[] args) {
         Injector injectorHandler = Guice.createInjector(new MainModule());
         ICommandBus commandBus = injectorHandler.getInstance(ICommandBus.class);

         WithdrawCommand command = new WithdrawCommand("John", "100000", "john");
         commandBus.execute(command);
     }
 }

 

Benefits and Drawbacks

  1. โ€Žํ˜œํƒโ€Ž
    • โ€Ž์ด ํŒจํ„ด์€ ๋„๋ฉ”์ธ ๊ธฐ๋ฐ˜ ๋””์ž์ธ ํŒจํ„ด์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ช…๋ น์„ ์ •์˜ํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์—๋งŒ ์ดˆ์ ์„ ๋งž์ถฅ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋…ผ๋ฆฌ๋Š” ๋‹ค๋ฅธ ๊ณ„์ธต์—์„œ ๋ˆ„์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.โ€Ž

      ์‚ฌ์šฉ์ž์™€ ๊ฐœ๋ฐœ์ž ๊ฐ„์˜ ์˜์‚ฌ ์†Œํ†ต์€ ์ •๋ง ์›ํ™œํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ๋“ค์€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ๋งํ•˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.โ€Ž
    • โ€Ž๋‹ค๋ฅธ ํŒจํ„ด์„ ํ™œ์šฉํ•˜๋ฉด CommandBus์˜ ๋™์ž‘์„ ํ™•์žฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.โ€Ž
    • โ€ŽCommandBus ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ Command ๊ฐ์ฒด๋ฅผ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      ๋น„๋™๊ธฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.โ€Žโ€Ž
      • โ€Ž๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์Šคํ…œ์˜ ์„ฑ๋Šฅ.โ€Ž
      • โ€ŽUX๋Š” ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์‘๋‹ต์ด ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.โ€Ž
  2. โ€Ž๋‹จ์ โ€Ž
    • โ€ŽCommandHandler ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์ฃผ์˜ ๊นŠ๊ฒŒ ์ƒ๊ฐํ•˜์ง€ ์•Š์œผ๋ฉด SRP๋ฅผ ์ค€์ˆ˜ํ•˜์ง€ ์•Š๋Š” CommandHandlers๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.โ€Ž
    • โ€Ž์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Command ์™€ CommandHandler ์‚ฌ์ด๋ฅผ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Ÿฐํƒ€์ž„์— ์‹œ์Šคํ…œ์˜ ์„ฑ๋Šฅ์—๋„ ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.โ€Ž

 

Some problems with Command Bus pattern

  1. โ€Ž๋•Œ๋กœ๋Š” CommandHandler ํด๋ž˜์Šค์—์„œ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ •์˜ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ž‘์—…์— ์ดˆ์ ์„ ๋งž์ถ”์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

    โ€Ž์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๊ฐ€ ์›น ์‚ฌ์ดํŠธ์— ๋กœ๊ทธ์ธํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์˜ ๊ธฐ๋ณธ ๋™์ž‘์€ ์‚ฌ์šฉ์ž ์ด๋ฆ„-์•”ํ˜ธ๋ฅผ ํฌํ•จํ•˜๋Š” ์ •๋ณด์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๊ณ  ์ด ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ์ด ์„ธ์…˜์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ˜„์žฌ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด ์ด๋ฉ”์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ณ„์ •์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.โ€Ž

    ๋”ฐ๋ผ์„œ ๋‘ ๋ฒˆ์งธ ์ž‘์—…์€ ํ˜„์žฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์ž ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.โ€Žโ€Ž

    โ€ŽCommandHandler์—์„œ ์—ฌ๋Ÿฌ ๋ณด์กฐ ์ž‘์—…์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ. ๊ทธ๊ฒƒ์€ ์šฐ๋ฆฌ์˜ CommandHandler ํด๋ž˜์Šค๋ฅผ ์˜ค์—ผ์‹œํ‚ค๊ณ  ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ์ถฉ์กฑ์‹œํ‚ค์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.โ€Ž

    ์ด ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์šฐ๋ฆฌ์˜ ํ•ด๊ฒฐ์ฑ…์€ ๊ฐ ๋ณด์กฐ ์ž‘์—…์— ๋Œ€ํ•œ ๊ธ€๋กœ๋ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ๋ฒ„์Šค ํŒจํ„ด์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.โ€Ž

    ๋˜ํ•œ ๊ธ€๋กœ๋ฒŒ ์ด๋ฒคํŠธ์™€ ๋„๋ฉ”์ธ ์ด๋ฒคํŠธ์˜ ์ฐจ์ด์ ์„ ์ธ์‹ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.โ€Ž
    1. ์™ธ๋ถ€ ํ–‰๋™์œผ๋กœ ์šฐ๋ฆฌ๋Š” ๊ธ€๋กœ๋ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.โ€Ž
    2. ๋„๋ฉ”์ธ ๋™์ž‘์œผ๋กœ ๋„๋ฉ”์ธ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ์•ผํ•ฉ๋‹ˆ๋‹ค.โ€Ž

The relationship with other patterns

  1. โ€Ž๋ช…๋ น ํŒจํ„ด ๋ฐ ๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ดโ€Ž
    • โ€Ž๋ช…๋ น ํŒจํ„ด์€ ๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด๊ณผ ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค.โ€Ž
    • โ€Ž๋ช…๋ น ํŒจํ„ด์€ ํ˜ธ์ถœ์ž์™€ ์ˆ˜์‹ ์ž ๊ฐ„์˜ ๋””์ปคํ”Œ๋ง๋˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ , Command ๊ฐ์ฒด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ˆจ๊น๋‹ˆ๋‹ค.โ€Ž
    • โ€Ž๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด์€ ์‚ฌ์šฉ ์‚ฌ๋ก€์˜ ์„ ์–ธ๊ณผ ํ•ด๋‹น ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ Command ๊ฐ์ฒด๋Š” ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฉ”์‹œ์ง€์ผ ๋ฟ์ž…๋‹ˆ๋‹ค.

      ๊ทธ๋ฆฌ๊ณ  CommandHandler๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.โ€Ž

      ๋”ฐ๋ผ์„œ ์‹ค์ œ๋กœ Command ๊ฐ์ฒด๋Š” CommandHandler์— ์ „๋‹ฌ ๋œ ๋ฉ”์‹œ์ง€ ์ผ๋ฟ์ž…๋‹ˆ๋‹ค.โ€Ž
  2. โ€Ž๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด ๋ฐ ๋ฉ”์‹œ์ง€ ๋ฒ„์Šค ํŒจํ„ดโ€Ž
    • โ€Ž๋ช…๋ น ๋ฒ„์Šค ํŒจํ„ด์€ ๋ฉ”์‹œ์ง€ ๋ฒ„์Šค ํŒจํ„ด์˜ ํŠน์ • ํŒจํ„ด์ž…๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€ ๋˜๋Š” ๋ช…๋ น ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ญ์ƒ ์ฒ˜๋ฆฌ๊ธฐ ๋˜๋Š” ๋Œ€์ƒ์„ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.โ€Ž

 

Wrapping up

  • โ€Ž์š”์ฆ˜์—๋Š” CommandBus ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌํ”Œ๋ ‰์…˜๊ณผ ์ฃผ์„์„ ์‚ฌ์šฉํ•˜์—ฌ DI ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ DI ํŒจํ„ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.โ€Ž
  • โ€ŽCommandBus ํŒจํ„ด์˜ ๋˜ ๋‹ค๋ฅธ ๊ตฌํ˜„์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ, Guice, CDI์™€ ๊ฐ™์€ DI ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ชจ๋“  ๋ช…๋ น์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Ÿฐํƒ€์ž„์— ๋ชจ๋“  ๊ฒƒ์„๋กœ๋“œ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.โ€Ž

https://ducmanhphan.github.io/2020-12-02-command-bus-pattern/#given-problem

 

Command Bus pattern

Table of contents Given problem Solution with Command Bus pattern Source code Benefits and Drawbacks Some problems with Command Bus pattern The relationship with other patterns Wrapping up Given problem The problem of CommandBus pattern is the child proble

ducmanhphan.github.io

 

Refer:

https://github.com/cloudogu/command-bus

 

GitHub - cloudogu/command-bus: Java implementation of the Command-Bus pattern for Spring and CDI

Java implementation of the Command-Bus pattern for Spring and CDI - GitHub - cloudogu/command-bus: Java implementation of the Command-Bus pattern for Spring and CDI

github.com

https://github.com/fdside/commandbus

 

GitHub - fdside/commandbus: Tiny, Lightweight Java Command Bus

Tiny, Lightweight Java Command Bus. Contribute to fdside/commandbus development by creating an account on GitHub.

github.com

https://www.sitepoint.com/command-buses-demystified-a-look-at-the-tactician-package/

 

Command Buses Demystified: A Look at the Tactician Package - SitePoint

Andrew Cairns explains Commands and Command Buses, demonstrating their use with Tactician, a popular Command Bus package for PHP

www.sitepoint.com

https://matthiasnoback.nl/2015/01/a-wave-of-command-buses/

 

A wave of command buses โ€” Matthias Noback - Blog

Recently many people in the PHP community have been discussing a thing called the "command bus". The Laravel framework nowadays contains an implementation of a command bus and people have been talking about it in several vodcasts. My interest was sparked t

matthiasnoback.nl

 

 

728x90
  1. Given problem
  2. Solution with Command Bus pattern
  3. Command class
  4. CommandHandler class
  5. CommandBus class
  6. Source code
  7. Benefits and Drawbacks
  8. Some problems with Command Bus pattern
  9. The relationship with other patterns
  10. Wrapping up
'Programmings/Design Pattern' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ํ—ฅ์‚ฌ๊ณ ๋‚  ์•„ํ‚คํ…์ฒ˜(Hexagonal Architecture)๋ž€?
์ง€๋‹ˆ๐Ÿงžโ€โ™‚๏ธ๐Ÿฅญ
์ง€๋‹ˆ๐Ÿงžโ€โ™‚๏ธ๐Ÿฅญ
์ผ์ƒ, ๊ฒŒ์ž„, ๋ง›์ง‘, ์—ฌํ–‰, ๊ฐœ๋ฐœ, IT ๋ธ”๋กœ๊ทธ๐Ÿงž
  • ์ง€๋‹ˆ๐Ÿงžโ€โ™‚๏ธ๐Ÿฅญ
    ์š”์ˆ  ๋žจํ”„๐Ÿซ–
    ์ง€๋‹ˆ๐Ÿงžโ€โ™‚๏ธ๐Ÿฅญ
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • All (549)
      • Languages (57)
        • JAVA (13)
        • JSP (1)
        • C_C++ (4)
        • Html (3)
        • CSS (1)
        • JavaScript (18)
        • Python (3)
        • Kotlin (13)
        • TypeScript (1)
      • Framework (14)
        • spring (11)
        • jstl (1)
        • angular (2)
      • Tool (28)
        • Eclipse (5)
        • vsCode (3)
        • scrcpy (2)
        • Git (1)
        • IntelliJ (6)
        • Visual-studio (1)
        • UML (1)
        • Gradle (8)
      • DB (6)
        • Oracle (1)
        • MySql (3)
        • Mongo (2)
      • OS (14)
        • Linux (2)
        • Windows (12)
      • Server (8)
        • Tomcat (1)
        • Apache (1)
        • Node.js (6)
      • Programmings (25)
        • Design Pattern (2)
        • Funny (20)
        • Algorithms (3)
      • Cloud (8)
        • Docker (1)
        • Kubernetes (4)
        • Istio (1)
        • ArgoCD (2)
      • IT (5)
        • gRPC (3)
        • RESTful (3)
        • Web UI (5)
        • AI (4)
      • Book (6)
      • TIP (187)
      • Life (53)
      • Game (83)
      • Storage (22)
      • ์‹๋‹น (15)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ์‚ฌ์ดํŠธ๋งต
    • RSS
    • ๊ธฐํƒ€ ์†Œ๋“
  • ๋งํฌ

    • ๊ตฌ๊ธ€
    • ๋„ค์ด๋ฒ„
    • ์ •๋ถ€24
    • Spring Framework ๋ฆด๋ฆฌ์ฆˆ ๋…ธํŠธ
    • Kotlin ๋ฆด๋ฆฌ์ฆˆ ๋…ธํŠธ
    • ์นด์นด์˜ค ์• ๋“œํ•
    • ๋ธ”๋กœ๊ทธ ์‚ฌ์ดํŠธ๋งต
    • ๋ธ”๋กœ๊ทธ RSS
  • ๊ณต์ง€์‚ฌํ•ญ

    • ์•ˆ๋…•ํ•˜์„ธ์š”
  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ํ€˜์ŠคํŠธ
    ๋ชฌ์Šคํ„ฐํ—Œํ„ฐ๋‚˜์šฐ
    ๋‰ด์Šค
    ๋‰ด์Šค ๋ธŒ๋ฆฌํ•‘
    ๋ชฌ์Šคํ„ฐํ—Œํ„ฐ์™€์ผ์ฆˆ
    ํƒœ๊ตญ
    ์˜ค๋Š˜์˜๋‰ด์Šค
    ๋ธŒ๋ฆฌํ•‘
    ๋ฐ์ผ๋ฆฌ ๋‰ด์Šค
    ํ•œ๋ˆˆ์— ๋ณด๋Š” ์˜ค๋Š˜์˜ ๋‰ด์Šค
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
์ง€๋‹ˆ๐Ÿงžโ€โ™‚๏ธ๐Ÿฅญ
[Design Pattern] Command Bus pattern ์ด๋ž€?

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.