Thursday, August 31, 2006

Roles: Composable Units of Object Behavior

Roles: Composable Units of Object Behavior: "A significant new part of Perl 6’s object model and type system is the addition of roles. Part of their origin is an implementation in Smalltalk (there called traits). They also solve some systemic problems of other OO systems. Why are they useful and how do they work? What is a Role? A role is a named collection of behavior — a set of methods identified by a unique name. This resembles a class or a type, in that referring to the role refers to the combined set of behaviors, but it is more general than a class and more concrete than a type. Put another way, a role is an assertion about a set of capabilities. For example, a Doglike role identifies the important behavior that any doglike entity will possess: perhaps a tail attribute and the methods wag() and bark(). Even only this much is a great advantage to using roles. Presuming you have a method that needs something Doglike, you can ask “Does the class or object I receive behave in a"

No comments: