Re: Switch Statements and Refactoring

From: Kurt (kurbylogic_at_hotmail.com)
Date: 11/10/04

  • Next message: Laurent Bossavit: "Re: agile/xp question (formal analysis)"
    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.


  • Next message: Laurent Bossavit: "Re: agile/xp question (formal analysis)"

    Relevant Pages

    • Re: Need to simplify Switch()
      ... >simplify the switch and it works great now. ... >> Rule #5 was partially taken care of in my suggested ... then the result of the IIf will be: ... >>>The finished product should be left with no extra spaces, ...
      (microsoft.public.access.reports)
    • Re: [linux-usb-devel] [PATCH] USB: Only enable autosuspend by default on certain device clas
      ... to switch it off" results in them wondering why it was switched on in ... Many of our users aren't technical - they don't care ... they just care about their printer working when they ... it certainly will suspend all devices when the system goes ...
      (Linux-Kernel)
    • Re: Purpose of TZ setting?
      ... -> Isn't the purpose of the TZ string in ... that the OS is supposed to be able to take care of ST/DST ... -> changes for the system clock on its own? ... -> Time868 to eventually make the switch to DST on its next run, ...
      (comp.os.os2.misc)
    • Re: Need to simplify Switch()
      ... I don't think Switch is the most ... Rule #5 was partially taken care of in my suggested ... Be sure to review my earlier explanation of how + and & work ... >The finished product should be left with no extra spaces, ...
      (microsoft.public.access.reports)
    • Re: implimenting select...Case
      ... Search = Switch(_ ... > This works fine except that ive got 7 queries for the rowsource of Result ... 'find all the students in a building ... > Its going to get worse cos ive been told that further criteria will be ...
      (microsoft.public.access.formscoding)