Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 面向对象的家具设计与测试_Oop_Ooad - Fatal编程技术网

Oop 面向对象的家具设计与测试

Oop 面向对象的家具设计与测试,oop,ooad,Oop,Ooad,这个问题是在一次采访中提出的 家具可以由一种或多种材料制成。家具种类繁多,如椅子、桌子、沙发等 家具测试取决于材料和家具类型。很少有试验是可选择的、耐火的、能承受100Kw重量等 在OOAD中设计这个。设计应遵循开闭原则,并应保持乐观,以便在未来添加新家具时,不需要更多的代码更改 最初建议 class Furniture{ List<Material> materials; boolean supportsTest(Test test); } class Chair extends

这个问题是在一次采访中提出的

家具可以由一种或多种材料制成。家具种类繁多,如椅子、桌子、沙发等

家具测试取决于材料和家具类型。很少有试验是可选择的、耐火的、能承受100Kw重量等

在OOAD中设计这个。设计应遵循开闭原则,并应保持乐观,以便在未来添加新家具时,不需要更多的代码更改

最初建议

class Furniture{
List<Material> materials;
boolean supportsTest(Test test);
}

class Chair extends Furniture{
boolean supportsTest(Test test){
// check material and based on type return true/false
}

采访者说家具就是家具,不应该说它是否支持这个测试。有其他解决办法吗?谢谢。

根据你的描述和面试官的评论,我想到的是:

// An interface so that it's separate from Furniture class
interface FurnitureTest {
   bool IsSupportedBy(Furniture furniture);
}

// Sample test, can add as many as you like
class FireResistable : FurnitureTest {
   public bool IsSupportedBy(Furniture furniture) {
       return furniture.Type.Name == ! "Mirror" && /* just to use the Type property */
              !furniture.HasMaterial(x => x == "Wood");
   }
}

///
/// Other class definitions.
///

// Open to extension
class Chair : Furniture { /* additional properties */ }
class Table : Furniture { /* additional properties */ }

// Closed to modification
class Furniture {
   public FurnitureType FurnitureType { get; private set; }
   private IList<Material> materials;

   public Furniture(FurnitureType type, IList<Material> materials) {
      this.materials = materials;
   }

   public bool HasMaterial(string materialName) {
      return materials.Any(x => x.Name == materialName);
   }
}

class FurnitureType {
   public string Name { get; set; }
   // ...other properties
}

class Material {
   public string Name { get; set; }
   // other properties..
}

有什么理由反对吗?