简体   繁体   English

这是表示抽象工厂模式的一个很好的例子

[英]Is this a good example for representing the abstract factory pattern

Want to check if this a good example for representing the abstract factory pattern. 想检查这是否是表示抽象工厂模式的好例子。 Here is the theme Dell (Factory) makes xps (Product) Dell (Factory) makes inspiron (Product) hp (Factory) makes envoy (Product) hp (Factory) makes presario (Product) 这是主题戴尔(工厂)制造xps(产品)戴尔(工厂)制造灵感(产品)惠普(工厂)使特使(产品)惠普(工厂)制造presario(产品)

BestBuy sells computers. BestBuy销售电脑。

//Abstract factory
abstract class ComputerFactory
{
    public abstract Computer BuildComputer(Computer.ComputerType compType);
}

//Concrete factory
class Dell : ComputerFactory
{
    public override Computer BuildComputer(Computer.ComputerType compType)
    {
        if (compType == Computer.ComputerType.xps)
            return (new xps());
        else if (compType == Computer.ComputerType.inspiron)
            return new inspiron();
        else
            return null;
    }
}

//Concrete factory
class Hp : ComputerFactory
{
    public override Computer BuildComputer(Computer.ComputerType compType)
    {
        if (compType == Computer.ComputerType.envoy)
            return (new envoy());
        else if (compType == Computer.ComputerType.presario)
            return new presario();
        else
            return null;
    }
}

//Abstract product
public abstract class Computer
{
    public abstract string Mhz { get; set; }
    public enum ComputerType
    {
        xps,
        inspiron,
        envoy,
        presario
    }
}

//Concrete product for DELL
public class xps : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for DELL
public class inspiron : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for HP
public class envoy : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for HP
public class presario : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

public class BestBuy
{
    ComputerFactory compFactory;
    Computer comp;
    public BestBuy(Computer.ComputerType compType)
    {
        if (compType == Computer.ComputerType.xps || compType == Computer.ComputerType.inspiron)
            compFactory = new Dell();
        else
            compFactory = new Hp();

        comp = compFactory.BuildComputer(compType);
    }

    public Computer Sell()
    {
        return comp;
    }
}

Thanks in advance. 提前致谢。

It is a good example of portions of the pattern. 这是模式部分的一个很好的例子。 The basic construction of objects is a decent example, however, the logic relies upon a single Computer.ComputerType enum. 对象的基本构造是一个不错的例子,但逻辑依赖于单个Computer.ComputerType枚举。 This enum needs to know, in advance, every type of computer exposed by every factory. 这个枚举需要事先知道每个工厂暴露的每种类型的计算机。

Often, the motivation for using an abstract factory is to abstract that type of hard coded requirement out of the picture. 通常,使用抽象工厂的动机是从图片中抽象出这种类型的硬编码要求。 Instead of having a single enum, it might be better to add a ComputerType class, and allow the factory to return a collection of available types. 最好不要使用单个枚举,而是添加ComputerType类,并允许工厂返回可用类型的集合。 You could then use the ComputerType returned to construct the new systems. 然后,您可以使用返回的ComputerType来构建新系统。

This allows you to add other factories without changing your API, which is one of the major advantages of the abstract factory pattern. 这允许您在不更改API的情况下添加其他工厂,这是抽象工厂模式的主要优点之一。 Read up on the Abstract Factory Pattern - one of the main points is: 阅读抽象工厂模式 - 其中一个要点是:

The client does not know (or care) which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. 客户端不知道(或关心)从每个内部工厂获得哪些具体对象,因为它仅使用其产品的通用接口。

In this case, you're "hard coding" the known types into the enum, which violates this portion of the pattern. 在这种情况下,您将已知类型“硬编码”到枚举中,这违反了模式的这一部分。

I'm not Factory pattern expert but here are couple of things I would do differently: 我不是工厂模式专家,但这里有几件我会做的事情不同:

  • Instead of an abstract class, I would use an Interface. 而不是抽象类,我会使用一个接口。 So if "Dell" needed to inherit from another class it could and still be able to be a ComputerFactory by implementing IComputerFactory for example. 因此,如果“戴尔”需要从另一个类继承,它可以并且仍然可以通过实现IComputerFactory来成为ComputerFactory。
  • The other small thing is use a "switch" instead of an "if/else if" in your BuildComputer function. 另一个小问题是在BuildComputer函数中使用“switch”而不是“if / else if”。 Who knows how many computers you might end up with in the end. 谁知道最终可能会有多少台计算机。
  • How would you know which concrete Factory to use between Hp and Dell? 你怎么知道在Hp和戴尔之间使用哪个混凝土工厂? You might use something like " Autofac " to "resolve" which factory to use. 您可以使用“ Autofac ”之类的东西来“解析”要使用的工厂。

I think, in the scenario and code you have provided, there is only one type of product, ie 'Computer'. 我认为,在您提供的场景和代码中,只有一种类型的产品,即“计算机”。 There is no family of products involved. 没有涉及的产品系列。 So, the abstract factory pattern will not apply here. 因此,抽象工厂模式不适用于此处。 Instead factory pattern can be used here. 相反,工厂模式可以在这里使用。 I have modified the code below for understanding. 我修改了下面的代码以便理解。

//Abstract factory
abstract class ComputerFactory
{
    public abstract Computer BuildComputer(Computer.ComputerType compType);
}

public class ConcreteFactory : ComputerFactory
{
    public override Computer BuildComputer(Computer.ComputerType compType)
    {
        if (compType == Computer.ComputerType.xps)
            return (new xps());
        else if (compType == Computer.ComputerType.inspiron)
            return new inspiron();
        else if (compType == Computer.ComputerType.envoy)
            return (new envoy());
        else if (compType == Computer.ComputerType.presario)
            return new presario();
        else
            return null;
    }
}

//Abstract product
public abstract class Computer
{
    public abstract string Mhz { get; set; }
    public enum ComputerType
    {
        xps,
        inspiron,
        envoy,
        presario
    }
}

//Concrete product for DELL
public class xps : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for DELL
public class inspiron : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for HP
public class envoy : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

//Concrete product for HP
public class presario : Computer
{
    string _mhz = string.Empty;

    public override string Mhz
    {
        get
        {
            return _mhz;
        }
        set
        {
            _mhz = value;
        }
    }
}

public class BestBuy
{        
    ConcreteFactory compFactory;
    Computer comp;
    public BestBuy(Computer.ComputerType compType)
    {
        comp = compFactory.BuildComputer(compType);            
    }

    public Computer Sell()
    {
        return comp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM