2013-12-12 72 views
1

我正在尋找利用.net應用程序的策略模式。我正在簡化代碼,但想法是一樣的。有一個交通接口,它將有兩個具體的類,摩托車,這兩個唯一的要求是實施可驅動的方法。爲了實現它,我想使用戰略模式和IDriveableStrategy接口,該接口將採用實現ITransport的類並使用該類中的信息來確定ITransport是否可驅動。我看到一些帖子質疑將背景發送給戰略的做法,但我現在想忽略這種辯論。如果我實現一個名爲MotorcycleDriveableStrategy的類,我想將其用作Motorcycle類的一個策略。採取其他接口的策略模式方法

在下面的代碼中,您會注意到我有兩個特定於Motorcycle類的附加屬性,我想用它來評估ITransport是否可驅動。在MotorcycleDriveableStrategy中,我已經註釋了理想情況下如何實現類,(因爲摩托車確實實現了ITransport,所以isDriveable實現了接口,但是當我嘗試這個時,我收到了一個編譯錯誤,所以我必須讓方法期待ITransport接口。

我能做到這一點,我想通過使IDriveableStrategy接口需要一個通用的實現ITransport

IDriveableStrategy<T> where T : ITransport 

,並宣佈

MotorcycleDriveableStrategy<Motorcycle> 

耦合,但我想知道如果我錯過了更大的圖片。泛型之前這將如何實現?戰略模式應該總是基於暴露的接口實現來評估上下文嗎?策略界面是否應該將界面作爲參數?有沒有另一種方法來實現這一點,而不添加numWheels到ITransport接口或使IDriveableStrategy通用?提前感謝您的任何建議或見解。

public interface ITransport 
    { 
     boolean driveable(); 
    } 

public class Car : ITransport 
{ 
    private driveableStrategy {get;set;} 
    public boolean driveable() 
    { 
     return driveableStrategy.isDriveable(); 
    } 

} 

public class Motorcycle: ITransport 
{ 
    private driveableStrategy {get;set;} 
    private int numWheels {get;set;} 
    private string weatherExceptions {get;set;} 
    public Motorcycle(IDriveableStrategy driveableStrategy,int numWheels,string weatherExceptions) 
    { 
     this.driveableStrategy = driveableStrategy; 
     this.numWheels = numWheels; 
     this.weatherExceptions = weatherExceptions; 
    } 

    public boolean driveable() 
    { 
     return driveableStrategy.isDriveable(); 
    } 

} 

public interface IDriveableStrategy 
{ 
     boolean isDriveable(ITransport transport); 
} 

public class MotorcycleDriveableStrategy 
{ 
     //What i would like to do 
     /*public boolean isDriveable(Motorcycle transport) 
      { 
       return transport.numWheels > 2; 
      } 
     */ 
     public boolean isDriveable(ITransport transport) 
     { 

     } 
} 

回答

1

讓我先說我有發的背景下,以戰略沒有問題,IMO個人策略的開始是抽象掉的細節,所以你可以隨心所欲的另一插頭。 IStrategy接口方法允許用戶類爲所有策略執行相同的命令,但我不會將策略本身限制爲接口方法。個人戰略應該對上下文有深入的瞭解。 請讓我知道如果我完全錯過了你的觀點,如果有人有不同的意見,我會非常感興趣。

在你的例子中,MotorcycleDriveableStrategy是我們唯一應該對摩托車駕駛性能做出決定的地方。因此:

public class MotorcycleDriveableStrategy : IDriveableStrategy 
{ 
    //What i would like to do 
    public bool IsDriveable(ITransport transport) 
    { 
     var mc = transport as Motorcycle; 
     if (mc == null) return false; 
     return mc.NumWheels > 2; 
    } 
} 

如果你有興趣在整個事情:

public interface ITransport { bool Driveable();} 
public interface IDriveableStrategy { bool IsDriveable(ITransport transport);} 

public class Car : ITransport 
{ 
    private IDriveableStrategy Strategy { get; set; } 
    public bool Driveable() 
    { 
     return Strategy.IsDriveable(this); 
    } 
} 

public class Motorcycle : ITransport 
{ 
    private IDriveableStrategy Strategy { get; set; } 
    public int NumWheels { get; set; } 
    public string WeatherExceptions { get; set; } 

    public Motorcycle(IDriveableStrategy driveableStrategy, int numWheels, string weatherExceptions) 
    { 
     Strategy = driveableStrategy; 
     NumWheels = numWheels; 
     WeatherExceptions = weatherExceptions; 
    } 

    public bool Driveable() 
    { 
     return Strategy.IsDriveable(this); 
    } 
} 

public class MotorcycleDriveableStrategy : IDriveableStrategy 
{ 
    //What i would like to do 
    public bool IsDriveable(ITransport transport) 
    { 
     var mc = transport as Motorcycle; 
     if (mc == null) return false; 
     return mc.NumWheels > 2; 
    } 
} 

// And for the Car strategy 
public class CarDriveableStrategy : IDriveableStrategy 
{ 
    //place ur implementation here ... 
    public bool IsDriveable(ITransport transport) {return true;} 
} 

public class TestMotorcycle 
{ 
    public TestMotorcycle() 
    { 
     var mcs = new MotorcycleDriveableStrategy(); 
     var mc = new Motorcycle(mcs, 2, "abc"); 
     Console.WriteLine("Motorcycle is {0}drivable", mc.Driveable()?"":"not "); 
    } 
}