Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Factory Pattern

This is a discussion on Factory Pattern within the Software Patterns forums, part of the Testing category; Is this bad code? (C#) I was under the impression that this is like a simple factory pattern, but I ...

  1. #1
    Jimbo Guest

    Factory Pattern

    Is this bad code? (C#)

    I was under the impression that this is like a simple factory pattern, but
    I now have my doubts (please ignore the lack of vallidation etc...)

    void main ()
    {
    string s = "";
    ProductFamily pf = new ProductFamily();
    s = someUserInputString();
    pf.getProductType(s);
    pf.doSomePolyMorphicStuff();
    }

    public class ProductFamily
    {
    public ProductFamily()
    {
    }

    public ProductFamily getProductType(string _prod_line_id)
    {
    ProductFamily productFamily;
    return ProductFactory.getProductFamily(_prod_line_id);
    }
    }

    public class ProductFactory
    {
    public ProductFactory()
    {
    }

    public static ProductFamily getProductFamily(string productLine)
    {

    switch(productLine)
    {
    // end caps -- all the same
    case "Insulating End Caps":
    case "Conductive End Caps":
    EndCap ec = new EndCap(productLine);
    return ec;

    // breakouts -- all more or less the same dimensions wise
    case "3 Way Conductive Breakout":
    case "2 Way Low Voltage Breakout":
    BreakOut boot = new BreakOut(productLine);
    return boot;

    // all tubes, they are basically the same dimensions
    case "Clear Insulating Tube":
    Tube tube = new Tube(productLine);
    return tube;

    // boots -- inline and right angle are different
    case "Inline Boots":
    InlineBoot inBoots = new InlineBoot(productLine);
    return inBoots;
    }
    }
    }

    How would I implement this with the factory or would that not be the
    correct pattern to use?



  2. #2
    EventHelix.com Guest

    Re: Factory Pattern

    > How would I implement this with the factory or would that not be the
    > correct pattern to use?


    Factory is the correct pattern to use here. The following article
    might
    help:

    http://www.eventhelix.com/RealtimeMa...ce_pattern.htm

    Sandeep
    --
    http://www.EventHelix.com/EventStudio
    EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams



Latest Article

Network Security Risk Assessment and Measurement

Read More...