820 likes | 1.28k Views
9. Object-Oriented design and UML (Unified Modeling Language). In this part you will learn: Definition of Object-Oriented design Encapsulation Information hiding Classes and Objects State retention Object identity Messages Inheritance Association Polymorphism Generosity
E N D
9. Object-Oriented design and UML (Unified Modeling Language) In this part you will learn: • Definition of Object-Oriented design • Encapsulation • Information hiding • Classes and Objects • State retention • Object identity • Messages • Inheritance • Association • Polymorphism • Generosity All of these concepts are represented with UML notation.
8.1 Definition of Object-Oriented design Before giving a definition, let’s first understand the difference in the principles of structured design and object-oriented design. Structured design: function-oriented or model-oriented. For example, a cash dispenser is modeled as follows:
It is difficult to give a definition for Object-Oriented design mathematically, but we can describe what object-orientation is about in an informal manner. Description: object-oriented design (OOD) means to build programs by means of designing objects and their relations. In other words, in OOD identification and design of objects and their relations are the most important tasks. If all of the necessary objects and their relations are well designed, the entire program will be well designed.
What is an object in general Description: an object is a material thing that can be seen or touched. Examples: • A watch: a place to show time + operations (e.g., (1) adjust time, (2) set alarm, (3) adjust dates) • A TV set: a screen + operations (e.g., (1) change channels, (2) adjust volume, (3) Change modes) • A book: a set of pages, but no operations to offer. • A person: physical body + operations (e.g., (1) eat, (2) walk, (3) drink, (4) sleep)
8.2 Encapsulation Object-oriented encapsulation is the packaging of operations and attributes representing state into an object type so that state is accessible or modifiable only via the interface provided by the encapsulation. • Attributes are represented by instance variables in a Java class. • Operations are like methods of a Java class. • State is a collection of instance variables together with their values. • Interface are like public methods of a Java class.
The illustration of encapsulation Attribute: reg Operations: Add, Subtract, Multiply State: reg; Interface: Add, Subtract class Calculator { private int reg; publicCalculator() { initialize reg; } //constructor of the class publicint Add(int i) { reg = reg + i; } publicint Subtract(int i) { reg = reg – i; } privateint Multiply(int i) { reg = reg * i; } }
The change of interface Attribute: reg Operations: Add, Subtract, Multiply State: reg; Interface: Add, Subtract, Multiply class Calculator { private int reg; publicCalculator() { initialize reg; } //constructor of the class publicint Add(int i) { reg = reg + i; } publicint Subtract(int i) { reg = reg – i; } publicint Multiply(int i) { reg = reg * i; } }
8.3 Information hiding Information hiding is the use of encapsulation to restrict from external visibility certain information or implementation decisions that are internal to the encapsulation structure. Information means: • Attributes. (1) the implementation details of attributes, including how they are stored and named, are hidden, (2) they are usually not directly accessible by outside operations. • Implementation details of all the operations. For example:
class Calculator { private int reg; //hidden attribute public Calculator() { initialize reg; } //constructor of the class publicint Add(int i) { //interface operation reg = reg + i; } publicint Subtract(int i) { //interface operation reg = reg – i; } publicint Multiply(int i) { //interface operation reg = reg * i; } }
8.4 Classes and objects Description(class):a class can be understood in both static and dynamic views: • Static view: a class is a specification that defines the attributes and operations which its every object shares. Therefore, a class is what you design and program. • Dynamic view: a class is a collection of objects that share the same features (i.e., with the same kinds of attributes and operations). Therefore, objects are what you create (from a class) at run-time.
Description(object):an object is an instance of a class. In other words, an object can be instantiated from a class. Graphical illustration of class and objects: x1, x2, and x3 represent objects of the class that has three operations Op1, Op2, and Op3. Op1 x1 class Op2 x2 x3 Op3
Graphical representations of classes and objects in UML UML: Unified Modeling Language. Initially, UML was proposed by Bouch, Jacobson, and Rumbaugh, and then standardized by Object Management Group (OMG).
Example of class / age means the attribute is not directly settable, it is read-only variable. Keywords: in, out, inout. in argument: input of the method. in is usually omitted. out argument: output of the method. inout argument: both input and output of the method.
Suppose that three objects are instantiated from class Person: object1 object2 object3 person1 person2 person3 var name var dateOfBirth var height var age var name var dateOfBirth var height var age var name var dateOfBirth var height var age setName getName getHeight setHeight setName getName getHeight setHeight setName getName getHeight setHeight
Since the methods are program codes, and can be shared by all of the objects instantiated from the same class, the implementation of objects of class Person at run time is in fact as follows: methods object1 object2 var name var dateOfBirth var height var age var name var dateOfBirth var height var age setName getName getHeight setHeight var name var dateOfBirth var height var age object3
8.5 State retention Definition (state): state of an object is a set of attributes, which is subject to change during the operation of the program. For example, the object realPerson has a state: name = “Mike” dateOfBirth = 25.2.1965 height = 175 age = 20
Description:state retention means that the state of an object is retained after it has finished executing its methods. In other words, an object sustains its state until it dies (e.g., garbage collection). This point is different from a function in C and a procedure in Pascal: when calling a function or procedure, the state of the function or procedure is created, and when the function or procedure terminates, its state vanishes.
8.6 Object identity Object identityis the property by which each object (regardless of its class or current state) can be identified and treated as a distinct software entity. In other words, every object has a unique identity. Such an object identity is called object reference or object handle. Such a handle is decided when an object is created with the new operator. It is a common way to use the address of the object (the starting memory unit of its attributes) as its handle.
Example Person p1 = new Person(); Person p2 = new Person(); 602237 P1 objects 142857 P2 p2 = p1; object 602237 P1 This object has died. 602237 P2
8.7 Messages Description: A message is the vehicle by which a sender object obj1 conveys to a target object obj2 a demand for object obj2 to apply one of its methods. obj2.m1(in a, out b) obj2 obj1 Sending a message is like calling a traditional function or procedure: call m1(obj2, a, b)
8.7.1 Message structure The general structure of a message is: obj2.m1(in a, b, c, out x, y, z) method name output arguments target object input arguments message
8.7.2 The roles of objects in messages An object may play the following roles in messages: • The sender of a message (e.g., obj1). • The target of a message (e.g., obj2). • Pointed to by an argument passed back or forth in a message (e.g., obj). obj2.m1(in a, b, c, out x, y, z, inout obj) obj1 obj2
8.7.3 Types of messages There are three types of messages: • Informative messages • Interrogative messages • Imperative messages
Informative messages An informative message is a message to an object that provides the object with information to update itself. It is also known as update, forward, or push message. For example, person1.setName(name1) In this message object person1 is informed that its name is changed to name1. Therefore, person1 needs to update its name using the method setName.
Interrogative message An interrogative message is a message to an object requesting it to reveal some information about itself. (It is also known as a read, backward, or pull message.). For example, person1.getName() Object person1 is requested to tell its name by calling its getName method.
Imperative message An imperative message is a message to an object that requests the object to take some action on itself, another object, or even the environment around the system. (It is also known as a force or action message.) For example, person1.sendName(obj); This message requests object person1 to send its name to another object obj of class Person.
8.8 Inheritance Question: In your design, if you wrote a class C and then later discovered a class D that was almost identical to C except for a few extra attributes or operations, what would you do? Example: class C { class D { T1 a1; a1, a2, method1, and T2 a2; method2 of C are method1; needed here; method2; method3; } }
Two solutions: • Duplicate all the attributes and operations of C and put them into D. class C { class D { T1 a1; T1 a1; T2 a2; T2 a2; method1; method1; method2; method2; } method3; }
Have class D use the attributes and operations of the class C. This solution is called inheritance. Description: Inheritance (by class D from C) is a mechanism that allows a class D to have implicitly the attributes and operations of class C, as if those attributes and operations had been defined upon D itself. In other words, through inheritance, objects of class D can make use of attributes and operations that would otherwise be available only to objects of class C.
Inheritance represents another major way in which object orientation departs from traditional systems approaches. It effectively allows you to build software incrementally in this way: • First, build classes to cope with the most general case. • Then, in order to deal with special cases, add more specialized classes that inherit from the first class. These new classes will be entitled to use all the operations and attributes (both class and instance operations and attributes) of the original class.
Definition:if class B inherits from class A, B is known as a subclass of A, and A is a super class of B. Inheritance is transitive. That is, if C inherits from B, and B inherits from A, then C will definitely inherit from A. In UML, the inheritance hierarchy: classes B, C, and D inherit from A, class E inherits from C, are represented by the following graphical notation.
Class inheritance hierarchy:
Multiple inheritance Description: multiple inheritance is a mechanism that allows a class to inherit from more than one super classes. For example, a Professor can be an Employee and an Employer simultaneously.
The problem in multiple inheritance The problem is the conflict of attribute or operation names in super classes. Both super classes have the attribute: name, and the method: getName.
Solutions for the problem in multiple inheritance • Resolve the conflict of attribute and operation names in the super classes. • Run time error • Check by the complier • Disallow the multiple inheritance (e.g., Java)
Exercise 7 • Describe the relationship between objects and classes. (2) Explain what is object-oriented design. (3) Suppose Animal be the super class of classes Dog, Cow, and Cat; Cat is the super class of classes WhiteCat and BlackCat. Draw the class inheritance hierarchy of these classes.
8.9 Association Description: an association (known as binary association)represents a relation between two classes, where a relation is a set of relationships between instances of the classes. Example: let LibraryPatron and LibraryBook be two classes. An association between them is Borrowing, a set of relationship links stating which patron is currently borrowing which book. As an example, the Borrowing association may contain the following four links:
Jeff is borrowing Program Design Chris is borrowing Programming in Java John is borrowing Software Engineering Jim is borrowing Formal Methods Notice that borrowing represents a bunch of links, each reflecting a relationship between two instances of two classes.
8.9.1 The basic UML notation for associations The following figure shows three associations: Employment between classes Person and Company; Residence between Person and County; and Site between County and Company. employee 0..1 Company Person Employment 0..* employer resident 0..* 0..* Residence Site 1..* 1..1 County
The meaning of each component of this diagram: • The box represents a class. • The line between two classes represents a relation (association) between the two classes. A relation has a name attached to the line. The name of a relation usually starts with a capital letter, like Employment and Residence. • The role of a class in the association diagram may appear beside it at the end of a association line, such as employee and residence. • The multiplicity of the association appears at the ends of each line. For example, 0..1 at the end of Employment association, beside class Company, means that a given person is an employee of 0 or 1 company, while 0..* at the end of the same association line beside class Person means that a given company is an employer of 0 to many (denoted by an asterisk *) persons.
Some important points about UML notation for associations: • The name of an association should be a noun, because it represents a class in implementation, as we will explain later. • UML doesn’t insist on a name for an association, but it is a good discipline to give a name for each association in general. • UML doesn’t require role names of classes either. The role names should be given whenever it is necessary in avoiding the confusion of the meaning of associations. • The multiplicity can be abbreviated. For example, 0..* can be written as *, and 1..1 can be written as 1. But notice that 0..1cannot be written as 1.
8.9.2 Associations depicted as classes An association between two classes can be depicted as a class. This also indicates a way to implement an association in the program code.
Employment is depicted as a class in which Person and Company may be used to declare instance variables (representing attributes of its objects),like employee, employer, startDay, and terminationDay. Operations may be defined in this class for providing necessary services, such as setStartDay() and GetTerminationDay().
8.9.3 Higher-order associations A higher-order association is an association among more than two classes. A diamond is used to represent a higher-order association. Example, the following Figure shows part of a purchasing model for buying items from vendors. In this business, the unit price depends on three factors: the item type (the product), the company that is selling it (the vendor), and the quantity of items you purchase (the price-break level).