1 / 17

COSC3311 – Software Design

COSC3311 – Software Design. Once Routines, Hockey Pucks & The Singleton Pattern. Instance Sharing – SOOSA p81-83. Very often groups of related objects share a common supplier a set of diskless workstations may all share a common file sharer In a hockey game, all players share the same puck.

Download Presentation

COSC3311 – Software Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC3311 – Software Design Once Routines, Hockey Pucks &The Singleton Pattern Object Oriented Software Construction 22/03/2009 11:51 PM0

  2. Instance Sharing – SOOSA p81-83 • Very often groups of related objects share a common supplier • a set of diskless workstations may all share a common file sharer • In a hockey game, all players share the same puck Object Oriented Software Construction 22/03/2009 11:51 PM1

  3. Non-sharing version Object Oriented Software Construction 22/03/2009 11:51 PM2

  4. Sharing version Object Oriented Software Construction 22/03/2009 11:51 PM3

  5. Non-sharing class HOCKEY_PLAYER create make feature puck: PUCK name: STRING make (s:STRING) is -- hockey player do create puck.make(s) name := s io.put_string(puck.name) end end Object Oriented Software Construction 22/03/2009 11:51 PM4

  6. Sharing class HOCKEY_PLAYER2 create make feature puck: PUCK2 is -- shared puck once create Result.make(name) end name: STRING make (s:STRING) is -- hockey player do name := s io.put_string(puck.name) io.new_line end end Object Oriented Software Construction 22/03/2009 11:51 PM5

  7. Complete run-time state Object Oriented Software Construction 22/03/2009 11:51 PM6

  8. Patterns • Designing good OO software products is hard – you must find the right classes and establish key relationships between them at the right level of granularity. • Certain problems occur over and over again. • A design pattern describes a recurring problem and then describes a re-usable core solution. • Available in Steacie Library • Design Patterns, Gamma, Helm, Johnson and Vlissides, Addison Wesley, 1995. • (Eiffel) Design Patterns and Contracts http://www.irisa.fr/prive/jezequel/DesignPatterns/ Object Oriented Software Construction 22/03/2009 11:51 PM7

  9. Singleton Pattern (creational) • Problem – shared data • Solution – Ensure a class that encapsulates the shared data has only one instance and provide a global point of access to it. Object Oriented Software Construction 22/03/2009 11:51 PM8

  10. Banking system Object Oriented Software Construction 22/03/2009 11:51 PM9

  11. Need for shared data Shared data such as interests rates and minimum balance … Too many inheritance links?? Object Oriented Software Construction 22/03/2009 11:51 PM10

  12. Alternative – Singleton pattern Object Oriented Software Construction 22/03/2009 11:51 PM11

  13. SHARED_DATA class SHARED_DATA feature minimum_savings_balance: INTEGER set_minimum_savings_balance (b: INTEGER) is do minimum_savings_balance := b end prime_interest_rate: REAL is 2.0 feature {NONE} singleton: SHARED_DATA is once Result := Current end invariant only_one_instance: Current = singleton end Object Oriented Software Construction 22/03/2009 11:51 PM12

  14. ACCESS_DATA class ACCESS_DATA feature data: SHARED_DATA is -- access to unique instance of shared data once create Result end feature {NONE} -- invariant implementation is_real_singleton: BOOLEAN is -- multiple calls to `data' returns same instance do Result := data = data end invariant is_real_singleton end Object Oriented Software Construction 22/03/2009 11:51 PM13

  15. ACCOUNT class ACCOUNT create make feature data: SHARED_DATA make (initial_amount: INTEGER) is -- Set up account with `initial_amount'. local ad: ACCESS_DATA do create ad; data := ad.data create deposits.make; create withdrawals.make deposits.extend (create {DEPOSIT}.make (initial_amount)) end balance: INTEGER is do Result := deposits.total - withdrawals.total end minimum_balance: INTEGER is do Result := data.minimum_savings_balance end … end Object Oriented Software Construction 22/03/2009 11:51 PM14

  16. Unit Test test_account_deposit_and_withdraw: BOOLEAN is -- Use a bank account. local an_account: ACCOUNT data: SHARED_DATA access_data: ACCESS_DATA do comment("test_account_deposit_and_withdraw") create access_data data := access_data.data data.set_minimum_savings_balance (999) create an_account.make (2000) Result := an_account.minimum_balance = 999 check Result end an_account.deposit (500) if an_account.may_withdraw (1500) then an_account.withdraw (1500) end Result := an_account.balance = 1000 end Object Oriented Software Construction 22/03/2009 11:51 PM15

  17. Demo • Bank2.zip Object Oriented Software Construction 22/03/2009 11:51 PM16

More Related