Re: Switch Statements and Refactoring
From: Kurt (kurbylogic_at_hotmail.com)
Date: 11/10/04
- Previous message: Phlip: "Re: agile/xp question (formal analysis)"
- In reply to: DT: "Re: Switch Statements and Refactoring"
- Next in thread: Christian Seebode: "Re: Switch Statements and Refactoring"
- Reply: Christian Seebode: "Re: Switch Statements and Refactoring"
- Reply: Topmind: "Re: Switch Statements and Refactoring"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 9 Nov 2004 22:46:16 -0800
"DT" <dt@nospam.co.uk> wrote in message news:<418a8258$0$87832$65c69314@mercury.nildram.net>...
> Sorry I'm probably being really thick here...
>
> But wouldn't you need conditional logic to create the concrete objects in
> the first place?
Not necessarily... you can use a map:
class ShipFactory : IShipFactory
{
Hashtable shipTypes = new Hashtable();
ShipFactory()
{
shipTypes.Add("Sub", new Sub());
shipTypes.Add("Cruiser", new Cruiser());
shipTypes.Add("Destroyer", new Destroyer());
shipTypes.Add("Carrier", new Carrier());
shipTypes.Add("Battleship", new Battleship());
}
public IShip CreateShip(string name)
{
return shipTypes[name].Clone();
}
}
Now your battleship starwars edition and lord of the rings editions
can use the same IShipFactory interface to create subs, cruisers,
etc... without knowing its type or using a if/else or switch, replace
name with whatever typecode identifier you would like to use.
The "Key" can be whatever you want it to be and the mapping doesn't
need to be a stored in a hashtable.
In .NET you can place the class name in a config file if you like and
do something like:
try
{ return Activator.CreateInstance(ConfigurationSettings.AppSettings[name]);
}
catch { return null; }
If statements can check more then one condition and perhaps it doesn't
map nicely to a single Key like switch statements, but in such cases
there isn't much stoping you from looping through each type to check
if it meets the criteria... in which case you may not care which type
is returned so long as it matches the conditions.
(min=2 max=3 could return a sub or a cruiser but caller doesn't care
or
they would have been more specific)
public IShip CreateShipByHitsToDestroy(int min, int max)
{
foreach(IShip s in shipTypes)
{
if(s.HitsToDestoryBetween(min, max)) return s.Clone();
}
}
If you noticed I used the prototype creational pattern so I could work
with actual ship instances allowing them to participate in doing there
own criteria checks, they are the ones with the information after all,
at least in this trivial example.
Does that help?
- Kurt
>
>
> "Doc O'Leary" <droleary.usenet@2004.subsume.com> wrote in message
> news:droleary.usenet-659EB6.11044304112004@corp.supernews.com...
> > In article <4189d7f3$0$87823$65c69314@mercury.nildram.net>,
> > "DT" <dt@nospam.co.uk> wrote:
> >
> >> I was kind of hoping for a more complete example. This
> >> simply
> >> gives the UML which I'm happy with. How would you then use the concrete
> >> classes without using conditional logic?
> >
> > You just call getSpeed() and the appropriate implementation for the
> > class in question is called instead of the super having to know about
> > and execute a switch on all of the subs.
- Previous message: Phlip: "Re: agile/xp question (formal analysis)"
- In reply to: DT: "Re: Switch Statements and Refactoring"
- Next in thread: Christian Seebode: "Re: Switch Statements and Refactoring"
- Reply: Christian Seebode: "Re: Switch Statements and Refactoring"
- Reply: Topmind: "Re: Switch Statements and Refactoring"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|