cosc3311 – software design

17
Department of Computer Science, York University Object Oriented Software Construction 22-6-20 04:33 PM 1 COSC3311 – Software Design Once Routines, Hockey Pucks & The Singleton Pattern

Upload: demetrius-price

Post on 30-Dec-2015

22 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 1

COSC3311 – Software Design

Once Routines, Hockey Pucks &The Singleton Pattern

Page 2: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 2

Instance Sharing – SOOSA p81-83

Very often groups of related objects share a common suppliero a set of diskless workstations may all share a

common file sharero In a hockey game, all players share the same puck

Page 3: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 3

Non-sharing version

HOCKEY_PLAYER PUCK

HOCKEY_PLAYER

HOCKEY_PLAYER

PUCK

PUCK

Game

Page 4: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 4

Sharing version

HOCKEY_PLAYER PUCK1

HOCKEY_PLAYER

HOCKEY_PLAYER

PUCK

Game

Page 5: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 5

Non-sharing

class HOCKEY_PLAYER createmake

featurepuck: PUCK

name: STRING

make (s:STRING) is-- hockey player

docreate puck.make(s)name := sio.put_string(puck.name)

endend

Page 6: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 6

Sharing

class HOCKEY_PLAYER2 createmake

featurepuck: PUCK2 is

-- shared puckonce

create Result.make(name)end

name: STRING

make (s:STRING) is-- hockey player

doname := sio.put_string(puck.name)io.new_line

endend

Page 7: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 7

Complete run-time state

Page 8: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 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 Libraryo Design Patterns, Gamma, Helm, Johnson and

Vlissides, Addison Wesley, 1995.o (Eiffel) Design Patterns and Contracts

http://www.irisa.fr/prive/jezequel/DesignPatterns/

Page 9: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 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.

Page 10: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 10

Banking system

Page 11: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 11

Need for shared data

Too many inheritance links??

Shared data such as interests rates and minimum balance …

Page 12: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 12

Alternative – Singleton pattern

Page 13: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 13

SHARED_DATA

class SHARED_DATA featureminimum_savings_balance: INTEGER

set_minimum_savings_balance (b: INTEGER) isdo

minimum_savings_balance := bend

prime_interest_rate: REAL is 2.0

feature {NONE}singleton: SHARED_DATA is

onceResult := Current

endinvariant

only_one_instance: Current = singletonend

Page 14: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 14

ACCESS_DATA

class ACCESS_DATA feature

data: SHARED_DATA is-- access to unique instance of shared data

oncecreate Result

end

feature {NONE} -- invariant implementationis_real_singleton: BOOLEAN is

-- multiple calls to `data' returns same instancedo

Result := data = dataend

invariantis_real_singleton

end

Page 15: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 15

ACCOUNT

class ACCOUNT create make

featuredata: SHARED_DATA

make (initial_amount: INTEGER) is-- Set up account with `initial_amount'.

localad: ACCESS_DATA

docreate ad; data := ad.datacreate deposits.make; create withdrawals.makedeposits.extend (create {DEPOSIT}.make (initial_amount))

end

balance: INTEGER isdo

Result := deposits.total - withdrawals.totalend

minimum_balance: INTEGER isdo

Result := data.minimum_savings_balanceend

end

Page 16: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 16

Unit Test

test_account_deposit_and_withdraw: BOOLEAN is-- Use a bank account.

localan_account: ACCOUNTdata: SHARED_DATAaccess_data: ACCESS_DATA

docomment("test_account_deposit_and_withdraw")create access_datadata := access_data.datadata.set_minimum_savings_balance (999)create an_account.make (2000)Result := an_account.minimum_balance = 999check Result endan_account.deposit (500)if an_account.may_withdraw (1500) then

an_account.withdraw (1500)endResult := an_account.balance = 1000

end

Page 17: COSC3311 – Software Design

Department of Computer Science, York University Object Oriented Software Construction 2023-04-19 04:37 PM 17

Demo

Bank2.zip