We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22
Advanced classes
Advanced classes
• Classes are indeed the most important building block of any
object-oriented system. However, classes are just one kind of an even more general building block in the UML classifiers. • Classifiers: A classifier is a mechanism which describes structural and behavioral features. • Beyond these basic features, there are several advanced features like multiplicity, visibility, signatures, polymorphism and others. • Classifiers include classes, interfaces, data types, signals, components, nodes, use cases, and subsystems. • In general, all the modeling elements that can have instances are called as classifiers. For example, classes have instances known as objects. Some elements like packages and a generalization does not have instances. • Classifiers in UML are as follows: Visibility • One of the important features that can be applied to the classifier’s attributes and operations is the visibility. The visibility feature specifies whether a classifier can be used or accessed by the other classifiers. The four access specifiers in UML are as shown below • The UML's visibility property matches the semantics common among most programming languages, including C++, Java, Ada. • Another feature that can be applied to the classifier’s attributes and operations is the scope. The scope of an attribute or an operation denotes whether they have their existence in all the instances of the classifier or only one copy is available and is shared across all the instances of the classifier. • A classifier scoped feature is graphically represented by underlining the feature within the classifier. All other normal features are treated as instanced scope. • Classifier scoped maps to what C++ calls static attributes and operations • Abstract, Root, Leaf, and Polymorphic Elements • While developing class diagrams, it is common to use generalization relationship to model generalization- specialization relationships between a group of classes. • The classes at the top of the hierarchy are more generalized or abstract and the classes lower in the hierarchy are more specialized. To represent abstract classes for which we cannot create instances, the class name is written in italics in UML. To represent abstract methods, we write the operation signature in italics. • We can also represent leaf and root classes in UML. A class which has no child classes is known as a leaf class. Such a class can be represented by writing leaf as a property under the class name • a class with no parents is known as a root class and such a class can be represented by writing root as a property as a property under the class name. OKButton is a leaf class, so it may have no children. • • Icon is a root class. • Operations have similar properties. Typically, an operation is polymorphic, which means that, in a hierarchy of classes, you can specify operations with the same signature at different points in the hierarchy. • display and isInside are both polymorphic operations. Furthermore, the operation Icon::display() is abstract, meaning that it is incomplete and requires a child to supply an implementation of the operation. In the UML, you specify an abstract operation by writing its name in italics. • By contrast, Icon::getID() is a leaf operation, so designated by the property leaf. This means that the operation is not polymorphic and may not be overridden Multiplicity • When modeling classes it is reasonable to assume that a class can have any number of instances. In some cases a class might not have any instances at all. Such a class with no instances is called a utility class which contains only attributes and operations with the classifier scope. • The number of instances a class may have is called its multiplicity. • you can specify the multiplicity of a class by writing a multiplicity expression in the upper-right corner of the class icon. For example, in Figure 9-6, NetworkController is a singleton class. Similarly, there are exactly three instances of the class ControlRod in the system. • Multiplicity applies to attributes, as well. You can specify the multiplicity of an attribute by writing a suitable expression in brackets just after the attribute name. • For example, in the figure, there are two or more consolePort instances in the instance of Network Controller. • Attributes: • While modeling classes, it is reasonable to write only the attribute name. Along with the name, you can also specify multiplicity, visibility and scope. Apart from these features, we can also specify the type, initial value and changeability of each attribute. The syntax of an attribute in UML is: • [visibility] name [multiplicity] [: type] [=initial value] [{property string}] The three predefined properties that can be used with attributes in UML are : • The frozen property maps to const in C++. • Operations • While modeling classes, it is simple to write only the operation name. Along with the name we can also mention the visibility and scope. Apart from these basic details, we can also specify the return type, parameters, concurrency semantics and other features. The syntax of an operation in UML is: [visibility] name [(parameters-list)] [: return-type] [{property- string}] • • The signature of parameter in the parameters list of an operation is as follows: [direction] name :type [=initial value] • Direction may be anyone of the following values: • The operation name, parameters list and its return type is collectively known as the signature of the operation. • There are four predefined properties (stereotypes) that can be applied to operations, in UML. They are: Template Classes • A template is a parameterized element. In such languages as C++ and Ada, you can write template classes, each of which defines a family of classes. • A template includes slots for classes, objects, and values, and these slots serves as the template's parameters. • You can't use a template directly; you have to instantiate it first. Instantiation involves binding these formal template parameters to actual ones. template <class Item, class Value, int Buckets> class Map { public: virtual Boolean bind(const Item&, const Value&); virtual Boolean isBound(const Item&) const; ... }; • You might then instantiate this template to map Customer objects to Order objects. • m : Map <Customer, Order, 3>; Standard Elements • The UML defines four standard stereotypes that apply to classes. • 1. metaclass Specifies a classifier whose objects are all classes • 2. powertype Specifies a classifier whose objects are the children of a given parent • 3. stereotype Specifies that the classifier is a stereotype that may be applied to other elements • 4. utility Specifies a class whose attributes and operations are all class scope Common Modeling Techniques • Modeling the Semantics of a Class • To model the semantics of a class: • Specify the responsibilities of the class in a note and attach it to the class with a dependency relationship. • Specify the semantics of a class as a whole in a note stereotyped with “semantics”. • Specify the body of a method using structured text or a programming language in a note and attach it to the class or operation using a dependency relationship. • Specify the pre-conditions and post-conditions for operations using structured text in a note. • Specify a state machine for a class which represents different states the object undergoes. • Specify a collaboration that represents a class.