supporting design model refactoring for improving class responsibility assignment

34
† Tokyo Institute of Technology, Japan ‡ Nagoya University, Japan S u p p o r t i n g D e s i g n M o d e l R e f a c t o r i n g f o r I m p r o v i n g C l a s s R e s p o n s i b i l i t y A s s i g n m e n t Motohiro Akiyama , Shinpei Hayashi , Takashi Kobayashi , and Motoshi Saeki

Upload: shinpei-hayashi

Post on 05-Jul-2015

2.899 views

Category:

Technology


0 download

DESCRIPTION

Presented at MODELS 2011 http://dx.doi.org/10.1007/978-3-642-24485-8_33

TRANSCRIPT

Page 1: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

† Tokyo Institute of Technology, Japan ‡ Nagoya University, Japan

Supporting Design Model Refactoring

for Improving

Class Responsibility Assignment

Motohiro Akiyama†, Shinpei Hayashi†, Takashi Kobayashi‡, and Motoshi Saeki†

Page 2: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

2

Aim − Computerized support of

Class Responsibility Assignment (CRA)

Approach: automated CRA refactoring − Usage of GRASP − Defining detection rules of bad smells − Defining transformation rules of refactoring operations

Result − Defined 5 CRA refactorings based on GRASP − Implemented an automated tool − Preliminary evaluated our approach

by a controlled experiment with 4 human subjects

Abstract

Page 3: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

3

Design of high quality Highly maintainable products

Our focus: Class Responsibility Assignment (CRA) − A design task before completing class diagrams − Assigning responsibilities to classes

Responsibility [Wirfs-Brock 90] − Representing

• The actions an object performs • The knowledge an object maintains • Major decisions an object makes that affect others

− Basis of the design of methods/attributes

Background

Page 4: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

4

How to model this? − Menu available after authentication − Different menu required according to roles

CRA: an example

for users

Menu • Display admin menu

• Display user menu

Responsibilities + displayMenu(authority: int)

for admins

Classes

hayashi

Login

Login

account password deadbeef

A solution: • Display admin menu • Display user menu

Page 5: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

5

“Bad smells” in CRA

if (authority == ADMIN) { …… } else if (authority == VIP) { …… } else { …… }

public void displayMenu(int authoriuty) { if (authority == ADMIN) { …… } else { …… } }

Low maintainability New option causes modification Branches

Menu

+ displayMenu(authority: int)

• Display admin menu

• Display user menu (authority: int)int)

Page 6: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

6

9 patterns/principles of CRA; e.g. − Polymorphism:

When related alternatives or behaviors vary by class, assign responsibility for the behavior using polymorphic operations to the classes for which the behavior varies.

− Protected Variations: Identify points of predicted variation or instability; assign responsibilities to create a stable “interface” around them.

Can combine multiple patterns − Polymorphism + Protected Variations

GRASP [Larman 04] General Responsibility

Assignment Software Pattern

Page 7: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

7

CRA Refactoring

Refactor based on Protected Variations Polymorphism

Menu

+ displayMenu(authority: int) • Display admin menu

• Display user menu

Menu

+ displayMenu()

Admin-Menu

+ displayMenu()

User-Menu

+ displayMenu()

• Display admin menu

• Display user menu

How to achieve automated support of CRA refactorings?

Page 8: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

8

1. How to analyze relationships of responsibilities?

2. How to deal with informal descriptions of GRASP?

Challenges

• Display admin menu

• To display the menu of users

• Display admin menu

• Display user menu similar. similar?

When related alternatives or behaviors vary by class, assign responsibility for the behavior using polymorphic operations to the classes for which the behavior varies. operations to the classes for which the behavior varies

When related alternatives or behaviors vary by classassign responsibility for the behavior using polymorphic operations to the classes for which the behavior varies

When related alternatives or behaviors vary by classassign responsibility for the behavior using polymorphic operations to the classes for which the behavior varies

Page 9: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

9

1. How to analyze relationships of responsibilities?

2. How to deal with informal descriptions of GRASP?

Our Solutions

Giving formal definition of smell detection rules based on GRASP

Giving a special form for responsibility descriptions enabling easier analysis of responsibilities

Page 10: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

10

1. Describing responsibilities in a special form

− enable us to analyze them easily

Our Approach

Requirements documents

(e.g. use case desc.)

Refactor

Responsibilities

CRA

2. Automated CRA refactoring − Detecting CRA smells − Suggesting CRA refactorings − Applying CRA refactoring when accepted

Page 11: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

11

Finer-grained than sentence − Describe a responsibility as 7 components − who does what to whom when − Similar to the Case Grammar [Fillmore 68]

Responsibility Form

ID (id): unique identifier number Action (action): the verb Target (target): the target of the action Modifier (mod): the modifier of the target Possessive (pos): the modifier which is a possessive case Condition (cond): whether the action is executed or not Dependency (dep): responsibilities to which are referred by this

Page 12: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

12

Responsibility Form • #1: Display admin menu

id: 1action: Display target: menu mod: admin pos: (nil) cond: (nil) dep: #4

Menu

LoginController

• #4: Check authority of account

id: 4 action: Check target: authority mod: (nil) pos: account cond: (nil) dep: (nil)

Page 13: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

13

Detecting using similarity of responsibility descriptions

coordinate Relationship

• #1: Display user menu

id: 1 action: Display target: menu mod: user pos: (nil) cond: (nil) dep: (nil)

• #2: Display admin menu

id: 2 action: Display target: menu mod: admin pos: (nil) cond: (nil) dep: (nil)

same same

different same

Page 14: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

14

1. Move Responsibility − moves a responsibility to more appropriate class

