43
Fall 2014 School of Computer Science © 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and Introduction to Inheritance Charlie Garrod Jonathan Aldrich

Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

Embed Size (px)

Citation preview

Page 1: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

     

Fall  2014  

School of Computer Science

© 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis

Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and Introduction to Inheritance

Charlie Garrod Jonathan Aldrich

Page 2: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 2 15-­‐214

Administrivia

• Homework 1 due Thursday

• Homework 2 coming soon! § Due next Thursday

Page 3: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 3 15-­‐214

Key concepts from last Thursday

Page 4: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 4 15-­‐214

The design process

1.  Object-oriented analysis §  Understand the problem §  Identify the key concepts and their relationships §  Build a (visual) vocabulary §  Create a domain model (aka conceptual model)

2.  Object-oriented design §  Identify software classes and their relationships with

class diagrams §  Assign responsibilities (attributes, methods) §  Explore behavior with interaction diagrams §  Explore design alternatives §  Create an object model (aka design model and design

class diagram) and interaction models

3.  Implementation §  Map designs to code, implementing classes and

methods

last Thurs.

Page 5: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 5 15-­‐214

Documenting a domain model

• Typical: UML class diagram § Simple classes without methods and essential attributes only

§ Associations, inheritances, etc. as needed

§ Do not include implementation-specific details, e.g., types, method signatures

§  Include notes as needed

• Complement with examples, glossary, etc. as needed

• Formality depends on size of project

• Expect revisions

Page 6: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 6 15-­‐214

Today

• Visualizing dynamic behavior §  Interaction diagrams

• Inheritance and polymorphism §  For maximal code re-use § Design ideas: hierarchical modeling §  Inheritance and its alternatives §  Java details related to inheritance

Page 7: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 7 15-­‐214

Problem

Space

Domain Model

Solution Space

Object Model

Our design trajectory

•  Real-world concepts •  Requirements, concepts •  Relationships among

concepts •  Solving a problem •  Building a vocabulary

•  System implementation •  Classes, objects •  References among objects

and inheritance hierarchies •  Computing a result •  Finding a solution

Page 8: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 8 15-­‐214

How do we bridge the representational gap?

• Domain model with a UML class diagram § Simple classes without methods and essential attributes only

§ Associations, inheritances, etc. as needed

§ Do not include implementation-specific details, e.g., types, method signatures

§  Include notes as needed

• Complement with examples, glossary, etc. as needed

• Formality depends on size of project

• Expect revisions

Page 9: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 9 15-­‐214

One tool: Interaction diagrams

• An interaction diagram is a picture that shows, for a single scenario of use, the events that occur across the system’s boundary or between subsystems

• Clarifies interactions: § Between the program and its environment § Between major parts of the program

• For this course, you should know: § Communication diagrams § Sequence diagrams

Page 10: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 10 15-­‐214

E.g., Lifecycle of Jonathan's simulation framework

Simulation Framework

Lodgepole agent

Infestation agent

Management agent

Douglas Fir agent

Observation agent

… Simulation Driver

1. Select and create agents

2. Add agents to framework 3. Invoke simulate() on the framework

4. Invoke timestep() on each agent

5. Update agent-specific state in timestep()

6. Invoke logState() on each agent 7. Repeat 4-6 until done

Page 11: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 11 15-­‐214

Creating a communication diagram

Page 12: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 12 15-­‐214

Communication diagram example, complete

Page 13: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 13 15-­‐214

(An annotated communication diagram)

Page 14: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 14 15-­‐214

Constructing a sequence diagram

Page 15: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 15 15-­‐214

Sequence diagram example, complete

Page 16: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 16 15-­‐214

(An annotated sequence diagram)

Page 17: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 17 15-­‐214

What kind of interaction diagram is this?

Simulation Framework

Lodgepole agent

Infestation agent

Management agent

Douglas Fir agent

Observation agent

… Simulation Driver

1. Select and create agents

2. Add agents to framework 3. Invoke simulate() on the framework

4. Invoke timestep() on each agent

5. Update agent-specific state in timestep()

6. Invoke logState() on each agent 7. Repeat 4-6 until done

Page 18: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 18 15-­‐214

What kind of interaction diagram is this?

