1.24k likes | 1.92k Vues
Software Design CS 406 Software Engineering I Fall 2001 Aditya P. Mathur Last update: October 2, 2001 Organization Part I: Design principles and process Part II: OO Design Part III: Design Patterns Part IV: Special topics Learning Objectives
E N D
Software DesignCS 406 Software Engineering IFall 2001 Aditya P. Mathur Last update: October 2, 2001
Organization Part I: Design principles and process Part II: OO Design Part III: Design Patterns Part IV: Special topics Software Design
Learning Objectives • To learn notations for representing a design. • To understand and be able to apply the basic principles of software design. (GRASP patterns). • To learn techniques and tools for Object- Oriented Design. • Interaction diagrams • Class diagrams • State diagrams Software Design
Class model Object behavior model Design state model Models during the Design Phase Static structure diagrams Interaction Diagrams: Collaboration diagrams Sequence diagrams Contracts for methods and operations State diagrams for classes Software Design
1: message2() message1() 2: message3() A collaboration diagram :classAinstance :classBinstance message1() is sent to an instance of class A. message2() and message3() are sent, in that order, by an instance of class A to an instance of class B. One diagram for each system event. Software Design
:classAinstance :classBinstance message1() message2() message3() Focus of control, activation box, optional A sequence diagram May be used for multiple events. Software Design
direction of message first internal message 1: makePayment(cashTendered) makePayment(cashTendered) :POST :Sale first message 1.1: create(cashTendered) link line instance of a class parameter Collaboration diagram:makepayment() :Payment Note: Post is Register in the second edition of Larman. Software Design
Making collaboration diagrams • Design a system of interacting objects to fulfill the tasks....this is the tough part of design! • One diagram for each system operation in the current development cycle, e.g. for makepayment(). • If the diagram gets complex, split into smaller diagrams. Software Design
:Sale s1:Sale Sale class object or instance named object Classes and Objects Software Design
msg1() addPayment(cashTendered) link line Links :Post :Sale Link illustrates client/server relationship. Software Design
msg1() 1: message1() 2: message2() 3: message3() :Post :Sale all messages flow on the same link Messages Software Design
msg1() 1: addPayment(amount:Money) :Post :Sale parameters shown within parentheses; types may also be shown Parameters Software Design
return value type msg1() :Post :Sale return value name Return values 1: tot:=total(): int Software Design
Messages to “self” or “this” msg1() :Post 1: clear() Software Design
msg1() 1*: li = nextLineItem: SalesLineItem :Post :Sale Iteration iteration, no recurrence values Software Design
1*: [i:= 1..10] li = nextLineItem: SalesLineItem :Post :Sale Iteration with recurrence msg1() iteration clause Software Design
msg1() 1*: [i:= 1..10] msg2() :A :myB: B 2*: [i:= 1..10] msg3() :myC: C Iteration clause: Multiple messages msg1() { for i:= 1 to 10 { myB.msg2(); myC.meg3(); } } equal iteration clauses Software Design
msg1() 1: create(cashier) :Post :Sale <<create>> 1: make(cashier) Creation new created instance create message with optional parameter for initialization. Note that use of the name create is a UML convention. Software Design
Languages and create C++ Automatic allocation, or new() operator followed by constructor call. Java new() operator followed by a constructor call. Software Design
first message not numbered second third (nested) msg1() 1: msg2() 1.1: msg3() 2.1: msg5() 2: msg4() fifth (nested) fourth Message sequence numbering :classA :classB :classC Software Design
msg1() 1*: [new sale] create() 1.1: create() conditional message with test Conditional message :Post :Sale :SalesLineItem Software Design
Unconditional after either msg2 or msg4 1a and 1b are mutually exclusive :ClassE 2: msg6() 1a: [test_1] msg2() msg1() :ClassA :ClassB 1b: [nottest_1] msg4() 1a.1: msg3() 1b.1: msg5() :ClassD :ClassC Mutually exclusive conditional messages Software Design
sales:Sale A multiobject or a collection of instances, e.g. a List of Sales. Collections Software Design
Message sent to a Multiobject. msg1() 1*: s:=size(): int :Sale :SalesLineItem The two *’s imply iteration over all elements. Messages to Multiobjects * Software Design
Message sent to an element. msg1() 1: el:=create() :Sale sl:SalesLineItem :SalesLineItem 2: addElement(el) Message sent to a collection. Messages to multiobjects and an element [1] Software Design
Design Guidelines • OO system consists of interacting objects. • How does one determine the assignment of responsibilities to various objects? • There is no unique assignment and hence “good” and “poor” designs, “beautiful” and “ugly” designs, “efficient” and “inefficient” designs. • Design guidelines assist in the derivation of a good design. Software Design
GRASP Patterns • GRASP: General Responsibility Assignment Software Patterns • These are best considered as design guidelines and principles. These could also be considered as “thought patterns” useful in software design. Software Design
Guidelines and principles • We first consider three useful guidelines: • Expert • Creator • Controller and two widely applicable principles: • High cohesion • Low coupling Software Design
Product Catalog Payment Item Manager Customer POS Store Cashier Sale date time address name amount Product Specification Sales LineItem description price itemID quantity POS: Partial Domain Model Contains 1 1…* 1 Used-by * 1 Describes 1..* * Contained-in Stocks 1 1 * 1 Houses Captured-on * Started-by 1 1 Paid-by 1 1 1 1 1 Initiated-by 1 1 Records-sale-on 1 Software Design
Expert • Question: How does one assign responsibilities ? • Answer: Assign a responsibility to an information expert, i.e. to a class that has the information needed to fulfill that responsibility. Software Design
Sale date time Contains 1…* Product Specification Sales LineItem Described by * description price UPC quantity Expert: Example [1] In POS, some class needs to know the grand total of a sale. Who should be responsible for knowing the grand total of a sale ? Software Design
Expert: Example [2] • From the model, identify the class that contains the information needed to obtain the grand total. • Information needed to obtain the grand total: • Knowledge of all SaleItems • Sum of their subtotals • Only a Sale object possesses this knowledge. • Sale being the information expert, we assign this responsibility to Sale. Software Design
1: t:=getTotal() Sale New method added to the Sale class. The class itself is derived from the domain model. date time getTotal Partial collaboration diagram [1] :Sale Software Design
Expert: Example [3] • What information is needed to determine subtotal? • We need: • Quantity of each SalesLineItem and its price. • Quantity is available with SalesLineItem and price with ProductSpecification. • Hence SalesLineItem is assigned the responsibility to compute the subtotal. Software Design
1: t:=total() 1*: st=getSubtotal() :Sale * Sale :SalesLineItem date time 2.1: p:=getPrice() SalesLineItem ProductSpecification :ProductSpecification getTotal() quantity description price itemID getSubtotal() getPrice() Partial collaboration diagram [2] Software Design
Responsibility Class Sale Knows sale total SalesLineItem Knows line item subtotal. ProductSpecification Knows the price of a product Summary: Example [4] Software Design
Expert: Discussion • Expert guideline used often. • Fulfillment of a responsibility often requires interaction amongst several objects (3 in our example). There are many semi-expertswho collaborate in performing a task. • Use of the Expert guideline allows us to retain encapsulation of information. • It often leads to “lightweight” classes collaborating to fulfill a responsibility. Software Design
Expert: Disadvantages • On some occasions the Expert guideline might not suggest a desirable solution. • Example: Who should save Sale in a database ? • As all the information to be saved is in the Sale class, it should be responsible for saving the information. • This implies that the Sale class must know about handling databases. Adding database related methods to sale class will make it in-cohesive. • It also violates the “separation of concerns” principle. Software Design
Expert: Benefits • Objects use their own information to fulfill tasks, hence encapsulation is maintained. • This leads to low coupling. • Behavior is distributed across cohesive and lightweight classes. Software Design
Creator [1] • Question: Who should be responsible for creating an instance of a class ? • Answer: Assign to B the responsibility to create an object of class A if the following is true: Software Design
Creator [2] • B aggregates objects of type A. • B contains objects of type A. • B records instances of objects of type A. • B closely uses objects of type A. • B has the data passed to A when A is created. Implication: B is an expert in the creation of A. Software Design
Sale date time Contains 1…* Product Specification Sales LineItem Described by * description price itemID quantity Creator: Example [1] Who should be responsible for creating a SalesLineItem? Software Design
Creator: Example [2] • Salecontains many SalesLineItem objects. • This implies that Sale is a good candidate for creating a SalesLineItem. • This leads to the following collaboration diagram. Software Design
:POST :Sale makeLineItem(quantity) create(quantity) :SalesLineItem Sequence diagram for the creation of SalesLineItem Software Design
makeLineItem(quantity) 1: create(quantity) Sale date time makeLineItem() total() New method added to the Sale class. Partial collaboration diagram :Sale :SalesLineItem Software Design
Creator: Discussion • Creator guides the assignment of responsibility of creating objects. • Creator suggests that the enclosing container or aggregate class is a good candidate for the creation of the object contained. Software Design
Controller [1] • Question: Who should be responsible for handling system events ? Recall that a system event is a high level event, an external event, generated by a system actor. • Answer: Assign a responsibility to handle a system event to a controller. Software Design
Controller [2] • A controller is a non-user interface object that handles system events. Here is a list of controllers: • Façade controller: Represents the overall system, device, or business. • Use case controller: Represents an artificial handler of all events of a use case. Software Design
Controller [3] • window, applet, view, document do not qualify as controllers. They typically receive system events and delegate them to a controller. • System event examples: • Pressing the “end of sale” button. • Request Spell Check. • Request Engine Start. Software Design
System operations System endSale() enterItem() makePayment() During system behavior analysis, system operations are assigned to the class System. It does not imply that the class named System performs these during design. Instead, during design, a controller class is assigned to perform these operations. Software Design