简体   繁体   中英

c# factory design pattern winforms

I am new to C# and need help with an assignment for school. I am supposed to create a program that gives an example of the factory design pattern using windows forms on visual studio. I have been looking online for help but the code i'm using is giving me errors. displayResults is the name of my textbox but it is not being recognized. Also, when I get to

protected void Page_Load (object sender, EventArgs e )

it is not recognizing some of my classes.

Thank you for any help or advice on this. I am extremely new and sorry if this is something really obvious.

here is all of my code:

namespace FactoryDesignPattern
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

abstract class Factory
{
    public abstract Product GetProduct();
}

class ConcreteFactoryForProudct1 : Factory
{
    public override Product GetProduct()
    {
        return new Product1();
    }
}

class ConcreteFactoryForProudct2 : Factory
{
    public override Product GetProduct()
    {
        return new Product2();
    }
}

interface Product
{
    void GetDetails();
}

class Product1 : Product
{
    public void GetDetails();

    displayResults.WriteLine("Product 1 details are: ");

}

 class Product2 : Product
{
    public void GetDetails();

    displayResults.WriteLine("Product 2 details are: ");
 }
protected void Page_Load (object sender, EventArgs e )
{
Factory[] objFactories = new Factory[2];
objFactories[0] = new ConcreteFactoryForProduct1();
objFactories[1] = new ConcreteFactoryForProduct2();
foreach (Factory objFactories in objFactories)
    {
        product objProduct = objFactory.GetProduct();
        objProduct.GetDetails();
    }
}



}

There are several issues with your code that will cause it not to compile. Below are improvements to your code, it may still not compile, haven't tried it myself, but it should get you going. I used Trace.WriteLine so watch debug window so pleas add using System.Diagnostics otherwise Trace is not recognized. I also added the public keyword a few times to make sure the classes are not hidden from each other. This may not have been needed. The factory scheme you implemented here is correct.
There were some typos in your code too, be careful with those, though your compiler will catch em all.

namespace FactoryDesignPattern
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Factory[] objFactories = new Factory[2];
            objFactories[0] = new ConcreteFactoryForProduct1();
            objFactories[1] = new ConcreteFactoryForProduct2();
            foreach (Factory objFactories in objFactories)
            {
                product objProduct = objFactory.GetProduct();
                objProduct.GetDetails();
            }
        }
    }

    public abstract class Factory
    {
        public abstract Product GetProduct();
    }

    public class ConcreteFactoryForProduct1: Factory
    {
        public override Product GetProduct()
        {
            return new Product1();
        }
    }

    public class ConcreteFactoryForProduct2: Factory
    {
        public override Product GetProduct()
        {
            return new Product2();
        }
    }

    public interface Product
    {
        void GetDetails();
    }

    public class Product1 : Product
    {
        public void GetDetails(){
            Trace.WriteLine("Product 1 details are: ");
        }
    }

    public class Product2 : Product
    {
        public void GetDetails(){
            Trace.WriteLine("Product 2 details are: ");
        }
    }
}
namespace Abstract_Factory
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int x = comboBox1.SelectedIndex;
            Factory[] objFactories = new Factory[1];
            switch (x)
            {
                case 0:
                    objFactories[0] = new Figura1();
                    foreach (Factory ob in objFactories)
                    {
                        FIGURAS objProduct = ob.GetFig();
                        objProduct.Mensaje();
                    }
                    break;
                case 1:
                    objFactories[0] = new Figura2();
                    foreach (Factory ob in objFactories)
                    {
                        FIGURAS objProduct = ob.GetFig();
                        objProduct.Mensaje();
                    }
                    break;
                case 2:
                    objFactories[0] = new Figura3();
                    foreach (Factory ob in objFactories)
                    {
                        FIGURAS objProduct = ob.GetFig();
                        objProduct.Mensaje();
                    }
                    break;
                default:
                    {
                        MessageBox.Show("***");
                        break;
                    }
            }
        }

        public abstract class Factory
        {
            public abstract FIGURAS GetFig();
        }

        public class Figura1 : Factory
        {
            public override FIGURAS GetFig()
            {
                return new Fig1();
            }
        }

        public class Figura2 : Factory
        {
            public override FIGURAS GetFig()
            {
                return new Fig2();
            }
        }
        public class Figura3 : Factory
        {
            public override FIGURAS GetFig()
            {
                return new Fig3();
            }
        }
        public interface FIGURAS
        {
            void Mensaje();
        }
        public class Fig1 : FIGURAS
        {
            public void Mensaje()
            {
                MessageBox.Show("FIGURA 1: CUADRADO");
            }
        }
        public class Fig2 : FIGURAS
        {
            public void Mensaje()
            {
                MessageBox.Show("FIGURA 2: RECTANGULO");
            }
        }
        public class Fig3 : FIGURAS
        {
            public void Mensaje()
            {
                MessageBox.Show("FIGURA 3: TRIANGULO");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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