2. Introduce Simple Factory − unifies creation responsibilities which instantiate the same class

3. Introduce Creator − moves responsibilities of instantiating a class to an appropriate class

4. Introduce Polymorphism − separately assigns coordinate responsibilities to individual classes

having a common parent class

5. Introduce Facade − reorganizes a CRA having complex dependencies among owner

classes of the responsibilities

CRA Refactorings

Page 15: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

15

Based on: − Polymorphism

When related alternatives or behaviors vary by class, assign responsibility for the behavior using polymorphic operations to the classes for which the behavior varies.

− Protected Variations Identify points of predicted variation or instability; assign responsibilities to create a stable “interface” around them.

Detection Condition: 1. Finding coordinate relationships 2. Satisfying pre-conditions

• There is no common parent class of the target classes

Introduce Polymorphism

Page 16: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

16

Introduce Polymorphism 1. Create new child classes 2. Move responsibilities 3. Create a parent class 4. Rename

Menu

• Display admin menu

• Display user menu

Page 17: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

17

Introduce Polymorphism

Menu

Child1

• Display admin menu

• Display user menu

1. Create new child classes 2. Move responsibilities 3. Create a parent class 4. Rename

Page 18: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

18

Introduce Polymorphism

Menu

Child1

• Display admin menu

• Display user menu

1. Create new child classes 2. Move responsibilities 3. Create a parent class 4. Rename

Page 19: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

19

Introduce Polymorphism

Base

Menu

Child1

• Display admin menu

• Display user menu

1. Create new child classes 2. Move responsibilities 3. Create a parent class 4. Rename

Page 20: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

20

Introduce Polymorphism

Menu

Admin-Menu

User-Menu

• Display admin menu

• Display user menu

1. Create new child classes 2. Move responsibilities 3. Create a parent class 4. Rename

Page 21: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

21

Supporting Tool: RAST Supporting Tool: RASTSupporting Tool: RAST

Page 22: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

22

Supporting Tool: RAST

Class diagram editor

Responsibility editor

Responsibilities

Page 23: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

23

Supporting Tool: RAST

Suggested CRA refactorings

metrics view: • CLCr • LCOMr

lower is better

Page 24: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

24

Supporting Tool: RAST

Related classes are highlighted

Page 25: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

25

Supporting Tool: RAST

Refactored

Page 26: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

26

8 / 9 patterns are supported in RAST

Supported GRASPs

GRASP CRA refactorings supports

✓ Information Expert ✓ Pure Fabrication

✓ Indirection ✓ Creator

✓ Protected Variations ✓ Polymorphism ✓ Low Coupling ✓ High Cohesion

✗ Controller

Move Responsibility Introduce Simple Factory Introduce Facade Introduce Creator Introduce Polymorphism

The metrics view in RAST

(Unsupported)

Page 27: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

27

RQ1: Does our method improve the design quality of CRA?

RQ2: Do CRA alternatives suggested by our tool support designers appropriately?

Preliminary Evaluation

Obtained positive results!

Obtained positive results!

Page 28: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

28

Subjects

Given data − Use case descriptions for the target system − Responsibilities extracted from the use case descriptions

Tasks − Preparation: Usage lecture (10 min) − Extracting (creating) classes − Assigning given responsibilities to classes

Experimental Design (1)

Student 1 (S1)

Expert 1 (E1)

Student 2 (S2)

Expert 2 (E2)

(2 hrs)

Page 29: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

29

Experimental Design (2) RQ1: Quality of design

RQ2: Support for designers

Metric • CLCr • NCr • LCOMr

Questionnaires

System

Supply chain management system for a virtual winery• 5 use cases • 46 responsibilities

Page 30: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

30

RQ1: Results

S1

E1

S2

E2 average

CLCr 0.67 1.04 0.86

2.53 1.30 1.92

NCr 0.85 1.07 0.96

1.27 1.60 1.43

LCOMr 0.72 0.79 0.76

0.80 0.97 0.89

: RAST’s CRA refactoring available

(Although the result is far from statistical evidence,) All metric values with tool support are better.

Page 31: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

31

Move Responsibility Tool suggested just what I intend to move. Reasonable suggestions. ✗However many unnecessary suggestions included.

Introduce Facade ✗Makes my design more complicated rather than reduce complexity.

Introduce Creator Reasonable and useful suggestions.

Introduce Polymorphism I just realized coordinate responsibilities when tool suggested this refactoring. Tool suggested a responsibility split just what I intend to.

Introduce Simple Factory − (No suggestions)

RQ2: Results

31

Mostly the answers are positive: The subjects prefer the suggested CRA.

Page 32: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

32

Improving refactoring rules − Usage of architecture information; e.g. MVC

Evaluation++ − Statistical evidences − Costs for describing responsibilities − Usability of RAST

Future Work

Page 33: Supporting Design Model Refactoring for Improving Class Responsibility Assignment

Conclusion

7

CRA Refactoring

Refactor based onProtected VariationsPolymorphism

How to achieve automated support of CRA refactorings? 10

1. Describing responsibilities in aspecial form− enable us to analyze them easily

Our Approach

Requirementsdocuments

(e.g. use case desc.)

Refactor

Responsibilities

CRA

2. Automated CRA refactoring− Detecting CRA smells− Suggesting CRA refactorings− Applying CRA refactoring when accepted

23

Supporting Tool: RAST

Suggested CRA refactorings

metrics view:•CLCr•LCOMr

lower is better

27

RQ1: Does our method improve the design quality of CRA?

RQ2: Do CRA alternatives suggested by our tool support designers appropriately?

Preliminary Evaluation

Obtained positive results!

Obtained positive results!