Design Pattern (maybe)

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I got the following scenario :
ComboBox in UI where the user chooses one of the options and according
to his/her choise - I create new object (or sub form). What is the best
way doing it? (I can do switch with cases but it seems not good enough)
Thanks!
 
If this is four options that are known at compile-time, then switch
should be fine. If you need the same code repeatedly, and the 4
options share some commonality (base-class or interface) then perhaps
make a factory method that accepts an enum and returns the common-
factor, i.e.

static IFoo CreateFoo(FooType type) // an enum
{
switch(type) {
case type.SimpleFoo: return new SimpleFoo();
...
default: throw new ArgumentException("type");
}
}

If the types aren't known in advance (i.e. it comes from a database),
then you'd need a factory approach, perhaps with reflection
(Type.GetType(name) and/or Activator.CreateInstance()) - but this is
slower and more complex.

But keep it simple if you can - it will be easier to debug, which is
worth a lot.
 
csharpula csharp said:
Hello,
I got the following scenario :
ComboBox in UI where the user chooses one of the options and according
to his/her choise - I create new object (or sub form). What is the best
way doing it? (I can do switch with cases but it seems not good enough)
Thanks!
It depends on the rules wich etnries should be shown in the DropDown and
wich form should be opend when.
In the easy case of a fixed (not to large) set of entries and each opens a
certain form I'd say switch is OK.

In other cases: It depends

But it should also be considered how the list/rules my change in future.
(Maintainability)

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top