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

时间:2016-07-29 15:57:24

标签: c# design-patterns factory-pattern

我正在尝试创建一个创建使用相同基类(或接口)的类的工厂,但具体工厂需要不同的参数集。如果我觉得我做错了,因为这些不同的Enums需要额外的代码。这可以做得更好吗?

要创建的类:

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; }
}

工厂:

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 ();
        }
    }
}

调用工厂的类:

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 ();
    }
}

这些"经理"实际上是WPF视图模型,可以在运行时更改其代表显示的数据。为简洁起见,我删除了viewmodel特定代码。

1 个答案:

答案 0 :(得分:1)

您可以将其缩小为:

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");
        }
    }
}

所以一个工厂,一个包含所有形状类型的枚举,你可以有一个基本上这样做的经理:

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