Simulation Framework

Lodgepole agent

Infestation agent

Management agent

Douglas Fir agent

Observation agent

… Simulation Driver

1. Select and create agents

2. Add agents to framework 3. Invoke simulate() on the framework

4. Invoke timestep() on each agent

5. Update agent-specific state in timestep()

6. Invoke logState() on each agent 7. Repeat 4-6 until done

(It is a communication diagram.)

Page 19: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 19 15-­‐214

Sequence vs. communication diagrams

• Relative advantages and disadvantages?

Page 20: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 20 15-­‐214

Today

• Visualizing dynamic behavior §  Interaction diagrams

• Inheritance and polymorphism §  For maximal code re-use § Design ideas: hierarchical modeling §  Inheritance and its alternatives §  Java details related to inheritance

Page 21: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 21 15-­‐214

An introduction to inheritance

• A dog of an example: § Dog.java § AbstractDog.java § Chihuahua.java § GermanShepherd.java

• Typical roles: § An interface define expectations / commitment for clients § An abstract class is a convenient hybrid between an interface and a full implementation

§ Subclass overrides a method definition to specialize its implementation

Page 22: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 22 15-­‐214

Inheritance: a glimpse at the hierarchy

• Examples from Java §  java.lang.Object  § Collections library

Page 23: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 23 15-­‐214

Java Collections API (excerpt)

Collection

List Set AbstractCollection

AbstractList

LinkedList

Vector

HashSet

AbstractSequentialList

AbstractSet

Cloneable

ArrayList

interfaces

Page 24: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 24 15-­‐214

Benefits of inheritance

• Reuse of code

• Modeling flexibility

• A Java aside: § Each class can directly extend only one parent class § A class can implement multiple interfaces

Page 25: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 25 15-­‐214

Aside: more UML class diagram notation

«interface»  Dog    getName()  :  String  getBreed()  :  String  bark()  :  String  setName(name  :  String)  toString()  :  String  

AbstractDog  -­‐  name  :  String    -­‐  breed  :  String    +  getName()  :  String  +  getBreed()  :  String  +  bark()  :  String  +  setName(name  :  String)  #  setBreed(breed  :  String)  +  toString()  :  String  

GermanShephard    bark()  :  String  play()  

«interface»  brand  

Name  of  class  or  interface  in  top  compartment  

Return  type  comes  aDer  

method  or  field  Methods  in  boGom  

compartment   Dashed  line,  open  triangle  arrowhead  for  implements  

Solid  line,  open  triangle  arrowhead  

for  extends  

Italics  means  abstract  

Italics  means  abstract  

OpMonal  visibility:  +  for  public  -­‐   for  private  #  for  protected  ~  for  package  (not  used  much)  

Fields  in  middle  compartment  

Page 26: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 26 15-­‐214

Another example: different kinds of bank accounts

«interface»  CheckingAccount    getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  getFee()  :  float  

«interface»  SavingsAccount    getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  getInterestRate()  :  float  

Page 27: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 27 15-­‐214

A better design: An account type hierarchy

«interface»  Account    getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

«interface»  CheckingAccount    getFee()  :  float  

«interface»  SavingsAccount    getInterestRate()  :  float  

«interface»  InterestCheckingAccount      

SavingsAccount  is  a  subtype  of  

Account.    Account  is  a  supertype  of  SavingsAccount.  

MulOple  interface  extension  

CheckingAccount  extends  Account.  All  methods  from  

Account  are  inherited  (copied  to  CheckingAccount)  

If  we  know  we  have  a  CheckingAccount,  addiOonal  methods  

are  available.  

Page 28: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 28 15-­‐214

A better design: An account type hierarchy

«interface»  Account    getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

«interface»  CheckingAccount    getFee()  :  float  

«interface»  SavingsAccount    getInterestRate()  :  float  

«interface»  InterestCheckingAccount      

SavingsAccount  is  a  subtype  of  

Account.    Account  is  a  supertype  of  SavingsAccount.  

MulOple  interface  extension  

CheckingAccount  extends  Account.  All  methods  from  

Account  are  inherited  (copied  to  CheckingAccount)  

If  we  know  we  have  a  CheckingAccount,  addiOonal  methods  

