2016-07-29 186 views
0

我正在嘗試創建一個使用相同基類(或接口)但具體工廠需要不同參數集的類的工廠。如果覺得我做錯了什麼,因爲這些不同的枚舉需要額外的代碼。這能做得更好嗎?要創建具有不同參數的工廠模式實現

類:

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的ViewModels,將可以改變他們的代表在運行時顯示數據。爲了簡潔,我刪除了viewmodel特定的代碼。

+0

這可能是一個很好的問題[codereview.se] – null

+1

我剛剛離開了評論,而不是近距離投票。由於近距離投票是「太寬泛」,我想這是因爲你的問題是開放式的,這對CR是有效的,而不是在SO上(見你鏈接到的元問題的接受答案中的表格)。在與您所引用的行相同的段落中:「*相反,投票結束時間過長或主要以意見爲基礎。*」 – null

回答

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

單獨的枚舉將用於在UI中顯示有效的選項。我想在UI方面可能發生轉換,以便在調用工廠時使用單個枚舉。 – kbeal2k

+0

這是有道理的。告訴工廠返回哪個類型的參數屬於'Create'方法(如答案中所示),而不是工廠的構造函數。如果該類需要知道哪些構造函數參數要傳遞給工廠,那麼該類已經負責創建工廠,這違背了工廠的目的。 –

+0

@ScottHannen - 同意。責任只在工廠而不在工廠周圍。 – Ric