Sei sulla pagina 1di 13

Chapter 10

Designing classes

UML object constraint language UML graphical language with a set of rules and semantics. The rules and semantics are of the UML are expressed in English , in a form known as object constraint language (OCL) OCL It is a specification language that uses simple logic for specifying the properties of a system.

Item.selector selector is the name of an attribute in the item. John.age Item.selector[qualifier-value] john.phone[2] Set->select(boolean-expression) company.employees->salary>30000

Designing classes: the process 1.Apply design axioms to design classes,their attributes,methods,associations,structuresand protocols. 1.1 Refine and complete the static UML class diagram by adding details to that diagram. 1.1.1 Refine attributes 1.1.2 Design methods and the protocols by utilizing a UML activity diagram to represent the methods algorithm. 1.1.3 Refine the associations between class(if required) 1.1.4 Refine the class hierarchy and design with inheritance (if required) 1.2 Iterate and refine.

Visibility
A class might have a set of methods that it uses only internally, messages to itself. This private protocol of the class, includes messages that normally should not be sent from other objects. Here only the class itself can use the methods. The public protocol defines the stated behavior of the class as a citizen in a population and is important information for users as well as future descendants, so it is accessible to all classes. If the methods or attributes can be used by the class itself (or its subclasses) a protected protocol can be used. Here subclasses can used the method in addition to the class itself. The lack of well-designed protocol can manifest itself as encapsulation leakage. It happens when details about a classs internal implementation are disclosed through the interface.

Class visibility: Designing well-defined public, private and protected protocols


In designing methods or attributes for classes, we are confronted with two problems. One is the protocol or interface to the class operations and its visibility and the other is how it is implemented. The classs protocol or the messages that a class understands, can be hidden from other objects (private protocol) or made available to other objects (public protocol). Public protocols define the functionality and external messages of an object.

Private protocols define the implementation of an object.

Attributes
Attributes represent the state of an object. When the state of the object changes, these changes are reflected in the value of attributes. Single value attribute has only one value or state. (Eg). Name, address, salary Multiplicity or multivalue attribute can have a collection of many values at any time. (Eg) If we want to keep tact of the names of people who have called a customer support line for help.

Designing classes: Refining attributes


Attributes identified in object-oriented analysis must be refined with an eye on implementation during this phase. In the analysis phase, the name of the attribute is enough. But in the design phase, detailed information must be added to the model. The 3 basic types of attributes are: Single-value attributes Multiplicity or multivalue attributes Reference to another object or instance connection

Multi value attribute


Instance connection attributes are required to provide the mapping needed by an object to fulfill its responsibilities. (E.g.) A person may have one or more bank accounts. A person has zero to many instance connections to Account(s). Similarly, an Account can be assigned to one or more person(s) (joint account). So an Account has zero to many instance connection to Person(s).

UML attribute presentation visibility name : type-expression=initial-value Where visibility is one of the following: + Public visibility(accessibility to all classes) # Protected visibility(accessibility to subclasses and operations of the class) - Private visibility(accessibility only to operations of the class) type-expression is a language-dependent specification of the implementation type of an attribute initial-value is a language-dependent expression for the initial value of a newly created object. It is optional Example: +size:length = 100

Designing methods and protocols


A class can provide several types of methods: Constructor: Method that creates instances (objects) of the class Destructor: The method that destroys instances Conversion Method: The method that converts a value from one unit of measure to another. Copy Method: The method that copies the contents of one instance to another instance Attribute set: The method that sets the values of one or more attributes Attribute get: The method that returns the values of one or more attributes I/O methods: The methods that provide or receive data to or from a device Domain specific: The method specific to the application.

Five rules If it looks messy,then its probably a bad design If it is too complex,then its probably a a bad design. If it is too big,then its probably a a bad design. If people dont like it ,then its probably a a bad design. If it doesnt work,then its probably a a bad design.

Design issues: avoiding pitfalls 1.Keep a careful eye on the class design and make sure that an objects role remains well defined. 2.Move some functions to new classes that the object would use. 3.Break up the class into two or more classes. 4.Rethink the class definition based on experience gained.

Potrebbero piacerti anche