PC Review


Reply
Thread Tools Rate Thread

Design Pattern (maybe)

 
 
csharpula csharp
Guest
Posts: n/a
 
      22nd Nov 2007

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!


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Nov 2007
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.
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      22nd Nov 2007
In this case, I would recommend a class factory pattern:

http://en.wikipedia.org/wiki/Factory_method_pattern


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"csharpula csharp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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!
>
>
> *** Sent via Developersdex http://www.developersdex.com ***


 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      22nd Nov 2007
"csharpula csharp" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design/Pattern guidance to refector my current design for unit testing sklett Microsoft C# .NET 1 22nd Aug 2007 12:58 PM
Please help about the design pattern. Cylix Microsoft Dot NET 1 10th Aug 2006 09:13 AM
Design Pattern? pitachu@gmail.com Microsoft C# .NET 6 31st Jan 2006 10:31 PM
Design Pattern? pitachu@gmail.com Microsoft Dot NET Framework 2 31st Jan 2006 06:36 PM
design pattern Hugo Batista Microsoft Dot NET Framework 3 25th Feb 2005 07:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:42 PM.