在面向对象设计中处理类型指示器的最佳方法是什么? [英] What's the best way to handle type indicators in object-oriented design?

查看:107
本文介绍了在面向对象设计中处理类型指示器的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个商业模型,需要识别不同类型的车辆.例如,摩托车,轮船,游艇,农用设备,雪地车,重型卡车,轻型卡车,乘用车和其他".

We have a business model that requires, say, different types of vehicles to be identified. For example, motorcycle, boat, yacht, farm equipment, snowmobiles, heavy truck, light truck, passenger car, and "Other".

我认为有两种处理方法:创建一个Vehicle类,其中包含几个子类,例如船,卡车,汽车,并有一个枚举来指示实际类型.这将起作用,使您能够代表与某些通用子类共享某些通用功能的车辆.但是,它允许您使类的枚举数不匹配.

I think there are two ways to handle it: Create a Vehicle class with a few subclasses, say, boat, truck, car and have a single enum indicating the actual type. This would work, allowing you to represent vehicles that share some common features with common subclasses. But it allows you to have mismatched enums with the classes.

我认为处理该问题的传统方法是为每种类型都有一个单独的子类,这可以将汽车子类化,也可以将汽车的子类化.例如,重型和轻型卡车可能是卡车的子类;小船和游艇可能会把小船归为一类;雪地车可能会把车辆归类.问题是,它并不能真正代表其他人的概念.

I think the traditional way to handle it is to have a separate subclass for each type, which may subclass the vehicle, or a subclass of vehicle. For example, heavy and light truck might subclass truck; boat and yacht might subclass boat; snowmobile might subclass vehicle. The problem is, it doesn't really lend itself to representing the concept of other.

有人针对这种情况有任何模式或最佳实践吗?

Does anyone have any patterns or best-practices for these sorts of scenarios?

彼得

该对象的目的是传达关于车辆的信息,并能够以用户友好的方式显示该信息.例如,从数据库中提取数据并将其显示在要查看/编辑的屏幕上.

The purpose of the object is to convey information about the vehicle and to be able to display that information in a user-friendly way. For example, pull the data from a database and display it on a screen to be viewed/edited.

这导致了传统OO的另一缺点,即从面向实际类型的类型指示器倾斜,因为要显示类型,将需要执行某种实例化并将值转换为用户友好的值.

This leads to another downside to the traditional OO means that leans away from type indicators for actual types, in that to display the type one would need to perform some sort of instance-of and convert the value to something user-friendly.

(此外,澄清一下,我在说的是单个枚举,而不是每个子类的枚举.)

(Also, clarified I was talking about a single enum, not an enum per subclass.)

推荐答案

真实问题

两个事物"在分别属于自己的班级之前需要有什么区别?

How different do two "things" need to be before they deserve their own class?

答案

这完全取决于您将如何使用对象.

It depends entirely on how you are going to use the objects.

  • 您是否会根据它们具有的某些属性(例如,它会浮动吗?)进行比较?如果是这样,具有差异属性的单个类就很有意义.
  • 您要在界面上显示对象吗?在您的应用程序中在一张桌子上显示轻型卡车和重型卡车是否有意义?如果它们不能有意义地存在并显示为一个数据集,则有两个类是有意义的.

那只是两个例子.关键是,如果您创建一个新类,它应该是有用的. Java特别容易过于抽象.

Those are just two examples. The point is, if you create a new class, it should be useful. Java is particularly prone to over-abstraction.

关于Other,我该怎么办?

What do I do about Other?

其他声音听起来像不是卡车而不是汽车的一组车辆.听起来您想赶上所有其他车辆并将它们归为一类.为什么不使用Vehicle类?不要创建从Vehicle派生但添加零功能的其他类.相同的有用性测试在这里适用.

Other sounds like a group of Vehicles that are not Trucks and not Cars. It sounds like you want to catch all other Vehicles and put them in one class. Why not use the Vehicle class? Don't create another class that derives from Vehicle but adds zero functionality. The same usefulness test applies here.

  • Vehicle是否足以满足我对所有其他"车辆的需求?如果没有,我需要创建更多的类.
  • Is a Vehicle useful enough to satisfy my needs for all "other" vehicles? If not, I need to create more classes.

我听取了您的建议,并创建了两个有用的类.现在,我无法将它们分开,并且我拒绝使用instanceof.我该怎么办?

无论您的类有多抽象,我始终可以将您的类的实例添加到List<Object>中,然后将它们区别开来.您无法防弹设计.

No matter how abstracted your classes are, I will always be able to add instances your class to a List<Object> and will then be unable to tell them apart. You cannot bulletproof your design.

让我换一种说法-如果您将鸡蛋和汽车"添加到列表中,而以后又需要区分鸡蛋和汽车",那么您的列表就会出现问题,而不是鸡蛋和汽车.

Let me put it another way - if you add Eggs and Cars to a List, and you later need to differentiate between Eggs and Cars, then there is a problem with your List, not the Eggs and Cars.

这篇关于在面向对象设计中处理类型指示器的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