Introduction to Software Engineering · General Responsibility Assignment Principle Polymorphism 5...

Preview:

Citation preview

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

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

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism

3

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

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

4

Who should be responsible for authorizing different kinds of payments?

CashPayment

CreditPayment

CheckPayment

|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?

|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.

|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

|GRASP - Polymorphism

General Responsibility Assignment PrinciplePolymorphism - Example

8

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

|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.

|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?

|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

|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.)

|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.)

|

▶ 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.

|GRASP - Pure Fabrication

Achieving Testability Using Pure Fabrications15

+writeCheck()CheckWriter +calculatePay()

+postPayment()

EmployeePayroll

+getEmployee()+putEmployee()

Employee Database

|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

|

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.

|

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

|

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.

|

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

Recommended