• No results found

11. Role-Based Design

N/A
N/A
Protected

Academic year: 2021

Share "11. Role-Based Design"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Design Patterns and Frameworks, © Dr. Sebastian Götz

1

11. Role-Based Design

Dr. Sebastian Götz

Software Technology Group Department of Computer

Science

Technische Universität Dresden

1) Running Example

2) The Role-object Pattern

3) Object Schizophrenia

4) Delegation vs. Forwarding

(2)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 2

Goals

► Understand how roles can be implemented in current

mainstream object-oriented languages (e.g., Java)

■ Role-Object Pattern

► Understand the problem of object-oriented compared to

role-oriented programming

■ Object Schizophrenia

► Understand the problem of identity and state

■ Delegation versus Forwarding

► Know how role types can be formally distinguished from

(3)

Design Patterns and Frameworks, © Dr. Sebastian Götz

3

11.1 Running Example

A Dialog asking a User for its

EMail-Address

(4)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 4

A Dialog Requesting an Email-Address

Provide EMail-Address

Please provide your Email-Address: [email protected]

OK Cancel

► User shall provide his Email-Address

► Application want's to ensure that the provided address is

(5)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 5

A Dialog Requesting an Email-Address

Provide EMail-Address

Please provide your Email-Address:

test

OK

Cancel

► User shall provide his Email-Address

► Application want's to ensure that the provided address is

valid (Pattern: [email protected])

(6)

Design Patterns and Frameworks, © Dr. Sebastian Götz

6

11.2 Role-object Pattern (ROP)

Delegation-based Realization of

Roles in Object-oriented Languages

Slides based on:

Dirk Bäumer, Dirk Riehle, Wolf Siberski, and Martina Wulf: Role Object Pattern. In: Pattern languages of program design (PLoP) 4, pp. 15-32

(7)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 7

Purpose of Role-Object Pattern

► Transparently adapting objects to client context

► Management of role playership, where roles are

represented as individual objects

addressField: TextField Child

root : Dialog Parent

(8)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 8

Purpose of Role-Object Pattern

► Transparently adapting objects to client context

► Management of role playership, where roles are

represented as individual objects

addressField: TextField Child DataSource root : Dialog Parent formProcessor : Validator DataSink

(9)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 9

Purpose of Role-Object Pattern

► Transparently adapting objects to client context

► Management of role playership, where roles are

represented as individual objects

description: TextArea Child DataSource root : Dialog Parent formProcessor : Validator DataSink 2nd Client = 2nd Context = Validation 1st Client = 1st Context = Drawing

(10)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 10

Structure of Role-Object Pattern

Dirk Bäumer, Dirk Riehle, Wolf Siberski, and Martina Wulf: Role Object Pattern. In: Pattern languages of program design (PLoP) 4, pp. 15-32

(11)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 11

Running Example: Email Checking

TextFieldCore

Child DataSource TextFieldRole

TextField

(12)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 12

Running Example: Email Checking

description: TextFieldCore

descriptionChild:

Child

(13)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 13

Running Example: Email Checking

description: TextFieldCore descriptionChild: Child addRole(descriptionDS); descriptionDS: DataSource

(14)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 14

Running Example: Email Checking

description: TextAreaCore

removeRole(descriptionChild);

descriptionDS:

(15)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 15

Running Example: Email Checking

description: TextAreaCore descriptionChild: Child descriptionDS: DataSource drawer : Parent validator : DataSink

(16)

Design Patterns and Frameworks, © Dr. Sebastian Götz

16

11.3 Object Schizophrenia

(17)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 17

The problem of split objects

► Object schizophrenia covers the problems, which arise

from splitting a conceptual object into multiple parts.

TextField DataSource Child TextField DataSource Child TextField text : String parent : Parent TextField text : String Child parent : Parent DataSource text : String

(18)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 18

The problem of split objects

► Object schizophrenia covers the problems, which arise

from splitting a conceptual object into multiple parts.

► Question for identity depends on which object is asked.

“Who are you?”

■ User: “I'm a TextField.”

■ Drawer: “I'm the child of this parent.” ■ Validator: “I'm a data source for you.”

TextField

DataSource Child

User

(19)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 19

The problem of split objects

► Object schizophrenia covers the problems, which arise

from splitting a conceptual object into multiple parts.

► Who manages the state of the compound object?

■ The text of the field is required for both roles

■ The size of the field is specific to the drawing task

■ The color of the text crosscuts both roles (drawing + validation)

► When should a role delegate to the player and when should a player delegate to its roles?

TextField

DataSource Child

User

(20)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 20

The ROP and Object Schizophrenia

► Clients always have to “ask” the core object

► The core object delegates the call to the respective role

► The core object represents the identity