are  available.  

public interface CheckingAccount ! extends Account {! …!} !

public interface InterestCheckingAccount! extends CheckingAccount, SavingsAccount {! …!} !

Page 29: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 29 15-­‐214

The power of object-oriented interfaces

• Polymorphism § Different kinds of objects can be treated uniformly by client code • e.g., a list of all accounts

§ Each object behaves according to its type • If you add new kind of account, client code does not change

§ Consider this pseudocode:

§ See the DogWalker example

If today is the last day of the month:! For each acct in allAccounts:! acct.monthlyAdjustment();! !

Page 30: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 30 15-­‐214

One implementation: Just use interface inheritance

«interface»  Account    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

«interface»  CheckingAccount    

getFee()  :  float  

«interface»  SavingsAccount    

getInterestRate()  :  float  

«interface»  InterestCheckingAccount      

CheckingAccountImpl    

…  

SavingsAccountImpl    

…  

InterestCheckingAccountImpl    

…  

Page 31: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 31 15-­‐214

Better: Reuse abstract account code

public abstract class AbstractAccount implements Account { protected float balance = 0.0; public float getBalance() { return balance; } abstract public void monthlyAdjustment(); // other methods…

} public class CheckingAccountImpl

extends AbstractAcount implements CheckingAccount { public void monthlyAdjustment() { balance -= getFee(); } public float getFee() { /* fee calculation */ }

}

«interface»  Account    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

CheckingAccountImpl    

monthlyAdjustment()  getFee()  :  float  

AbstractAccount    

#  balance  :  float    

+  getBalance()  :  float  +  deposit(amount  :  float)  +  withdraw(amount  :  float)  :  boolean  +  transfer(amount  :  float,  

 target  :  Account)  :  boolean  +  monthlyAdjustment()  

«interface»  CheckingAccount    

getFee()  :  float  

Page 32: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 32 15-­‐214

Better: Reuse abstract account code

public abstract class AbstractAccount implements Account { protected float balance = 0.0; public float getBalance() { return balance; } abstract public void monthlyAdjustment(); // other methods…

} public class CheckingAccountImpl

extends AbstractAcount implements CheckingAccount { public void monthlyAdjustment() { balance -= getFee(); } public float getFee() { /* fee calculation */ }

}

«interface»  Account    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

CheckingAccountImpl    

monthlyAdjustment()  getFee()  :  float  

AbstractAccount    

#  balance  :  float    

+  getBalance()  :  float  +  deposit(amount  :  float)  +  withdraw(amount  :  float)  :  boolean  +  transfer(amount  :  float,  

 target  :  Account)  :  boolean  +  monthlyAdjustment()  

«interface»  CheckingAccount    

getFee()  :  float  

protected  elements  are  visible  in  subclasses  

an  abstract  class  is  missing  the  

implementaOon  of  one  or  more  methods  

an  abstract  method  is  leT  to  be  implemented  in  a  

subclass  

no  need  to    define  getBalance()  –  the  code  is  

inherited  from  AbstractAccount  

Page 33: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 33 15-­‐214

Inheritance and subtyping

• Inheritance is for code reuse § Write code once and only once § Superclass features implicitly available in subclass

• Subtyping is for polymorphism § Accessing objects the same way, but getting different behavior

§ Subtype is substitutable for supertype

class  A  extends  B  

class  A  implements  I  class  A  extends  B  

Page 34: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 34 15-­‐214

Challenge: Is inheritance necessary?

• Can we get the same amount of code reuse without inheritance?

«interface»  Account    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

«interface»  CheckingAccount    

getFee()  :  float  

«interface»  SavingsAccount    

getInterestRate()  :  float  

«interface»  InterestCheckingAccount      

Page 35: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 35 15-­‐214

Reuse via composition and delegation

«interface»  Account    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean  monthlyAdjustment()  

«interface»  CheckingAccount    

getFee()  :  float  

CheckingAccountImpl    

monthlyAdjustment()  {  …  }  getFee()  :  float  {  …  }  getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean    

BasicAccountImpl    

balance  :  float    

