2014-04-01 64 views
2

最近我參加了一次採訪,並給出了這個問題:設計類

問:與您以下實體,設計類圖或框架代碼:

該實體是:

garmentshirtpantfabricbuttonszip

The best I could do was this: 


class Shirt : Fabric 
{ 
    Buttons buttons {get;set;} 
    //Inherits all Fabric methods 

    MakeShirt() 
    { 
    //make a shirt here 
    } 
} 

class Pant : Fabric 
{ 
    Buttons buttons {get;set;} 
    // Inherits all Fabric methods 

    MakePant() 
    { 
    //Make a pant here 
    } 
} 


class Fabric 
{ 
    private MaterialType materialType {get;set;} 
    private FabricType fabricType {get;set;} 

    Fabric(MaterialType mType, FabricType fType) 
    { 
    this.materialType = mtype; 
    this.fabricType = fType; 
    } 

    public GetFabricMaterial(); 
    public GetFabricType(); 

} 

class Garment : IGarment 
{ 
    Price price {get;set;} 
    Audience audience {get;set;} 
} 

enum FabricType 
{ 
    Fabric_Formal, 
    Fabric_Casual, 
} 

enum MaterialType 
{ 
    Fabric_Cotton, 
    Fabric_Silk, 
    Fabric_Wool, 
} 

class Buttons 
{ 
Color color {get;set;} 
Shape shape {get;set;} 
} 

class Zip 
{ 
Color color {get;set;} 
Size size {get;set;} 
} 

但我仍可以看到許多東西從上面的骨架代碼錯過了。

  1. 我如何能與Garment與其他實體(對象關係!?)
  2. 什麼可以的功能MakeShirt()MakePant()返回類型?
  3. 在回答這些問題時,哪種方法最好?換句話說,如何處理這些類型的問題?

對此有任何意見。 (如果不是在這裏問正確的問題,請讓我知道這個移動到右側計算器網站!)

+0

嗯。襯衫是一種面料嗎?我敢打賭,這是一件服裝,而且這件服裝*有一塊織物。 – spender

+0

襯衫,褲子是服裝。服裝是用織物製成的。 – Anirudha

+0

@spender:假設襯衫是一種面料,您能否指導我如何修改我的課程? –

回答

2

我想你過去想過這個,我把它看成是這個。褲子和襯衫是Garment。服裝由織物組成。

褲子有拉鍊,襯衫有按鈕..

public enum Fabric { Cotton, Silk, Poly } 

public abstract Garment{ 


    public Fabric Fabric {get; set; } 
} 

class Buttons 
{ 
    Color color {get;set;} 
    Shape shape {get;set;} 
} 
class Zip 
{ 
    Color color {get;set;} 
    Size size {get;set;} 
} 

public class Shirt : Garment{ 
    public Buttons Buttons { get; set;} 
} 
public class Pants : Garment{ 
    public Zip Zip { get; set;} 
} 
+0

好,很好,關於'對象關係'部分?請輸入任何內容? –

+0

你無法從代碼中辨別出來嗎? –

+2

今天我正在穿褲子的褲子;) – Jeb

1

這更多的是父/子關係的練習。我認爲Garment是您的抽象基類,其中ShirtPant繼承。 FabricButtonsZip都是屬性Garment

我沒有具體的Make方法。特定服裝類型的構造函數應該使服裝類型準備好,而不是初始化它。