繁体   English   中英

正确设计Java类

[英]Proper design of Java Classes

我的问题是我应该做counterbore (一凹孔的板)的财产plate还是应该counterbore是一个属性joint

我为工程分析程序提供以下课程:

Plate
Bolt
Washer
Nut 
Material
Coating

然后,我有代表各种关节的课程

TappedJoint (a joint were the bolt is threaded into the bottom plate) will have:
  Plate topPlate
  Plate bottomPlate
  Bolt bolt
  Washer topWasher

BoltedJoint
  Plate topPlate
  Plate bottomPlate
  Bolt bolt
  Nut nut
  Washer topWasher
  Washer bottomWasher

沉孔仅适用于topPlate但我需要验证用户输入的板厚度大于沉孔的深度。 我是否只是在bottomPlate中将沉孔设置为null,还是将沉孔属性放入联合类中更好? 还是我应该使用其他模式,例如子类?

我将涂层和材料作为属性添加到每个零件,因为添加到接缝将太冗长,即:

  BoltedJoint
    Plate topPlate
    Plate bottomPlate    
    Coating topPlateTopSurface
    Coating topPlateBottomSurface
    Coating bottomPlateTopSurface ...etc 

我可能可以使它适用于任何一种情况,但也许它们是更好的设计?

我认为,如果将counterbore作为Joint属性,那么PlateBolt都可以访问特定于每个Joint的属性。 这样,螺栓无需访问Plate对象即可找到孔。

如果我正确地理解了上下文,则counterBoreJoint对其topPlate要求。 在这种情况下,它应该是Joint的属性(可能取自Bolt或其他指定的东西),并且如果其厚度不足, Joint应该拒绝topPlate

如果counterBorePlate的物理属性(即Plate实际上具有钻孔的沉孔),则它也应该是Plate的属性。 但是Joint仍然需要它作为其topPlate的要求。

希望有道理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM