Is Abstract Factory Pattern implemented correctly for given scenario.... ???

Posted by Amit on Stack Overflow See other posts from Stack Overflow or by Amit
Published on 2010-06-01T13:19:13Z Indexed on 2010/06/01 13:23 UTC
Read the original article Hit count: 259

Filed under:
|

First thing... I am novice to pattern world, so correct me if wrong anywhere Scenario: There are multiple companies providing multiple products of diff size

so there are 3 entities i.e. Companies, Their Product and size of product

I have implement Abstract Pattern on this i.e. so that I will create instance of IProductFactory interface to get desired product...

Is below implementation of Abstract Factory Pattern correct ??? If not then please correct the approach + Also tell me if any other pattern can be used for such scenario Thanks in advance...

public enum Companies { Samsung = 0, LG = 1, Philips = 2, Sony = 3 }

public enum Product
{
    PlasmaTv = 0,
    DVD = 1
}

public enum ProductSize
{
    FortyTwoInch,
    FiftyFiveInch
}

interface IProductFactory
{
    IPhilips GetPhilipsProduct();
    ISony GetSonyProduct();
}

interface ISony
{
    string CreateProducts(Product product, ProductSize size);
}
interface IPhilips
{
    string CreateProducts(Product product, ProductSize size);
}

class ProductFactory : IProductFactory
{
    public IPhilips GetPhilipsProduct()
    {
        return new Philips(); 
    }

    public ISony GetSonyProduct()
    {
        return new Sony();
    }
}

class Philips : IPhilips
{
    #region IPhilips Members

    public string CreateProducts(Product product, ProductSize size)
    {// I have ingnore size for now....
        string output = string.Empty;
        if (product == Product.PlasmaTv)
        {
            output = "Plasma TV Created !!!";
        }
        else if (product == Product.DVD)
        {

            output = "DVD Created !!!";
        }
        return output;
    }

    #endregion
}

class Sony : ISony
{// I have ingnore size for now....
    #region ISony Members
    public string CreateProducts(Product product, ProductSize size)
    {
        string output = string.Empty;
        if (product == Product.PlasmaTv)
        {
            output = "Plasma TV Created !!!";
        }
        else if (product == Product.DVD)
        {

            output = "DVD Created !!!";
        }
        return output;
    }
    #endregion
}

IProductFactory prodFactory = new ProductFactory(); IPhilips philipsObj = prodFactory.GetPhilipsProduct(); MessageBox.Show(philipsObj.CreateProducts(Product.DVD, ProductSize.FortyTwoInch)); or //ISony sonyObj = prodFactory.GetSonyProduct(); //MessageBox.Show(sonyObj.CreateProducts(Product.DVD, ProductSize.FortyTwoInch));

© Stack Overflow or respective owner

Related posts about design

Related posts about design-patterns