设计模式

发布于 2024-11-03 06:14:41 字数 827 浏览 1 评论 0原文

我试图理解设计模式(特别是观察者模式)。我正在尝试为以下场景(特别是第二段)创建一个设计模式。我已经包含了迄今为止已成功生成的 UML 图的图像。人们可以建议它是否正确/错误/充分/不足,或者给我任何有帮助的提示吗?我尝试使用观察者模式来对系统进行建模 - 是否有任何其他模式可用于对这种情况进行建模?

考虑设计一个支持航班预订和航班状态的系统 航空公司警报。系统集中保存注册信息 客户并控制客户对信息的访问。一位顾客 维护一个描述一些基本信息的个人资料,包括姓名、国家/地区 居住地、性别、出生日期、电子邮件地址和手机号码。客户可以 输入城市名称或机场搜索航空公司的往返航班 出发地和目的地的代码。找到可以接受的行程后 航班,客户可以购买经济舱、公务舱或 头等舱。完成购买后,顾客可以在座位上选择座位 已付费服务等级的所选航班。系统将发出有关以下内容的警报 飞往客户提醒地址的航班,该地址可以是电子邮件地址和/或 手机号码,取决于客户的选择。警报可能表明 航班延误、航班取消或航班状态发生其他变化 可能会在系统的未来版本中引入。

在某些时候,航班可以通过以下方式购买: 指定的航班日期。客户可以购买航班 np 的座位 航班日期前一周;在此日期之后,航班将关闭以进一步 座位购买。此外,一旦航班可供购买, 在航班起飞前一天,其状态为准点,之后可以 因天气延误而延误,因天气延误而取消 决定取消航班,并在成功完成航班后着陆 航班。航班取消或降落后即不再存在。

https://i.sstatic.net/YB9lJ.jpg

I am trying to understand design patterns (the Observer pattern in particular). I am trying to create a design pattern for the below scenario (specifically the seconnd paragraph). I have included an image of the UML diagram I have managed to produce so far. 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?

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.

https://i.sstatic.net/YB9lJ.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2024-11-10 06:14:41

观察者模式应该没问题。唯一的评论是您提供的图像不完整。

  1. 尽量避免之间的直接耦合
    观察者和主体。
  2. 相反,使用可以管理所有观察者的类。
  3. 这为您的设计提供了具有多个主题的灵活性
    类发布相同的事件。在
    未来如果你有新的科目
    那么你不需要做任何改变
    我客户端。

当然这取决于你的需要。

Subject1 -----                                                             ---- Client1
Subject2 ----- ISubject------  Observer implements IObserver  --- IClient  ---- Client2
                                                                           ---- Client3
  1. 观察者维护 IClient 列表和
    订阅主题1、主题2...
    等等。
  2. 任何一个科目都可以
    将通知连同
    状态(主体)参考观察者
    遍历 IClient 列表并
    通知每个客户。
  3. 这是
    当相同的通知可以时很有用
    发生于多个主题。
    示例:文件夹重命名使用完成
    缓慢双击或按 F2
    窗口上下文

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.

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.
  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.
  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
白昼 2024-11-10 06:14:41

与 Splendor 不同,我不会使用管理所有观察者的第三类。

  1. 它增加了复杂性。
  2. 对其他类型的对象重用事件很可能会导致违反里氏替换原则。

但是,如果您有一个 Car 类,则可以派生它 Volvo : Car 并且您仍然可以与订阅者一起工作。

换句话说,你的设计很好。

请注意,我没有阅读您引用的那些冗长的文字。如果您需要更多帮助,请将文本分成您认为设计模式适用的部分。

Unlike Splendor I would not use a third class which manages all observers.

  1. It adds complexity.
  2. Reusing events for other types of objects will most likely result in violations of Liskovs Substitution Principle.

However, if you have a Car class, you can derive it Volvo : Car and you'll still be able to work with the subscribers.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文