Architecture 这个小程序的设计?

Architecture 这个小程序的设计?,architecture,ooad,Architecture,Ooad,请不要介意我的问题,因为它有点理论性。 上周我参加了一次面试,面试时我被分配了一个任务,后来他们问了我一个问题,我的设计有问题。所以我没能得到这份工作。我正在分享我的设计。请看它,并建议我哪里我错了,什么样的解决方案将是一个好的设计 问题 这是一个为建造游泳池的建筑公司准备的包。游泳池由游泳池周边和游泳池两部分组成。两者(游泳池和周围区域)可以是矩形或圆形(请参见附图),因此将有4种可能的形状。程序应计算阴影区域(水池至混凝土的外部区域) ****计算矩形面积**的公式**为长度*宽度*2 计算

请不要介意我的问题,因为它有点理论性。

上周我参加了一次面试,面试时我被分配了一个任务,后来他们问了我一个问题,我的设计有问题。所以我没能得到这份工作。我正在分享我的设计。请看它,并建议我哪里我错了,什么样的解决方案将是一个好的设计

问题 这是一个为建造游泳池的建筑公司准备的包。游泳池由游泳池周边和游泳池两部分组成。两者(游泳池和周围区域)可以是矩形或圆形(请参见附图),因此将有4种可能的形状。程序应计算阴影区域(水池至混凝土的外部区域)

****计算矩形面积**的公式**为长度*宽度*2 计算圆形面积的公式为2*2.1718

我的设计 我用两种方法制作了IPool接口(1)CalculateCircularArea(2)CalculateRectangularArea。制作了两个实现IPool接口的类(CircularPool、RectangularPool)

问题
如果有更多的形状,而不是矩形和圆形呢。比如说,如果还有100种其他形状,未来会考虑什么解决方案?在这里,我的设计并不好,因为当一个新的形状出现时,它需要改变界面。

子类化的典型案例。使用抽象函数
getArea()
设计一个抽象类
Shape
,并为您必须处理的每个可能的形式创建一个具体的子类

Pool
SurroundingArea
只需调用
getArea()
,如下所示:

public class SwimmingPool {
   Shape pool;
   Shape surrounding;

   public double geArea() {return pool.getArea() + surrounding.getArea()}
}

public abstract class Shape {
   public abstract double getArea();
}

public class Circle extends Shape {
   public double getArea() {
     // implementation of area calculation of circles
   }
}

public class Square extends Shape {
   public double getArea() {
     // implementation of area calculation of squares
   }
}

...

对于池或其他形状的环境,您只需要设计形状的另一个子类。

子类化的典型案例。使用抽象函数
getArea()
设计一个抽象类
Shape
,并为您必须处理的每个可能的形式创建一个具体的子类

Pool
SurroundingArea
只需调用
getArea()
,如下所示:

public class SwimmingPool {
   Shape pool;
   Shape surrounding;

   public double geArea() {return pool.getArea() + surrounding.getArea()}
}

public abstract class Shape {
   public abstract double getArea();
}

public class Circle extends Shape {
   public double getArea() {
     // implementation of area calculation of circles
   }
}

public class Square extends Shape {
   public double getArea() {
     // implementation of area calculation of squares
   }
}

...

对于池或其他形状的环境,您只需要设计另一个Shape子类。

我认为更好的设计是使用类似于
IShape
的接口,该接口公开
getArea()
方法。(或者,更好的是,将其作为财产)


通过这种方式,您的两个“对象”都实现了这样的接口,您可以通过简单地计算
getArea()

的值之间的差值来计算所需的面积。我认为更好的设计是使用类似于
IShape
的接口来公开
getArea()
方法。(或者,更好的是,将其作为财产)


这样,您的两个“对象”都实现了这样的接口,您可以通过简单地计算
getArea()

的值之间的差值来计算所需的面积。问题中已经给出了设计:一个游泳池有一个周围,它有一个内池。该任务要求组合,而不是继承

有了这个设计,我们可以发明其他的游泳池形状和周围环境,仍然可以拥有游泳池

