Вопрос

Recently I had attended an interview and was given this question:

Question: With the following entities with you, design a class diagram or skeleton code:

The entities are:

garment shirt pant fabric buttons zip

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;}
}

But still I can see many things missing out from the above skeleton code.

  1. How can I relate Garment with the other entities (Object relationship! ?)
  2. What can be the return type of functions MakeShirt() and MakePant() ?
  3. While answering these questions, what approach is best ? In other words, how to approach these type of questions ?

Any inputs on this is appreciated. (If this is not the right question to be asked here, kindly let me know to move this to the right stackoverflow site!)

Это было полезно?

Решение

I think you over thought this, I see it as this. Pants and Shirts are Garment. Garments are made up of Fabric.

Pants have Zip, shirts have buttons..

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;}
}

Другие советы

This is more an exercise in parent/child relationships. I would have thought Garment is your abstract base class from which Shirt and Pant inherit. Fabric, Buttons and Zip are all properties of a Garment.

I wouldn't have specific Make methods. The constructor for the specific Garment types should make the Garment type ready to go instead of post initialising it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top