...

Use Case Realizations with GRASP Patterns makeNewSale

by user

on
68

views

Report

Comments

Transcript

Use Case Realizations with GRASP Patterns makeNewSale
Use Case Realizations with GRASP Patterns
“The assignment of responsibilities and design of collaborations
are very important and creative steps during design”
“there is no ‘magic’ or unjustifiable decisions in object design”
“A use-case realization describes how a particular use case is
realized within the design model, in terms of collaborating
objects”
makeNewSale
enterItem
You should review
these sections
endSale
makePayment
startUp
Feb 2005
92.3913
Ron McFadyen
1
Ch 17: Use Case Realizations with GRASP Patterns
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.
Feb 2005
92.3913
Ron McFadyen
2
Use Case Realizations with GRASP Patterns
By “Controller”, Register is our “façade” controller.
:Register
makeNewSale()
Feb 2005
92.3913
Ron McFadyen
3
Use Case Realizations with GRASP Patterns
Here, we are looking ahead to some code written to support
the design
We have just decided that Register must have a
method makeNewSale
public class Register
…
{
Register
public makeNewSale()
makeNewSale
…
{
…
}
Feb 2005
92.3913
Ron McFadyen
4
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 …
Feb 2005
92.3913
Ron McFadyen
5
Use Case Realizations with GRASP Patterns
By “Creator”, Register creates a Sale.
:Register
makeNewSale()
create()
Feb 2005
:Sale
92.3913
Ron McFadyen
6
Use Case Realizations with GRASP Patterns
Here, we are looking ahead to some code written to support
the design
The method makeNewSale creates the sale object
public class Register
…
{
Private Sale sale;
…
public makeNewSale()
{
sale = new Sale();
Feb 2005
}
92.3913
Ron McFadyen
7
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…
Feb 2005
92.3913
Ron McFadyen
8
Use Case Realizations with GRASP Patterns
By “Creator”, Register creates a Sale.
:Register
makeNewSale()
create()
:Sale
create()
Feb 2005
92.3913
:
:SalesLineItem
Ron McFadyen
9
Use Case Realizations with GRASP Patterns
By “Creator”, Register creates a Sale.
3rd edition
:Register
makeNewSale()
create()
:Sale
create()
Feb 2005
92.3913
lineItems:
List<SalesLineItems>
Ron McFadyen
10
Use Case Realizations with GRASP Patterns
The constructor for Sale creates the container for the line items.
Sale contains a list of line items
public class Sale
{
private List lineItems = new ArrayList();
private Date date = new Date();
private boolean isComplete = false;
private Payment payment;
…
Feb 2005
92.3913
Ron McFadyen
11
Use Case Realizations with GRASP Patterns
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.
Feb 2005
92.3913
Ron McFadyen
12
Use Case Realizations with GRASP Patterns
Contract for enteritem specifies
Preconditions:
•A Sale is underway
Postconditions:
•salesLineItem is created
By Creator, Sale can do this
•It is associated with the current Sale
Sale stores the new sales line
item in its collection
•Its quantity is set
•It is associated with a ProductSpecification
Feb 2005
92.3913
Ron McFadyen
The product specification will
need to be found
13
Use Case Realizations with GRASP Patterns
Message 2 … see later slide
enterItem()
:Register
2:makeLineItem()
:Sale
2.1:create()
2.2:add()
: SalesLineItem
:
:SalesLineItem
Feb 2005
92.3913
Ron McFadyen
14
Use Case Realizations with GRASP Patterns
Message 2 … see later slide
enterItem()
:Register
2:makeLineItem()
:Sale
2.1:create()
2.2:add()
: SalesLineItem
3rd edition
Feb 2005
lineItems:
List<SalesLineItems>
92.3913
Ron McFadyen
15
Use Case Realizations with GRASP Patterns
public class Sale
{
…
public void makeLineItem (…, …)
{
lineItems.add ( new SalesLineItem (…, …) ) ;
}
The “create” message for a new sales line item
Feb 2005
The “add” message sent to the multiobject is handled within Sale. Sale has
a list of line items 92.3913 Ron McFadyen
16
Use Case Realizations with GRASP Patterns
public class SalesLineItem
{
private int quantity;
private ProductSpecification productSpec;
public void SalesLineItem (…spec, …quantity)
{
this.productSpec = spec ;
this.quantity = quantity
Feb 2005
}
92.3913
Ron McFadyen
The constructor for a
sales line item
arranges that the sales
line item knows its
product specification
and its quantity
17
Use Case Realizations with GRASP Patterns
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.
Feb 2005
92.3913
Ron McFadyen
18
Use Case Realizations with GRASP Patterns
enterItem()
:Register
1:getSpecification()
:ProductCatalog
1.1:find()
:
:ProductSpecification
Feb 2005
92.3913
Ron McFadyen
19
Use Case Realizations with GRASP Patterns
enterItem()
:Register
1:getSpecification()
:ProductCatalog
1.1:get()
3rd edition
:
Map<ProductSpecifications>
Feb 2005
92.3913
Ron McFadyen
20
Use Case Realizations with GRASP Patterns
Collection of product specifications
public class ProductCatalog
{
private Map productSpecifications = new HashMap();
…
public ProductSpecification getSpecification (ItemID id)
{
Method to find a specific item
Return( …. productSpecifications.get ( id );
}
Feb 2005
Handles the “find” message
92.3913
Ron McFadyen
21
Use Case Realizations with GRASP Patterns
enterItem()
:Register
2:makeLineItem()
:Sale
1:getSpecification()
2.1:create()
2.2:add()
:ProductCatalog
:SalesLineItem
1.1:find()
:
:SalesLineItem
:
:ProductSpecification
Feb 2005
92.3913
Ron McFadyen
22
Use Case Realizations with GRASP Patterns
3rd edition
enterItem()
:Register
2:makeLineItem()
:Sale
1:getSpecification()
2.1:create()
2.2:add()
:ProductCatalog
:SalesLineItem
1.1get()
:
Map<ProductSpecifications>
Feb 2005
92.3913
lineItems:
List<SalesLineItems>
Ron McFadyen
23
Use Case Realizations with GRASP Patterns
Given the previous collaboration, what methods/responsibilities
have been assigned to the various classes?
Register
Sale
ProductCatalog
Feb 2005
SalesLineItem
92.3913
Ron McFadyen
24
Fly UP