getBalance()  :  float  deposit(amount  :  float)  withdraw(amount  :  float)  :  boolean  transfer(amount  :  float,  

 target  :  Account)  :  boolean    

public class CheckingAccountImpl! implements CheckingAccount {! BasicAccountImpl basicAcct = new(…);! public float getBalance() {! return basicAcct.getBalance();! }! // …!

CheckingAccountImpl  is  composed  of  a  BasicAccountImpl  

Page 36: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 36 15-­‐214

Java details: extended re-use with super!

public abstract class AbstractAccount implements Account {!!protected float balance = 0.0;!

!public boolean withdraw(float amount) {!

! // withdraws money from account (code not shown)!

!}!

}!

!

public class ExpensiveCheckingAccountImpl!

! !extends AbstractAcount implements CheckingAccount {!

!public boolean withdraw(float amount) {!

! !balance -= HUGE_ATM_FEE;!

! !boolean success = super.withdraw(amount)!

! !if (!success)!

! ! !balance += HUGE_ATM_FEE;!

! !return success;!

!}!

}!

 Overrides  withdraw  but  also  uses  the  superclass  withdraw  method  

Page 37: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 37 15-­‐214

Java details: constructors with this and super!

public class CheckingAccountImpl!

! !extends AbstractAcount implements CheckingAccount {!

!

!private float fee;!

!

!public CheckingAccountImpl(float initialBalance, float fee) {!

! !super(initialBalance);!

! !this.fee = fee;!

!}!

!

!public CheckingAccountImpl(float initialBalance) {!

! !this(initialBalance, 5.00);!

!}!

!/* other methods… */ }!

!

Invokes  another  constructor  in  this  

same  class  

Invokes  a  constructor  of  the  superclass.  Must  be  the  first  statement  of  the  

constructor.  

Page 38: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 38 15-­‐214

Java details: final!

• A final class: prevents extending the class §  e.g., public final class CheckingAccountImpl { …!

• A final method: prevents overriding the method

• A final field: prevents assignment to the field §  (except to initialize it)

• Why might you want to use final in each of the above cases?

Page 39: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 39 15-­‐214

Note: type-casting in Java

• Sometimes you want a different type than you have §  e.g., !float pi = 3.14;! !int indianaPi = (int) pi;!

• Useful if you know you have a more specific subtype: §  e.g., Account acct = …;! CheckingAccount checkingAcct = ! (CheckingAccount) acct;! float fee = checkingAcct.getFee(); § Will get a ClassCastException if types are incompatible

• Advice: avoid downcasting types § Never(?) downcast within superclass to a subclass

Page 40: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 40 15-­‐214

Note: instanceof!

• Operator that tests whether an object is of a given class public void doSomething(Account acct) {! float adj = 0.0;! if (acct instanceof CheckingAccount) {! checkingAcct = (CheckingAccount) acct;!! adj = checkingAcct.getFee();! } else if (acct instanceof SavingsAccount) {! savingsAcct = (SavingsAccount) acct;!! adj = savingsAcct.getInterest();! }! …!}!

• Advice: avoid instanceof if possible § Never(?) use instanceof in superclass to check type against subclass

!

Page 41: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 41 15-­‐214

Avoiding instanceof with the Template Method pattern

public interface Account {! …! public float getMonthlyAdjustment();!}!!public class CheckingAccount implements Account {! …! public float getMonthlyAdjustment() {! return getFee();! }!}!!public class SavingsAccount implements Account {! …! public float getMonthlyAdjustment() {! return getInterest();! }!}!!!!

Page 42: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 42 15-­‐214

Avoiding instanceof with the Template Method pattern!

float adj = 0.0;!if (acct instanceof CheckingAccount) {! checkingAcct = (CheckingAccount) acct;!! adj = checkingAcct.getFee();!} else if (acct instanceof SavingsAccount) {! savingsAcct = (SavingsAccount) acct;!! adj = savingsAcct.getInterest();!}!!!

Instead:!float adj = acct.getMonthlyAdjustment();!!

Page 43: Principles of Software Construction: Objects, Design, … · Principles of Software Construction: Objects, Design, and Concurrency Interaction Diagrams and ... Explore behavior with

toad 43 15-­‐214

Thursday…

• Behavioral contracts and subtyping rules