简体   繁体   English

构建此复杂对象的设计模式是什么

[英]what design pattern for building this complex object

I have to create an itinerary object which is essentially made of other components. 我必须创建一个基本上由其他组件组成的路线对象。

These components need to be added in a particular order. 这些组件需要按特定顺序添加。 I need to make sure when things are added out of order, an exception/error needs to be thrown. 我需要确保在添加乱序时,需要引发异常/错误。

Quick walkthrough: 快速演练:

Itinerary Build-itinerary():

- AddSegment()
- AddBaggagePolicy()
- AddMisc()

Segment AddSegment(...)

- add departure airport
- add arrival airport
- add departure time
- add arrival time
- add duration (total duration)
- add airline

where airport and airline are object types. 其中机场和航空公司是对象类型。

Once segments are done, I need to consolidate the segments to yield starting departure to final arrival destination (since there can be multiple hop points from one point to other). 分段完成后,我需要合并分段以产生开始出发点到最终到达目的地的位置(因为从一个点到另一个点可能有多个跳跃点)。

What kind of a pattern can I refer to building this itinerary ? 我可以参考哪种模式来建立此行程?

Looks like it isn't specific enough. 看起来不够具体。 At first glance, I might characterise this as a "builder pattern". 乍一看,我可以将其描述为“构建者模式”。 Many (but not all!) design patterns in Java are workarounds for lack of language features. Java中的许多(但不是全部!)设计模式都是缺乏语言功能的解决方法。

In fact, your design there is a code smell, you should not have an apparently arbitrary ordered sequence of steps to construct an object. 实际上,您的设计中有代码的味道,您不应构造任何明显的任意顺序的步骤来构造对象。 Instead, define the data, and pass that into the object. 而是定义数据,并将其传递给对象。

If you must build your object like this, then I would ensure that any code that does this is unit tested using a mock that supports the record and playback style of mocking. 如果必须像这样构建对象,那么我将确保使用支持模拟记录和回放样式的模拟对所有执行此操作的代码进行单元测试。 (So-called "strict mocks". (所谓的“严格模拟”。

And I am not good with design patterns at all: I would start with a Builder Pattern and try to validate each component if it has been added or not. 而且我对设计模式根本不满意:我将从构建器模式开始,然后尝试验证每个组件是否已添加。 At least this would be my start point : a Builder or a private static inner class. 至少这是我的出发点:Builder或私有静态内部类。

Try using the Builder Pattern. 尝试使用构建器模式。 Also you could look for other variants in the Creational Patterns category. 您也可以在“创建模式”类别中查找其他变体。

If you're new to design patterns then take a look at "Head First Design Patterns" - its in google books (I dont know if complete or preview though...) 如果您不熟悉设计模式,请查看“ Head First设计模式”-它在Google图书中的显示(我不知道它是完整的还是预览...)

have fun :) 玩得开心 :)

When I hear the words "building" and "order" then first design pattern that comes to mind is Builder. 当我听到“建筑物”和“订单”这两个词时,想到的第一个设计模式就是“建造者”。 If you new to Design Patterns then I would recommend you to read "Applied Java Patterns". 如果您不熟悉设计模式,那么我建议您阅读“应用的Java模式”。

Cannot agree with @Arafangion, I belive that ordering of some procedure invocations is sometimes necessary. 我不同意@Arafangion,我相信有时需要对某些过程调用进行排序。 I would recommend looking into a Factory Method design pattern, from what I remember there is a nice example in Head First Design Patterns book when preparing pizza stuff :) 我建议您研究一下Factory Method设计模式,据我记得,在准备披萨时, Head First Design Patterns书中有一个很好的例子:)

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

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