public interface SwimmingPool {
  Surrounding getSurrounding();
  Pool getPool();
  double getOuterArea();
}

public interface Surrounding extends Shape{
  // some methods for surroundings
}

public interface Pool extends Shape {
  // some methods for pools
}

public interface Shape {
   double getArea();
}
接下来,将为矩形和圆形游泳池创建具体类,并使用这些模块设计游泳池


现在,我们创建一个具体的游泳池类:

public SwimmingPoolImpl implements SwimmingPool {
 private Surrounding surrounding;
 private Pool pool;
 public SwimmingPoolImpl(Surrounding surrounding, Pool pool) {
   this.surrounding = surrounding;
   this.pool = pool;
 }

 public double getOuterArea() {
   return surrounding.getArea() - pool.getArea();
 }

 public Surrounding getSurrounding() { return surrounding; }
 public Pool getPool() { return pool; }
}
想象一下,我们有一个由几十个不同游泳池和周围环境组成的图书馆,我们想创建一个自定义游泳池:

SwimmingPool customPool = new SwimmingPoolImpl(new OvalSurrounding(), new StarShapedPool());
double pavementArea =  customPool.getOuterArea();

就这样。现在,我们可以维护不同游泳池和外部区域的库,并立即使用它们创建游泳池,而无需更改游泳池的实现。这就是组合的好处。

问题中已经给出了设计:一个游泳池周围有一个游泳池,里面有一个游泳池。该任务要求组合,而不是继承

有了这个设计,我们可以发明其他的游泳池形状和周围环境,仍然可以拥有游泳池

public interface SwimmingPool {
  Surrounding getSurrounding();
  Pool getPool();
  double getOuterArea();
}

public interface Surrounding extends Shape{
  // some methods for surroundings
}

public interface Pool extends Shape {
  // some methods for pools
}

public interface Shape {
   double getArea();
}
接下来,将为矩形和圆形游泳池创建具体类,并使用这些模块设计游泳池


现在,我们创建一个具体的游泳池类:

public SwimmingPoolImpl implements SwimmingPool {
 private Surrounding surrounding;
 private Pool pool;
 public SwimmingPoolImpl(Surrounding surrounding, Pool pool) {
   this.surrounding = surrounding;
   this.pool = pool;
 }

 public double getOuterArea() {
   return surrounding.getArea() - pool.getArea();
 }

 public Surrounding getSurrounding() { return surrounding; }
 public Pool getPool() { return pool; }
}
想象一下,我们有一个由几十个不同游泳池和周围环境组成的图书馆,我们想创建一个自定义游泳池:

SwimmingPool customPool = new SwimmingPoolImpl(new OvalSurrounding(), new StarShapedPool());
double pavementArea =  customPool.getOuterArea();

就这样。现在,我们可以维护不同游泳池和外部区域的库,并立即使用它们创建游泳池,而无需更改游泳池的实现。这就是合成的好处。

感谢用户1180720,“为每种可能的形式创建一个具体的子类”是指外部形状还是池?可以有外部和内部形状的所有可能组合。如何管理这些组合?感谢用户1180720,“为每个可能的形式创建一个具体的子类”意味着外部形状或池?可以有外部和内部形状的所有可能组合。如何管理这些组合?谢谢Andreas_D,请多指导一点。什么是两个类,哪个类将组成另一个类?如果有更多的形状出现,可以有所有的形状组合,如何处理?如何告诉Methode什么是外部形状,什么是内部形状?(初始标记集包含Java,因此我选择Java来可视化这个概念)感谢Andreas_D,Pool和Surounding是类还是枚举?在代码的第一块中,请解释围绕GetAround()的内容;池getPool();谢谢,请接受我的提问。请忽略我的无知。您的第一个代码块与第二个代码块是否准确?谢谢
Pool
周围的
都是接口。想象一下,一些
类ovalsurround实现围绕着
。我已经纠正了这个例子,忘记了实现接口方法。谢谢Andreas\D