简体   繁体   English

具有不同参数的工厂模式实现

[英]Factory pattern implementations with different parameters

I am trying to make a factory that creates classes that use the same base class(or interface) but the concrete factories need different parameter sets. 我正在尝试创建一个工厂,该工厂创建使用相同基类(或接口)的类,但具体工厂需要不同的参数集。 If feel like I'm doing something wrong since these different Enums require extra code. 如果觉得我做错了,因为这些不同的Enum需要额外的代码。 Could this be done better? 可以做得更好吗?

Classes to be created: 要创建的类:

public interface IShapeData {}

public abstract class ShapeDataWithCorners : IShapeData
{
    public double Width { get; set; }
}

class Square : ShapeDataWithCorners {}

class Rectangle : ShapeDataWithCorners
{
    public double Height { get; set; }
}

class Circle : IShapeData
{
    public double Radius { get; set; }
}

class Oval : IShapeData
{
    public double Radius1 { get; set; }
    public double Radius2 { get; set; }
}

Factories: 工厂:

public enum RoundShapeTypes
{
    Circle,
    Oval
}

public enum CornerShapeTypes
{
    Square,
    Rectangle
}

public class RoundShapeDataFactory : IShapeDataFactory
{
    private readonly RoundShapeTypes m_shapeType;

    public RoundShapeDataFactory (RoundShapeTypes shapeType)
    {
        m_shapeType = shapeType;
    }

    public IShapeData CreateShapeData ()
    {
        switch (m_shapeType)
        {
            case RoundShapeTypes.Circle:
                return new Circle ();
            case RoundShapeTypes.Oval:
                return new Oval ();
        }
    }
}

public class CornerShapeDataFactory : IShapeDataFactory
{
    private readonly CornerShapeTypes m_shapeType;

    public CornerShapeDataFactory (CornerShapeTypes shapeType)
    {
        m_shapeType = shapeType;
    }

    public IShapeData CreateShapeData ()
    {
        switch (m_shapeType)
        {
            case CornerShapeTypes.Square:
                return new Square ();
            case CornerShapeTypes.Rectangle:
                return new Rectangle ();
        }
    }
}

Classes that call the factory: 调用工厂的类:

public class RoundShapeManager
{
    public IShapeData CurrentShapeData{get; set; }

    public void SetShapeType (RoundShapeTypes shapeType)
    {
        RoundShapeDataFactory factory = new RoundShapeDataFactory (shapeType);
        CurrentShapeData = factory.CreateShapeData ();
    }
}

public class CornerShapeManager
{
    public IShapeData CurrentShapeData {get; set; }

    public void SetShapeType (CornerShapeTypes shapeType)
    {
        CornerShapeDataFactory factory = new CornerShapeDataFactory (shapeType);
        CurrentShapeData = factory.CreateShapeData ();
    }
}

These "managers" are actually WPF viewmodels that will can change their representative displayed data at run-time. 这些“经理”实际上是WPF视图模型,可以在运行时更改其代表的显示数据。 I removed the viewmodel specific code for brevity. 为了简洁起见,我删除了特定于ViewModel的代码。

You can reduce it to this: 您可以将其简化为:

public interface IShapeData { }

public abstract class ShapeDataWithCorners : IShapeData
{
    public double Width { get; set; }
}

public class Square : ShapeDataWithCorners { }

public class Rectangle : ShapeDataWithCorners
{
    public double Height { get; set; }
}

public class Circle : IShapeData
{
    public double Radius { get; set; }
}

public class Oval : IShapeData
{
    public double Radius1 { get; set; }
    public double Radius2 { get; set; }
}

public enum ShapeType
{
    Circle,
    Oval,
    Square,
    Rectangle
}

public interface IShapeDataFactory
{
    IShapeData CreateShapeData(ShapeType shapeType);
}

public class ShapeDataFactory : IShapeDataFactory
{
    public IShapeData CreateShapeData(ShapeType shapeType)
    {
        switch (shapeType)
        {
            case ShapeType.Circle:
                return new Square();
            case ShapeType.Oval:
                return new Oval();
            case ShapeType.Rectangle:
                return new Rectangle();
            case ShapeType.Square:
                return new Square();
            default:
                throw new ArgumentException("invalid shape type");
        }
    }
}

So one factory, one enum with all shape types, and you can have a single manager that basically does this: 因此,一个工厂,一个具有所有形状类型的枚举,您可以只有一个管理器,基本上可以做到这一点:

IShapeDataFactory s = new ShapeDataFactory();
IShapeData temp = s.CreateShapeData(ShapeType.Square);

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

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