这个小程序的设计?

haa*_*nsi 1 architecture ooad

在此输入图像描述请不要介意我的问题,因为它有点理论化.

上周我接受了一份工作面试,在那里我接到了一份工作,后来他们提问并且我的设计有问题.所以我无法得到这份工作.我正在分享我的设计.请看看它,并建议我在哪里错了,什么解决方案将是一个很好的设计.

问题 这是一个建造游泳池的建筑公司的包裹.游泳池有2个游泳池周边区域和游泳池.两个(泳池和周围区域)可以是矩形或圆形(请参见附图),因此将有4种可能的形状.程序应计算阴影区域(池到混凝土的外部区域).

****计算矩形区域**的公式**是长度*宽度*2 计算圆形区域的公式是2*2.1718

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

问题 如果除了矩形和圆形之外还有更多形状,该怎么办?如果还有其他100种形状,那么什么解决方案将考虑未来的招股说明书?在这里,我的设计并不好,因为当需要改变新的形状时,它需要改变界面.

And*_*s_D 5

设计已经在问题中给出:游泳池有一个周围,​​它有一个内池.该任务需要组合而不是继承.

通过这种设计,我们可以发明其他游泳池形状和周围环境,并仍然有游泳池

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();
}
Run Code Online (Sandbox Code Playgroud)

接下来,您将为矩形和圆形池创建具体类,并使用这些模块设计游泳池.


现在,我们创建一个混凝土游泳池类:

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; }
}
Run Code Online (Sandbox Code Playgroud)

想象一下,我们有一个由数十个不同的游泳池和周围环境组成的图书馆,我们想创建一个自定义游泳池:

SwimmingPool customPool = new SwimmingPoolImpl(new OvalSurrounding(), new StarShapedPool());
double pavementArea =  customPool.getOuterArea();
Run Code Online (Sandbox Code Playgroud)

而已.现在我们可以维护一个不同游泳池和外围区域的图书馆,并立即使用它们创建游泳池,而无需改变游泳池的实施.这是作文的好处.