简体   繁体   English

抽象工厂设计模式C#

[英]abstract factory design pattern c#

Hi I am new to design pattern and apologize if this question is creating any confusion although i am trying to describe the issue in best possible way.I have implemented sample abstract factory pattern in winforms. 嗨,我是设计模式的新手,虽然这个问题正在引起任何困惑,但我很抱歉,尽管我试图以最好的方式来描述问题。我已经在Winforms中实现了示例抽象工厂模式。 Front end contains two check boxes to create the objects. 前端包含两个用于创建对象的复选框。 Note: If both the check box are checked, both the objects are created. 注意:如果两个复选框都被选中,则将创建两个对象。 I am using objs.CreateProduct(Maxima,Ultima) method and passing the boolean values to create the objects. 我正在使用objs.CreateProduct(Maxima,Ultima)方法并传递布尔值来创建对象。 Here I am passing the values of both the properts whether I want to create object for ultima or maxima. 在这里,无论我要为最终还是最大对象创建对象,我都要传递两个属性的值。 Can you suggest any other better way to achieve this ? 您能否提出其他更好的方法来实现这一目标? I don't want to pass the properties for maxima and ultima if I am creating the objects. 如果我要创建对象,则不希望传递maxima和ultima属性。

 public partial class Form1 : Form
    {
        public bool Maxima
        {
            get;
            set;
        }

        public bool Ultima
        {
            get;
            set;
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Factory[] obj = new Factory[2];
            obj[0] = new B();
            obj[1] = new C();

            foreach (Factory objs in obj)
            {
                iProduct prod = objs.CreateProduct(Maxima,Ultima);
                if (prod != null)
                {
                    prod.GetDetails();
                }
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
                Maxima = true;
                else
                Maxima = false;

            if (checkBox1.Checked)
                Ultima = true;
            else
                Ultima = false;
        }
    }


    abstract class Factory
    {
        public abstract iProduct CreateProduct(bool maxima, bool ultima);
    }

    class B : Factory
    {
        public override iProduct CreateProduct(bool maxima,bool ultima)
        {
            if (ultima)
            {
                return new NissanUltima();
            }
            else return null;
        }
    }

    class C : Factory
    {
        public override iProduct CreateProduct(bool maxima,bool ultima)
        {
            if (maxima)
            {
                return new NissanMaxima();
            }
            else return null;
        }
    }

    interface iProduct
    {
        void GetDetails();
    }

    class NissanUltima:iProduct
    {

        public void GetDetails()
        {
            MessageBox.Show("NissanUltima is created");
        }

    }

    class NissanMaxima:iProduct
    {
        public void GetDetails()
        {
             MessageBox.Show("NissanMaxima is created");
        }
    }

I would suggest to redesign that code. 我建议重新设计该代码。 Abstract Factory is to create an abstract product say a car in your sample. 抽象工厂将创建一个抽象产品,在您的样本中说一辆汽车。 A specific factory addss a trait of the product. 特定的工厂增加了产品的特性。 Lets say Nissanfactory and Fordfactory then in each CreateFactory() you may scecify a model of the car you want to create. 假设Nissanfactory和Fordfactory,则可以在每个CreateFactory()中指定要创建的汽车的模型。

 abstract class Factory
{
    public abstract iProduct CreateProduct(int Model);
}

class NissanFactory : Factory
{
    public override iProduct CreateProduct(int Model)
    {
        // say 1  is Maxima
        //say 2   is Untima 
        if (Model ==1)
        {
            return new NissanMaxima();
        }
        if(Model ==2)
        {
            return new NissanUltima();

        }
         return null;
    }
}

class FordFartory : Factory
{
    public override iProduct CreateProduct(int Model)
    {
        if (Model == 1)
        {
            return new GrandTorino();
        }
        if (Model == 2)
        {
            return new Mustang();

        }
        return null;
    }
}

// //

 private void button1_Click(object sender, EventArgs e)
    {


         Factory[] obj = new Factory[1];
          obj[0] =new NissanFactory();

        private List<iProduct> products = new List<iProduct>();

        //create maxima if it's chacked
        if (checkBox2.Checked)
           products.Add(obj.CreateProduct(1));

         //create ultima
        if (checkBox1.Checked)
            products.Add(prod = obj.CreateProduct(2));

        //now you can navigate via list of created products
        foreach (IProduct car in products)
        {


                prod.GetDetails();

        }

    }

A factory base class interface should be allow clients to create any kind of descendant instance, based only on the parameters provided to its create method. 工厂基类接口应允许客户端仅基于提供给其create方法的参数来创建任何种类的后代实例。 The whole point is decoupling object creation from knowledge about specific concrete types, in order to allow eg dependency injection. 关键是要使对象创建与有关特定具体类型的知识脱钩,以便允许例如依赖注入。

If you want to provide distinct initialization data to various descendant factories, that data should be contained in or provided to the factory class itself (since whatever code is creating and configuring the factories is the only part that should know about the concrete type). 如果要向各种子代工厂提供不同的初始化数据,则该数据应包含在工厂类本身中或提供给工厂类本身(因为创建和配置工厂的任何代码都是唯一应了解具体类型的部分)。 So, initialize B with the bool value for Ultima and C with the value of Maxima . 因此,用Ultima的布尔值初始化BMaxima的值初始化C

Frankly, you may have edited your example a bit too heavily: I'm not really sure of what you are trying to do. 坦白说,您可能对示例进行了过多的编辑:我不太确定您要做什么。 If the WinForms code should be unaware of the concrete types, you're going to need to introduce some kind of decoupling interface between that and your factory creation code in order to pass initialization data. 如果WinForms代码不知道具体类型,则将需要在它们与工厂创建代码之间引入某种解耦接口,以便传递初始化数据。

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

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