繁体   English   中英

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

[英]Factory pattern implementations with different parameters

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

要创建的类:

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的代码。

您可以将其简化为:

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

暂无
暂无

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

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