► But, all of this has to be implemented manually!

■ Role management code

- AddRole, RemoveRole, Operation

■ Code for reflection

- HasRole, GetRole

► Roles need to be implemented aware of their core

■ Delegation to core object for every method call, as it could

be overridden by another role object, which is currently being played.

(21)

Design Patterns and Frameworks, © Dr. Sebastian Götz

21

11.4 Delegation vs. Forwarding

(22)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 22

Delegation vs. Forwarding

► What does this or self actually mean?

ClassA ClassB m1() m2() m1() m2() class B { A a; m1() { a.m1(); } m2() { print(“B”); } } a class A { m1() { this.m2(); } m2() { print(“A”); } } a = new A(); b = new B(a); b.m1();

(23)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 23

Delegation

► What does this or self actually mean?

Delegation Semantics:

this bound to delegatee (i.e., object “a”)

ClassA ClassB m1() m2() m1() m2() class B { A a; m1() { a.m1(); } m2() { print(“B”); } } a class A { m1() { this.m2(); } m2() { print(“A”); } } a = new A(); b = new B(a); b.m1();

(24)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 24

Forwarding

► What does this or self actually mean?

Forwarding Semantics: no delegation of this (i.e., this = b) ClassA ClassB m1() m2() m1() m2() class B { A a; m1() { a.m1(); } m2() { print(“B”); } } a class A { m1() { this.m2(); } m2() { print(“A”); } } a = new A(); b = new B(a); b.m1();

(25)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 25

Forwarding in Java

► Java does not directly support forwarding

► Workaround required

■ Passing this to the receiver

► Keep this in mind when implementing operations in the

Role-Object Pattern! ClassA ClassB m1(me : Object) m2() m1() m2() class B { A a; m1() { a.m1(this); } m2() { print(“B”); } } a class A { m1(Object me) { me.m2(); } m2() { print(“A”); } }

(26)

Design Patterns and Frameworks, © Dr. Sebastian Götz

26

11.5 Roles Types Formally

How role types differ

from other types.

(27)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 27

Rigidity

► Role types differ from natural types in terms of rigidity

■ Natural types are rigid ■ Role types are non-rigid

► Instances of a rigid type, cannot stop being of this type without

ceasing to exist

► Instances of a non-rigid type can!

■ You can stop being an employee without dying

- Employee is a role type

■ You cannot stop being a human

- Human is a natural type

► Instances of rigid types provide identity

► Instances of non-rigid types derive identity from players

► The non-rigidity property and the need for identity motivate

(28)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 28

Foundedness

► Role types differ from natural types in terms of foundedness

■ Natural types are non-founded ■ Role types are founded

► Instances of a founded type, cannot exist on their own; they always

need to be connected to another instance

■ Being a listener only works if there is a speaker

- Listener is a founded type

■ Being a tree does not have such a constraint

- Tree is a non-founded type

► Instances of founded types always require a counter-type against

which they are defined

► The foundedness property of role types motivates the need for at

(29)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 29 startNumber : int

Current Research on Role Types

Non-Founded Founded

Rigid Natural types Compartment Types Non-Rigid Phase types Role types

► Phase types don't have an own identity (non-rigid), but do not depend on

other types. They describe phases of an object.

■ For example, Child and Adult are phase types of Person

► Compartment types describe objectified collaborations

Rider Horse

Contest

Participant Jury Team

(30)

D r. S eb as tia n G öt z, D e si gn P a ttern s an d F ra m ew or ks 30

What have we learned?

► The Role-object Pattern

■ Realization of roles in object-oriented languages ■ Using delegation and forwarding

► Object Schizophrenia

■ Problem of identity

■ Problem of state management

► Formal properties of role types (and others)

■ Rigidity

References

Related documents

The mothers did not meet the Recommended Dietary Allowances (RDA) for Vitamin A, iron and zinc, while Food Consumption Score ( FCS ) tool categorized 33% of the mothers

The mean tick abundance per animal was 21.4 ticks, with the highest count of 111.8 ticks per animal in Tharaka Nithi district of Eastern province.. The highest prevalence rate of

Smaller θ (longer cone) was found to reduce the tangential field component along the cable insulation boundary but at the same time significantly increased the maximum total

Dual culture plate has been routinely used to select Trichoderma strains with antagonis- tic activity and omit those who do not present any biological potential (Philion,

Before the start of the competition element of the event, the Organiser may assemble all the competing vehicles in a start area, into which they must be driven

Combination of the pre- operative PSA level, biopsy gleason score, percentage of positive biopsies, and MRI T-stage to predict early PSA failure in men with clinically localized

The methodology consists of three parts, discussed in each of the following subsections: modeling of the high-lift geometry as separate three-dimensional components in OpenVSP