簡體   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