开发者

please suggest design pattern

开发者 https://www.devze.com 2023-03-28 13:44 出处:网络
I am trying to find out, what pattern will be the best to use in following scenario. I have different types of Data representation for user. User can choose , how data will be rendered on screen.

I am trying to find out, what pattern will be the best to use in following scenario.

I have different types of Data representation for user. User can choose , how data will be rendered on screen.

  1. List item
  2. Drop Down Li开发者_如何学Pythonst
  3. Radio Buttons List
  4. Check Boxes etc..

I know , that Abstract factory , or factory method will suite here. But is there any way to get rid of following:

If (SomeType == SomeTypes.DropDown)
 {
    return new DropDownClass();
 }

Is there any way to do it more abstractive ?


You can create a List for SimpleFactories and ask each in turn if it can handle the Type and if so let it create the component.

It would look like this:

interface SimpleFactory{
    boolean canHandle(SomeType type);
    Component create()
}

class Factory{
    List<SimpleFactory) factories = ....

Component create(SomeType type){
    for(f : factories)
        if (f.canHandle(type) return f.create()
    return null;
}


in .Net I regularly use a combination of generics, inheritance and polymorphism to determine the handler for a specific request during run-time.

It all is very easy when you use something like described in this post

0

精彩评论

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