Object-Oriented Programming (Java) Topics Covered Today • Unit 1.2 Designing Classes – – – – 1.2.1 UML Class Diagrams 1.2.2 Relationships Between Classes 1.2.3 Common Class Structures 1.2.4 UML with Eclipse 2 UML Introduction • Programming is like building a house. An architect creates a design, and a builder uses appropriate tools to carry out the design. The builder does not proceed without a blueprint from the architect. • Software developers also need a blueprint to create complex systems. • The UML is a toolbox of graphical notations used to produce the blueprint -- a graphical depiction of the software design • UML, Unified Modeling Language 3 Unified Modelling Language (UML) • A graphical language for – – – – Visualising Specifying Constructing Documenting • Object-oriented software systems • OMG (Object Management Group国际对象管理 组织) is responsible for UML standard. 4 UML • UML is not a method. It does not outline a procedure for designing software; it is a modeling language that captures the design graphically. • Whatever process you use, you can use UML to record the results of your analysis and design decisions. 5 UML • The main reason to use UML notation is communication. – To discuss design with someone, both need to understand the modeling language, not the process used to come up with the design. – Human language is imprecise – Code is too detailed 6 UML Diagrams • Use case diagram (用例图) • Class diagram (类图) • Behavior diagrams (行为图) – Statechart diagram (状态图) – Activity diagram (活动图) – Interaction diagrams (交互图) • Sequence diagram (顺序图) • Collaboration diagram (协作图) • Implementation diagrams (实现图) – Component diagram (组件图) – Deployment diagram (部署图) 7 Class Notation • A class is represented by a rectangle with three compartments. – The first compartment contains the name of the class; – the second compartment describes the attributes of the class; – and the third compartment describes the methods of the class. 8 UML Class Diagram NameClass -attribute1 : Type -attribute2 : Type name attribute method +method1(param1 : Type, param2 : Type) : Type +method2(param1 : Type, param2 : Type) : Type • - private • + public • # protected 9 Employee Class • This class contains three private attributes: – name – hourlyWage – hourWorked • It also contains the following public methods: – Methods to access the values of the attributes: • getName, getHourlyWage, and getHoursWorked – Methods to modify the values of the attributes: • setName, setHourlyWage, and setHoursWorked – A method to obtain the employee's earnings: • getEarnings 10 Representation of Employee Employee -name : String -hourlyWage : Double -hoursWorked : Double +getName() : String +getHourlyWage() : Double +getHoursWorked() : Double +setName(newName : String) +setHourlyWage(newHourlyWage : Double) +setHoursWorked(newHoursWorked : Double) +getEarnings() : Double 11 Abbreviated Representation (1) Employee -name : String -hourlyWage : Double -hoursWorked : Double +getName() +getHourlyWage() +getHoursWorked() +setName(newName : String) +setHourlyWage(newHourlyWage : Double) +setHoursWorked(newHoursWorked : Double) +getEarnings() 12 Abbreviated Representation (2) Employee Employee -name : String -hourlyWage : Double -hoursWorked : Double +getEarnings() 13 Associations (关联) • An association represents the relationship between two or more classes. – 关联就是类和类之间的关系。 • Association is represented by a solid line that connects the two classes. • For instance: – a professor instructs students Student Professor – clients hold bank accounts Client BankAccount 14 One-way Associations (单向关联) • In a one-way association, the first class has a reference to an object of the second class, but the second class does not have a reference to an object of the first class. – 单向关联中,第一个类中有一个第二个类的对象的 引用,但第二个类中没有第一个类的对象的引用 • UML indicates a one-way association with an arrow at the end of the association line. Car Engine -engine 15 One-way Associations (单向关联) • A class has associations with more than one class. Country -capital -goverment Goverment capital 16 One-way Associations (单向关联) • A class can contain more than one association with another class (一个类和另一个类可以有多个关 联) Flight -coPilot -pilot Pilot 17 Two-way Associations(双向关联) • A two-way association indicates bi-directional navigation between objects of two classes. – 双向关联中每个类都有另外一个类的对象的引用。 Course -students -courses 18 Student Multiplicity(多重性) • Multiplicity indicates the number of instances of a class that may be associated with a single instance of another class. – 多重性就是一个类有多少个实例与另外一个类的实例相关 联 – Car has four tires and one engine – One client holds one or more bank accounts – A professor can instruct more than one student • The multiplicity can be specified with a single integer or as a range – n,n=1,2,3,… – n…m,n代表最小值,m代表最大值 19 Common multiplicities 0…1 Zero or one instance 0…* or * Zero or more instances 1 Exactly one instance 1…* One or more instances 20 Multiplicity • States the number of objects that are connected across the association. – Unspecified – Exactly one – Zero or more (many, unlimited) 1 0..* * – – – – 1..* One or more Zero or one Specified range Multiple, disjoint ranges 0..1 2..4 2, 4..6 21 One-to-one Association(一对一关联) -engine Car 1 1 -goverment 1 Engine 1 Country 1 1 Goverment -capital capital 22 One-to-many Association(一对多关联) -account Client Year 1…* 1 1 12 -months 1 Month BankAccount 4…5 -weeks Week 1 7 -days Day 23 Many-to-many Association(多对多关联) • In a many-to-many association between classes A and B, one instance of class A may be related with many instances of class B, and one instance of class B may be related with many instances of class A. Student -student 0…* -course 0…* 24 Course Many-to-many Association • Find the association between Person and Company Person -employee 1…* -employer 0…1 25 Company Many-to-many Association Person -employee 1 1…* -employer 0…1 Company “One person can work for at most one employer.” 26 Many-to-many Association Person -employee 1…* -employer 1 0…1 Company “One company has at least one employee.” 27 Many-to-many Association • Find associations between Employee, Department and Project. 1…* Employee -employees -participants 1…* -projects 1…* 1…* Department -departments Project 28 Aggregation(聚集) • Aggregation is a special form of association. • Models a whole/part relationship. • Represents a “has-a” relationship 29 Aggregation Example • A book is composed of one table of contents, zero or one prefaces, one or more chapters, and one index. In a book, each chapter is composed of one or more sections, and each section is composed of one or more paragraphs and zero or more figures. 30 Aggregation hierarchy of a book 31 Composition(合成) • Models a whole/part relationship with a strong ownership; when the whole dies, the part does so as well. • The whole part is responsible for creation and destruction of its parts. RootSystem Plant Stem 32 Specialization/Generalization • 具体化/概括化 • It is a “is a ” relationship • For example: – Client is a person. 33 Specialization/Generalization notation • Specialization/generalization is represented in UML by adding a triangle next to the generalization class: 34 Specialization/Generalization (cont.) • How do we structure these classes? 35 Specialization/Generalization (cont.) • Employee and Manager are Person • Manager is an Employee 36 Specialization/Generalization (cont.) • The associate is a inheritance relationship. 37 Specialization/Generalization (end) • A subclass inherits from its superclass: – Attributes – Operations – Relationships. • A subclass may add to its definition: – – – – Attributes Operations Relationships Redefine inherited operations 38 Common Class Structure • Although the definition of classes and their relationships depends on the particular application, some class structures are common to many designs. • These class structures are thought of as basic building blocks and can be composed to design complex systems. • Some of common class structure: – Collection(集合) – Self-containing(自容类) – Relationship loops(关系环) 39 Collection • A collection models a one-to-many relationship. • Collections store many instances of one class. • For example: 40 Collections • 在该类中有一个属性是BankAccout类的实例集合; • 每个BankAccout类的实例都有一个索引值; • 该类中提供了增加和获取BankAccout类的实例的方法; 集合 41 Self-containing • A class can have an association with itself. • 一个类有一个与其自身的关联,即该类包含一 个同类对象的引用作为其属性; 42 Self-Containing Classes • The following diagram shows the relationship "each employee has one boss." • The diagram indicates that the boss of an employee is another employee. 43 Relationship Loops • Self-containment can be found in relationships that include two or more classes – 在多个类之间存在自容关系,就会形成关系环。 • An example of a self-containment loop is a file system. – A file system has folders, and folders contain files or more folders, or both 44 Class Diagram of a Files System 45 Roundup: Class Diagrams • Class Diagrams … – show a set of classes and their relationships to each other. – illustrate the static view of a system. – are different from “object diagrams”. 46 UML Class Diagram Tools • • • • • Eclipse PPT Microsoft Visio Violet IBM Rational Rose 47 UML with eclipse • • • • • • • • • • Introduction Create Project Create Folder Create New Class Diagram Create Class BankAccount Create Class Person Create Class Client Create Specialization/Generalization Relationship Create Association Relationship Export Image 48