please suggest design pattern
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.
- List item
- Drop Down Li开发者_如何学Pythonst
- Radio Buttons List
- 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
精彩评论