开发者

design pattern - dispatcher

开发者 https://www.devze.com 2023-03-11 22:33 出处:网络
I have a use-case like the following: Lets say I have anenumType Foo (possible values A,B,C). Suppose I have

I have a use-case like the following:

Lets say I have an enumType Foo (possible values A,B,C). Suppose I have

开发者_Go百科
Class Bar{
 List<Foo> foos; // list of Foo objects where each object can have 1 of 3 possible values
 long id;
 String name;
 String num;
 ...
}

My clients would call a method "dispatch" in Dispatcher class, which takes in "Bar".

Now for each (Foo foo : foos), it delegates the actual dispatch to specific types (i.e. I want a seperate handler for A, B and C type of messages).

Secondly, the message which has to be dispatched should be created differently for A, B and C. Based on the value of foo, we pick up the message from templates and insert "name", "num" as obtained from Bar).

Are there specific patterns I am looking at for this design? How should I be designing such a system. would appreciate a discussion or pointers to existing patterns or best practices.


Use polymorphism.

enum Foo {
    VALUE1 {
        @Override public void someMethod() {...}
    },
    VALUE2 {
        @Override public void someMethod() {...}
    },
    VALUE 3 {
        @Override public void someMethod() {...}
    };

    public abstract void someMethod();

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号