240 likes | 482 Views
Ch 17: Use Case Realizations with GRASP Patterns Assigning responsibilities to objects to achieve user goals. Section 17.4 makeNewSale Section 17.5 enterItem Section 17.6 endSale Section 17.7 makePayment Section 17.8 Start Up. You should review these sections.
E N D
Ch 17: Use Case Realizations with GRASP Patterns Assigning responsibilities to objects to achieve user goals Section 17.4 makeNewSale Section 17.5 enterItem Section 17.6 endSale Section 17.7 makePayment Section 17.8 Start Up You should review these sections 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Who/What is going to be responsible for : • Handling the system operation makeNewSale? • Creating a Sale? • Who/What should create the container of SalesLineItems? • Handling the System operation enterItem? • Creating a Saleslineitem? • Finding a match for a product specification? For doing a lookup? • Handling the system operation endSale? • Setting the isComplete attribute of Sale to true? • Calculating the sale total? • Calculating the SalesLineItem subtotal? • Providing the ProductSpecification price? • Handling the system operation makePayment? • Creating a Payment instance? • ………. Which GRASP pattern justified the decision? 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Section 17.4 makeNewSale • 1st concern: who/what is going to be responsible for handling the system operation makeNewSale? • Decision: Using the Controller Pattern • … since there aren’t very many system operations and since Register (in our domain) represents the overall system, we choose Register as a “façade” controller. 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns By “Controller”, Register is our “façade” controller. :Register makeNewSale() 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Page 314 public class Register { … public makeNewSale() { … } We have just decided that Register must have a method makeNewSale 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • 2nd concern: who/what is going to be responsible for creating a Sale? • Should Register delegate the responsibility or …? • Since Register (in the domain) actually records a Sale then, by “Creator”, we decide that Register will do this. Register has the data and it needs to keep track of a Sale, so … 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns By “Creator”, Register creates a Sale. :Register makeNewSale() create() :Sale 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Page 314 public class Register { … Private Sale sale; … public makeNewSale() { sale = new Sale(); } The method makeNewSale creates the sale object 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • 3rd concern: Sale needs to know of its SalesLineItems. A container for these is required. • Who/What should create this? • Since Sale will contain the lines, by “Creator”, we decide that Sale will do this… 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns By “Creator”, Register creates a Sale. :Register makeNewSale() create() :Sale : create() :SalesLineItem 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns The constructor for Sale creates the container for the line items Page 315 public class Sale { private List lineItems = new ArrayList(); private Date date = new Date(); private boolean isComplete = false; private Payment payment; … 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Section 17.5 enterItem • 1st concern: who/what is going to be responsible for handling the system operation enterItem? • We continue using the Controller Pattern … Register is responsible for handling enterItem. 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Contract for enteritem specifies • Preconditions: • A Sale is underway • Postconditions: • salesLineItem is created • It is associated with the current Sale • Its quantity is set • It is associated with a ProductSpecification By Creator, Sale can do this Sale stores the new sales line item in its collection The product specification will need to be found 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Message 2 … see later slide enterItem() 2:makeLineItem() :Register :Sale 2.1:create() 2.2:add() : SalesLineItem : :SalesLineItem 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Page 315 • public class Sale • { … • public void makeLineItem (…, …) • { • lineItems.add ( new SalesLineItem (…, …) ) ; • } The “create” message for a new sales line item The “add” message sent to the multiobject 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Page 316 • public class SalesLineItem • { private int quantity; • private ProductSpecification productSpec; • public void SalesLineItem (…spec, …quantity) • { • this.productSpec = spec ; • this.quantity = quantity • } The constructor for a sales line item arranges that the sales line item knows its product specification and its quantity 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns • Section 17.5 enterItem • 2nd concern: who/what is going to be responsible for finding a match for a product specification? For doing a lookup? • Who has knowledge of product specifications? Who is capable of doing a lookup? • Expert suggests the product catalogue is the best candidate. The product catalogue contains the product specifications … the product catalogue has the information, it is the expert. 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns enterItem() :Register 1:getSpecification() :ProductCatalog 1.1:find() : :ProductSpecification 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Collection of product specifications • Page 316 • public class ProductCatalog • { private Map productSpecifications = new HashMap(); • … • public ProductSpecification getSpecification (ItemID id) • { • Return( …. productSpecifications.get ( id ); • } Method to find a specific item Handles the “find” message 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns enterItem() 2:makeLineItem() :Register :Sale 1:getSpecification() 2.1:create() 2.2:add() :ProductCatalog :SalesLineItem 1.1:find() : :SalesLineItem : :ProductSpecification 91.3913 Ron McFadyen
Ch 17: Use Case Realizations with GRASP Patterns Given the previous collaboration, what methods/responsibilities have been assigned to the various classes? Register Sale ProductCatalog SalesLineItem 91.3913 Ron McFadyen