SOEN 343 Software Design Computer Science and Software
- Slides: 16
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin SOEN 343, © P. Chalin SOEN 344, © P. Chalin
Agenda – Lecture 11 a • GRASP interrelationships. • Dice Game Test class: a solution. • Go. F – (Simple) Factory – Singleton 3/4/2021 SOEN 343, © P. Chalin, 2
GRASP • • • Information Expert Creator High Cohesion Low Coupling Controller 3/4/2021 • • Polymorphism Pure Fabrication Indirection Protected Variations SOEN 343, © P. Chalin, 3
GRASP: Interrelationships 3/4/2021 SOEN 343, © P. Chalin, 4
Dice Game Test Class: A Solution • (Given in class) 3/4/2021 SOEN 343, © P. Chalin, 5
Gang Of Four • Gamma, Helm, Johnson, Vlissides 3/4/2021 SOEN 343, © P. Chalin, 6
Go. F Pattern Summary (& Relationhips) 3/4/2021 SOEN 343, © P. Chalin, 7
Design Issue: Ensure No More Than One Instance of a Class is Created • Given a class C, how can we ensure that only one instance of C is ever created? 3/4/2021 SOEN 343, © P. Chalin, 8
Converting C to a Singleton. 3/4/2021 SOEN 343, © P. Chalin, 9
Singleton Pattern Notice: • Constructor is no longer public. • To access the instance use get. Unique. Instance(). • All other attribute and method declarations of C stay the same. 3/4/2021 SOEN 343, © P. Chalin, 10
«Singleton» C. get. Unique. Instance() public static C get. Unique. Instance() { if(unique. Instance == null) { unique. Instance = new C(); } return unique. Instance; } 3/4/2021 SOEN 343, © P. Chalin, 11
Thread-safe Creation Method public static synchronized C get. Un…() { … } 3/4/2021 SOEN 343, © P. Chalin, 12
Singleton: Design Alternatives/Issues • Create a class with static attributes and methods. Trade-offs: consider how easy the following are: • Adapting the design so that x instances can be created (where x > 1). • Subclassing. • Passing the instance as a parameter. • … • (See Larman 05, Section 26. 5) 3/4/2021 SOEN 343, © P. Chalin, 13
Singleton: Design Issues (Cont’d) • See Larman 05, Section 26. 5 for discussion of – Design trade-offs, including • eager/lazy evaluation of Singleton. unique. Instance see. 3/4/2021 SOEN 343, © P. Chalin, 14
Singleton & UML 3/4/2021 SOEN 343, © P. Chalin, 15
Singleton & UML 3/4/2021 SOEN 343, © P. Chalin, 16