2009-06-26 83 views
4

我爲新項目編寫接口,並希望得到一些建議。強制接口實現在c#中實現層次結構

我有一個類有一個子類,它有一個子類。這種類的樹是這樣的:

Class Car 
{ 
    Wheels Wheel; 
} 
Class Wheels 
{ 
    Rims Rim; 
} 

所以爲了簡化:一輛車有一個車輪,一個車輪有一個輪輞。 (不能做出其他更好的例子,對不起)。

所以我想強制這個層次結構在我的接口實現ICar,IWheels和IRims。

所以我做了這樣的事情(在C#):

ICar 
{ 
    IWheels Wheel; 
} 
IWheels 
{ 
    IRims Rim; 
} 

,我有,我不能在接口實現字段錯誤。所以這開始了我的事情,也許它是錯誤的界面設計。我想強制接口實現來實現這種層次結構。但是也許應該以其他方式來設計模式和最佳實踐呢?

你能告訴我如何設計我的系統,使對象將被迫實現這種層次?

也許有東西在我的問題不準確或遺漏我一些重要的信息。如果是,請在評論中提問。

回答

10

在你的界面,你必須說清楚,車輪應該是ICAR的屬性,因爲你不能聲明哪些字段接口的實現應該有。 (字段是內部工作,所以界面不應該知道它)。

interface ICar 
{ 
    IWheels Wheels 
    { 
     get; 
    } 
} 
4

由於錯誤提示,您不能在接口中指定字段。您可以指定雖然性能:

interface ICar 
{ 
    IWheels Wheel { get; set; } 
} 

interface IWheels 
{ 
    IRims Rim { get; set; } 
} 
7

您可以在界面上沒有指定(你不應該能夠 - 這是一個實現的決定),但你可以指定一個屬性

public interface ICar 
{ 
    IWheels Wheel { get; set; } 
} 

public interface IWheels 
{ 
    IRims Rim { get; set; } 
} 

您可能希望只放在界面,吸氣,雖然 - 這是有點不同尋常,包括在接口二傳手:

public interface ICar 
{ 
    IWheels Wheel { get; } 
} 

public interface IWheels 
{ 
    IRims Rim { get; } 
} 

(有古怪,如果你想覆蓋現有的(或抽象的)財產只有一個getter添加一個二傳手,但也沒關係實現與setter方法「吸氣劑只是」界面,以及,我相信。)

4

您不能聲明字段,但可以聲明屬性。這將具有強制特定類提供另一個類的實例的相同最終效果。

ICar 
{ 
    IWheels Wheel { get; set; } 
} 
IWheels 
{ 
    IRims Rim { get; set; } 
} 
0

我沒有那麼多用於C#,但它的聲音對我說,你可以強制通過使抽象類,你要使用的字段實現。 所以,如果你擴展這些抽象類,你會在他們可用的字段。 你將不得不作出一個抽象類和接口,但...

0

這是一個全功能的代碼... 希望它可以幫助...

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication10 
{ 
    //Interfaces 

    public interface ICar 
    { 
     string name { get;} 
     IWheel wheel { get;} 
    } 


    public interface IWheel 
    { 
     string brand { get;} 
    } 

    //Implementations 

    public class Michelin : IWheel 
    { 
     #region IWheel Members 

     public string brand 
     { 
      get { return "michelin"; } 
     } 

     #endregion 
    } 


    public class Toyota : ICar 
    { 
     Michelin m = new Michelin(); 
     #region ICar Members 

     public string name 
     { 
      get { return "toyota"; } 
     } 

     public IWheel wheel 
     { 
      get { return m; } 
     } 

     #endregion 
    } 

    //A user of the interfaces. Only cares about ICar but knows implicitly about IWheel 

    public class Stand 
    { 
     public Stand() 
     { 
      cars = new List<ICar>(2); 
      cars.Add(new Toyota()); 
      cars.Add(new Toyota()); 
     } 
     List<ICar> cars; 

     public string ShowCars() 
     { 
      StringBuilder str = new StringBuilder(); 
      foreach (ICar iterCar in cars) 
      { 

       str.AppendLine(string.Format("car {0} with wheel {1}", 
        iterCar.name, iterCar.wheel.brand)); 
      } 
      return str.ToString(); 
     } 
    } 

    //entry point. creates a stand and shows the cars, testing that properties are visible 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Stand myLittleStand = new Stand(); 
      Console.WriteLine(myLittleStand.ShowCars()); 
     } 
    } 
} 
+0

當然,以下最佳實踐屬性應該是大寫字母和字符串不應該被硬編碼,但我認爲這些都是小細節 – 2009-06-26 14:05:54