开发者

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

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(sonyO开发者_如何学Gobj.CreateProducts(Product.DVD, ProductSize.FortyTwoInch));


No, it should rather look like this:

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

class SonyProductFactory : IProductFactory
{
    string CreateProducts(Product product, ProductSize size) { ... }
}
class PhilipsProductFactory : IProductFactory
{
    string CreateProducts(Product product, ProductSize size) { ... }
}

...
IProductFactory prodFactory = loadProductFactory();
MessageBox.Show(prodFactory.CreateProducts(Product.DVD, ProductSize.FortyTwoInch));

loadProductFactory() returns a Sony or Philips factory, maybe based on configuration, or application state. This functionality could be moved into a separate factory loader class as well, to make it more reusable.

Update: A trivial implementation could be

IProductFactory loadProductFactory() {
    String factoryName = System.getProperty("factory.name");
    if (factoryName.equals("Sony") {
        return new SonyProductFactory();
    } else {
        return new PhilipsProductFactory();
    }
}

A more robust version could load the concrete product factory classname from a config file, load the class, verify that it is an IProductFactory, then return an instance of it. This has the benefit that there are no more dependencies to any concrete product factory class, so adding/changing/removing factory implementations requires no recompilation of the sources, only a config file change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