简体   繁体   English

设计模式

[英]Design patterns

I am trying to understand design patterns (the Observer pattern in particular). 我试图了解设计模式(特别是Observer模式)。 I am trying to create a design pattern for the below scenario (specifically the seconnd paragraph). 我正在尝试为下面的场景创建一个设计模式(特别是seconnd段落)。 I have included an image of the UML diagram I have managed to produce so far. 我已经包含了我迄今为止设法生成的UML图的图像。 Could people please advise on whether it is correct/wrong/adequate/inadequate or give me any tips that would help? 人们可以告知是否正确/错误/充分/不足或给我任何有用的提示? I have tried to use the Observer pattern to model the system - are there any additional patterns that could be used to model this scenario? 我曾尝试使用Observer模式对系统进行建模 - 是否有任何其他模式可用于对此方案进行建模?

Consider the design of a system to support flight reservations and flight status alerts for an airline. 考虑设计一个系统来支持航空公司的航班预订和航班状态警报。 The system centrally stores information about registered customers and controls customers' access to the information. 系统集中存储有关已注册客户的信息,并控制客户对信息的访问。 A customer maintains a profile describing some basic information including name, country of residence, gender, birth date, email address and mobile number. 客户维护一份简介,描述一些基本信息,包括姓名,居住国家,性别,出生日期,电子邮件地址和手机号码。 A customer can search for round-trip flights on the airline by entering the city name or airport code for the origin and destination. 客户可以通过输入起点和终点的城市名称或机场代码来搜索航空公司的往返航班。 Upon finding an itinerary of acceptable flights, a customer can purchase the flights in economy class, business class or first class. 在找到可接受航班的行程后,客户可以购买经济舱,商务舱或头等舱的航班。 Upon completing the purchase, a customer can select seats on the chosen flights in the class of service paid for. 完成购买后,客户可以在所支付的服务等级中选择所选航班上的座位。 The system will deliver alerts about the flights to the customer's alert address, which can be the email address and/or mobile number, depending on the customer's choice. 系统将根据客户的选择向客户的警报地址发送有关航班的警报,该警报地址可以是电子邮件地址和/或手机号码。 An alert may indicate a delay to a flight, a cancellation to a flight, or some other change to flight status that may be introduced in future versions of the system. 警报可以指示可能在系统的未来版本中引入的航班延误,航班取消或航班状态的一些其他改变。

At some point a flight becomes available for purchase with a specified flight date. 在某些时候,航班可以在指定的航班日期前购买。 A customer can purchase a seat on the flight np to one week before the flight date; 客户可以在航班日前一周购买航班上的座位; after this date the flight is closed to further seat purchases. 在此日期之后,航班将关闭以进一步购买座位。 In addition, once a flight becomes available for purchase, its status is on-time until one day before the flight, after which it can become delayed upon occurrence of a weather delay, cancelled upon a decision to cancel the flight, and landed upon successful completion of the flight. 此外,一旦航班可以购买,其状态将在航班起飞前一天准时到达,之后可能会在天气延迟发生时延迟,在取消航班时取消,并在成功时降落完成飞行。 A flight ceases to exist after it becomes cancelled or landed. 航班取消或降落后不再存在。

http://i.stack.imgur.com/YB9lJ.jpg http://i.stack.imgur.com/YB9lJ.jpg

Observer pattern should be fine. 观察者模式应该没问题。 Only comment is the image that you have provided is not complete. 只有评论是您提供的图像不完整。

  1. Try avoiding direct coupling between observer and subject. 尽量避免观察者和主体之间的直接耦合。
  2. Instead use a class which can manage all the observers. 而是使用一个可以管理所有观察者的类。
  3. This provides flexibility to your design to have multiple subject classes publishing same event. 这为您的设计提供了灵活性,使多个主题类可以发布相同的事件。 In future if you have a new subject then you need not make any change to IClient. 将来,如果您有新主题,则无需对IClient进行任何更改。

Ofcourse this depends on your need. 当然这取决于你的需要。

Subject1 -----                                                             ---- Client1
Subject2 ----- ISubject------  Observer implements IObserver  --- IClient  ---- Client2
                                                                           ---- Client3
  1. Observer maintains list of IClient and subscribes to Subject1, subject2... and so on. Observer维护IClient列表并订阅Subject1,subject2 ......等等。
  2. Any of the subject can send the notification along with the state (subject) reference Observer goes through the list of IClient and notifies each of the client. 任何主题都可以发送通知以及状态(主题)引用Observer遍历IClient列表并通知每个客户端。
  3. This is useful when same notification can occur from more than one subject. 当多个主题发生相同的通知时,这很有用。 Example: Folder rename done using slow double click or press of F2 in windows context 示例:使用慢速双击或在Windows上下文中按F2完成文件夹重命名

Unlike Splendor I would not use a third class which manages all observers. 与Splendor不同,我不会使用管理所有观察者的第三类。

  1. It adds complexity. 它增加了复杂性。
  2. Reusing events for other types of objects will most likely result in violations of Liskovs Substitution Principle. 重复使用其他类型对象的事件很可能会导致违反Liskovs替换原则。

However, if you have a Car class, you can derive it Volvo : Car and you'll still be able to work with the subscribers. 但是,如果你有一个Car类,你可以推导出它Volvo : Car ,你仍然可以与订阅者合作。

Your design is in other words fine. 换句话说,你的设计很好。

Note that I didn't read that bloat of text that you quoted. 请注意,我没有读过您引用的那段文字。 If you need more help, break the text into parts where you think that a design pattern is applicable. 如果您需要更多帮助,请将文本分成您认为设计模式适用的部分。

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

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