Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.

Similar presentations


Presentation on theme: "SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin."— Presentation transcript:

1 SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin

2 Agenda – Lecture 3b Approaches to Design Exercise: smelly Farm.
Larman, GRASP. Wirfs-Brock et. al., RDD. Exercise: smelly Farm. Applying GRASP via Refactoring. 12/29/2018 SOEN 343, © P.Chalin,

3 Generalization Super (or parent) class; subclass (or child).
+ aa + getAa ( ) Super (or parent) class; subclass (or child). Sub is-a-kind-of Super Means an object of Sub can be used anywhere an object of Super can. Sub inherits properties of Super. An operation (or attribute) in Sub with same signature overrides the operation (attr.) in Super. Sub + getAa ( ) Operation override is one form of polymorphism. 12/29/2018 SOEN 343, © P.Chalin,

4 Dependency A dependency is a using relationship that states that a change … of one thing may affect another that uses it. A B 12/29/2018 SOEN 343, © P.Chalin,

5 Association Is a structural relationship.
Specifies that objects of one thing are connected to objects of another. Given an association you can navigate from an object of one class to an object of another and vice versa. I.e. bidirectional navigability is implicit. A B 12/29/2018 SOEN 343, © P.Chalin,

6 Directed Association Given an association you can navigate from an object of one class to an object of another (but not necessarily vice versa). (… an A might be eventually reachable from a B.) A B 12/29/2018 SOEN 343, © P.Chalin,

7 Directed Association … with (role) names. A - b : B [a..b] A - b B
12/29/2018 SOEN 343, © P.Chalin,

8 Approaches to Design Larman’s approach (GRASP).
Wirfs-Brock et. al. Responsibility-Drive Design (RDD). Both are based on responsibility assignment. I.e. assigning responsibilities to classes / objects. 12/29/2018 SOEN 343, © P.Chalin,

9 Object Responsibilities
A responsibility is an obligation of an object in terms of its behavior. (More on this later) 12/29/2018 SOEN 343, © P.Chalin,

10 Design: Larman’s Approach
Methodology based on Responsibility assignment. GRASP General Responsibility Assignment Software Patterns. Input: Use cases. Domain model. Operation contracts. Larman, Chapter 16. Principles 12/29/2018 SOEN 343, © P.Chalin,

11 GRASP Information Expert Creator Low Coupling High Cohesion Controller
General Responsibility Assignment Software Patterns. Information Expert Creator Low Coupling High Cohesion Controller Polymorphism 12/29/2018 SOEN 343, © P.Chalin,

12 Information Expert “… expresses the common ‘intuition’ that objects do things related to the information they have.” Assign the responsibility for “knowing” [something] to the object (class) that has the information necessary to fulfill the responsibility. 12/29/2018 SOEN 343, © P.Chalin,

13 Creator Assign to class B the responsibility to create an instance of class A if B aggregates A objects. B contains A objects. B records instances of A objects. B closely uses A objects. B has the initializing data that will be passed to A when it is created. 12/29/2018 SOEN 343, © P.Chalin,

14 Farm Example Exercise Set 3 12/29/2018 SOEN 343, © P.Chalin,

15 Farm Class Diagram - animals 12/29/2018 SOEN 343, © P.Chalin,

16 Test Cases package farm.tests; … public class FarmTest extends …{
public void testGetNumLegs() { Farm f = new Farm(); f.add(new Animal("Duck", "Donald")); assertEquals(2, f.getNumLegs()); f.add(new Animal("Dog", "Pluto")); assertEquals(6, f.getNumLegs()); } 12/29/2018 SOEN 343, © P.Chalin,

17 Animal Class public class Animal { private String name;
private String kind; public Animal(String aKind, String aName) { kind = aKind; name = aName; } public String getKind() { return kind; 12/29/2018 SOEN 343, © P.Chalin,

18 Farm Class, public int getNumLegs() {
int result = 0; Iterator it = animals.iterator(); while(it.hasNext()) { Animal a = (Animal) it.next(); if(a.getKind().equals("Duck")) { result += 2; } else if(a.getKind().equals( "Dog")) { result += 4; } else { // ? } return result; 12/29/2018 SOEN 343, © P.Chalin,

19 Bad Smell in Farm.getNumLegs()?
Can you fix it? Hints: By Information Expert, who should know about the number of legs of Animals, the Farm or … ? Having cascaded if’s (or switch statements) over information from another class is usually a very bad smell in OO. Apply Larman’s GRASP principles of Information Expert and Polymorphism. 12/29/2018 SOEN 343, © P.Chalin,

20 GRASP: Polymorphism Principle
Larman: When related alternatives or behaviors vary be type (class), assign responsibility for the behavior—using polymorphic operations—to the types for which the behavior varies. (This principle was illustrated last week with the Animal hierarchy.) 12/29/2018 SOEN 343, © P.Chalin,

21 Farm Example, Solution #1
12/29/2018 SOEN 343, © P.Chalin,

22 Farm Example, Solution #2
12/29/2018 SOEN 343, © P.Chalin,

23 Practice in Tutorial Apply Larman’s GRASP principles of Information Expert and Polymorphism. 12/29/2018 SOEN 343, © P.Chalin,


Download ppt "SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin."

Similar presentations


Ads by Google