170 likes | 242 Views
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.
E N D
COSC3311 – Software Design Once Routines, Hockey Pucks &The Singleton Pattern Object Oriented Software Construction 22/03/2009 11:51 PM0
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
Non-sharing version Object Oriented Software Construction 22/03/2009 11:51 PM2
Sharing version Object Oriented Software Construction 22/03/2009 11:51 PM3
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
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
Complete run-time state Object Oriented Software Construction 22/03/2009 11:51 PM6
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
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
Banking system Object Oriented Software Construction 22/03/2009 11:51 PM9
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
Alternative – Singleton pattern Object Oriented Software Construction 22/03/2009 11:51 PM11
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
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
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
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
Demo • Bank2.zip Object Oriented Software Construction 22/03/2009 11:51 PM16