introduction to software engineering · general responsibility assignment principle polymorphism 5...

20
Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of Computer Science Technische Universität Darmstadt

Upload: others

Post on 19-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

Introduction to Software Engineering (2+1 SWS)Winter Term 2009 / 2010 Dr. Michael EichbergVertretungsprofessur Software EngineeringDepartment of Computer ScienceTechnische Universität Darmstadt

Page 2: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

Introduction to Software Engineering

Dr. Michael EichbergFachgebiet Software EngineeringDepartment of Computer ScienceTechnische Universität Darmstadt

GRASP(Advanced Principles)• The following slides make extensive use of material from:

Applying UML and Patterns, 3rd Edition; Craig Larman; Prentice Hall

Page 3: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism

3

How to handle alternatives based on type? How to create pluggable software components?

Page 4: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

4

Who should be responsible for authorizing different kinds of payments?

CashPayment

CreditPayment

CheckPayment

Page 5: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism

5

When related alternatives or behaviors vary by type (class), assign responsibility for the behavior - using polymorphic operations - to the types for which the behavior varies.

Do not test for the type of an object

and use conditional logic to perform

varying alternatives based on type.

How to handle alternatives based on type? How to create pluggable software components?

Page 6: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

6

Who should be responsible for authorizing different kinds of payments?

authorize()

CashPayment

authorize()

CreditPayment

authorize()

CheckPayment

Each class who’s behavior varies is responsible.

Page 7: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

7

Who should be responsible for authorizing different kinds of payments?

authorize()

CashPayment

authorize()amount

Payment{abstract}

authorize()

CreditPayment

authorize()

CheckPayment

Page 8: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

8

In Monopoly, the behavior when a player lands on a square varies.

Page 9: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

9

In Monopoly, the behavior when a player lands on a square varies.

Avoid Designing systems with interfaces and polymorphism for speculative “future-proofing” against unknown possible variations.

Page 10: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication

10

What object should have the responsibility when you do not want to violate High Cohesion and

Low Coupling; i.e., when the solutions suggested by the previous GRASPriniples are not

appropriate?

Page 11: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication - Example

•We need support to save Sale instances in a relational database.• by Expert GRASPrinciple we could assign this responsibility to

the Sale class itself• but ...

11

Page 12: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication

12

What object should have the responsibility when the solutions suggested by the previous

GRASPriniples are not appropriate?

Assign a highly cohesive set of responsibilities to an artificial or convenience class that does not represent a problem domain concept.(Such a class is called a (Pure) Fabrication.)

Page 13: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication - Examples

13

▶ a PersistentStorage class▶ a use case handler class: ProcessSaleHandler▶ many objects suggested by patterns (more of that later)

Assign a highly cohesive set of responsibilities to an artificial or convenience class that does not represent a problem domain concept.(Such a class is called a (Pure) Fabrication.)

Page 14: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|

▶ a PersistentStorage class▶ a use case handler class: ProcessSaleHandler▶ many objects suggested by patterns (more of that later)

Assign a highly cohesive set of responsibilities to an artificial or convenience class that does not represent a problem domain concept.(Such a class is called a (Pure) Fabrication.)

GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication - Examples

14

A Pure Fabrication usually takes on responsibilities from the domain class that would be assigned those responsibilities based on Expert.

Page 15: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

Achieving Testability Using Pure Fabrications15

+writeCheck()CheckWriter +calculatePay()

+postPayment()

EmployeePayroll

+getEmployee()+putEmployee()

Employee Database

Page 16: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|GRASP - Pure Fabrication

Achieving Testability Using Pure Fabrications16

+writeCheck()CheckWriter +calculatePay()

+postPayment()

EmployeePayroll

+getEmployee()+putEmployee()

Employee Database

+writeCheck()

«interface»CheckWriter +calculatePay()

+postPayment()

«interface»EmployeePayroll

+getEmployee()+putEmployee()

«interface»Employee Database

PayrollTestMockCheckWriter

MockEmployee

MockEmployeeDatabase

Page 17: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|

Assign a highly cohesive set of responsibilities to an artificial or convenience class that does not represent a problem domain concept.(Such a class is called a (Pure) Fabrication.)

GRASP - Pure Fabrication

General Responsibility Assignment PrinciplePure Fabrication - Examples

17

When abused, the creation of Pure Fabrications will lead to a function or

process-oriented design that is implemented in an object-oriented language; i.e., to

classes for sets of functions.

Page 18: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|

How to design objects, subsystems, and systems so that the variations of instability in these elements do not

have an undesirable impact on other elements?

GRASP - Protected Variations

General Responsibility Assignment PrincipleProtected Variations

18

Page 19: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|

How to design objects, subsystems, and systems so that the variations of instability in these elements do not

have an undesirable impact on other elements?

GRASP - Protected Variations

General Responsibility Assignment PrincipleProtected Variations

19

Identify points of predicted variation or instability; assign responsibilities to create a stable interface around them.

Page 20: Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5 When related alternatives or behaviors vary by type (class), assign responsibility

|

How to design objects, subsystems, and systems so that the variations of instability in these elements do not

have an undesirable impact on other elements?

Identify points of predicted variation or instability; assign responsibilities to create a stable interface around them.

GRASP - Protected Variations

General Responsibility Assignment PrincipleProtected Variations - Related

20

▶!Open-Closed Principle▶!Information Hiding

(as used in Parnas’ paper: “On the Criteria To Be Used in Decomposing Systems into Modules”).

▶!Liskov Substitution Principle(Helps to answer questions such as: “Is it meaningful to derive a class Square from a class Rectangle, at least (in the real world) a square is a rectangle?”

Discussed in Software

Engineering Design and

Construction