200 likes | 471 Views
Object Modeling (1). Chapter 3 (1) Part 1: Modeling Concepts Object-Oriented Modeling and Design Byung-Hyun Ha ( bhha@pusan.ac.kr ). Lecture Outline. Introduction Object and Classes Links and Associations. Introduction. Object model
E N D
Object Modeling (1) Chapter 3 (1) Part 1: Modeling Concepts Object-Oriented Modeling and Design Byung-Hyun Ha (bhha@pusan.ac.kr)
Lecture Outline • Introduction • Object and Classes • Links and Associations
Introduction • Object model • Static structure of a system by showing the objects in the system, relationships between the objects, and the attributes and operations that characterize each class of objects • Most important of the three models • Important concepts • Object, class, link, association, generalization, inheritance
Object and Classes • Objects • An object is simply something that makes sense in an application context • e.g. Joe Smith, Simplex company, Lassie, process number 7648 • Our definition • A concept, abstraction, or thing with crisp boundaries and meaning for the problem at hand • Purposes • Promote understanding of the real world • Provide a practical basis for computer implementation • Decomposition of a problem into objects • Depend on judgment and the nature of problem • There is no one correct representation
Object and Classes • Classes • A group of objects with similar properties (attributes), common behavior (operations), common relationships to other objects, and common semantics • Objects in a class • have the same attributes and behavior patterns • share a common semantic purpose • e.g. barn and horse with attributes of cost and age
Object and Classes • Classes (cont’) • We focus on object modeling, but why do we consider class? • By grouping objects into classes, we abstract a problem! • Common definitions and operations can be written once • publicclass Vector { • doublex; • doubley; • double length() { • double len = Math.sqrt(x * x + y * y); • return len; • } • }
Object and Classes • Classes (cont’) • Each object “knows” its class • Most object-oriented programming languages can determine an object’s class at runtime • An object’s class is an implicit property of the object
Object and Classes • Each object “knows” its class • a.k.a. Run-Time Type Information (RTTI) • instanceof keyword in case of Java • But, someone doesn’t like RTTI • publicclass Vector { • ... • publicboolean equals(Object obj) { • if (this == obj) { • returntrue; • } • if (obj instanceof Vector) { • Vector v = (Vector)obj; • returnx == v.x && y == v.y; • } • returnfalse; • } • }
Object and Classes • Two types of object diagram • Class diagram • Schema, pattern or template for describing many possible instances of data • Instance diagram • How a particular set of objects relate to each other • Useful for documenting test cases (especially scenarios) and discussing examples • A given class diagram corresponds to an infinite set of instance diagrams Person (Person) Joe Smith (Person) Mary Sharp (Person)
Object and Classes • Attribute • Data value held by the objects in a class • Should be a pure data value, not an object • Pure data values do not have identity • c.f. String object in case of Java • Each object has its own (internal) identity • Do not confuse internal identity with real-world attributes • e.g. SSN Person (Person) Joe Smith 24 (Person) Mary Sharp 52 name: string age: integer Person Person person ID: ID name: string age: integer name: string age: integer
Object and Classes • Operation • Function or transformation that may be applied to or by objects in a class • All object in a class share the same operations • Each operation has a target object as an implicit argument
Object and Classes • Operation (cont’) • Methods • The same operation may apply to many different classes • Such an operation is polymorphic • Method is the implementation of an operation for a class • Queries • Those that merely compute a function value without modifying any objects • e.g. length() method of our Vector • Base attribute vs. derived attributes • The choice of base attributes is arbitrary but should be made to avoid over-specifying the state of object • e.g. (x, y) vs. (ux, uy) & len in case of our Vector
Object and Classes • Operation (cont’) Person Geometric object name age color position change-job change-address move(delta: Vector) select(p: Point): Boolean rotate(angle)
Links and Associations • Link • A physical or conceptual connection between object instances • Association • A group of links with common structure and common semantics • Inherently bidirectional Has-capital Country City name name (Country) Canada Has-capital (City) Ottawa (Country) France Has-capital (City) Paris (Country) Senegal Has-capital (City) Dakar
Links and Associations • Association (cont’) • Associations are often implemented in programming language as “pointers” from one object to another • e.g. “references” in case of Java Works-for Person Company • publicclass Person { • Company employer; • } • publicclass Company { • Person employee; • }
Links and Associations • Association (cont’) • Implementing associations as pointers is perfectly acceptable, but associations should not be modeled this way (why?) Works-for Person Company • publicclass Person { • Company employer; • } • publicclass Company { • Person employee; • } • publicstaticvoid main(String[] args) { • Person p = new Person(); • Company c = new Company(); • p.employer = c; • c.employee = p; • }
Links and Associations • Example in CAD • Lines and points • c.f. multiplicity symbols (solid balls and “2+”) • Sample data • Could you draw an instance diagram for the sample data? Intersects Line Point 2+ name name L1 L2 L3 L4 P1 P2 L5
Links and Associations • Order of association • Binary, ternary, or higher order • e.g. ternary association Project Language Person (Project) accounting (Language) Cobol (Person) Mary (Project) CAD (Language) C
Links and Associations • Order of association (cont’) • Q: same or not? Project Language Person Project Language Person
Homework • HW6: exercise 3.1 (p. 49) • HW7: write Java code of representing the instance diagram in Figure 3.7 (p. 29)