480 likes | 500 Views
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. UML Introduction.
E N D
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
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
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.
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.
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
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 (部署图)
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.
UML Class Diagram name attribute method • - private • + public • # protected
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
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 • clients hold bank accounts Student Professor Client BankAccount
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
One-way Associations (单向关联) • A class has associations with more than one class. Country -capital -goverment Goverment capital
One-way Associations (单向关联) • A class can contain more than one association with another class (一个类和另一个类可以有多个关联) Flight -coPilot -pilot Pilot
Two-way Associations(双向关联) • A two-way association indicates bi-directional navigation between objects of two classes. • 双向关联中每个类都有另外一个类的对象的引用。 Student Course -courses -students
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代表最大值
1 0..* * 1..* 0..1 2..4 2, 4..6 Multiplicity • States the number of objects that are connected across the association. • Unspecified • Exactly one • Zero or more (many, unlimited) • One or more • Zero or one • Specified range • Multiple, disjoint ranges
-engine Car Engine Country -capital -goverment Goverment capital One-to-one Association(一对一关联) 1 1 1 1 1 1
-account Client BankAccount Month Week Year -weeks -months -days Day One-to-many Association(一对多关联) 1 1…* 1 12 1 4…5 1 7
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 -course Student Course 0…* 0…*
Many-to-many Association • Find the association between Person and Company -employee -employer Person Company 1…* 0…1
-employee -employer Person Company 1…* 0…1 Many-to-many Association 1 “One person can work forat most one employer.”
-employee -employer Person Company 1…* 0…1 Many-to-many Association 1 “One company hasat least one employee.”
Many-to-many Association • Find associations between Employee, Department and Project. 1…* 1…* Employee Department -employees -departments -participants 1…* -projects 1…* Project
Aggregation(聚集) • Aggregation is a special form of association. • Models a whole/part relationship. • Represents a “has-a” relationship
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.
RootSystem Plant Stem 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.
Specialization/Generalization • 具体化/概括化 • It is a “is a ” relationship • For example: • Client is a person.
Specialization/Generalization notation • Specialization/generalization is represented in UML by adding a triangle next to the generalization class:
Specialization/Generalization (cont.) • How do we structure these classes?
Specialization/Generalization (cont.) • Employee and Manager are Person • Manager is an Employee
Specialization/Generalization (cont.) • The associate is a inheritance relationship.
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
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(关系环)
Collection • A collection models a one-to-many relationship. • Collections store many instances of one class. • For example:
Collections • 在该类中有一个属性是BankAccout类的实例集合; • 每个BankAccout类的实例都有一个索引值; • 该类中提供了增加和获取BankAccout类的实例的方法; 集合
Self-containing • A class can have an association with itself. • 一个类有一个与其自身的关联,即该类包含一个同类对象的引用作为其属性;
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.
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
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”.
UML Class Diagram Tools • Eclipse • PPT • Microsoft Visio • Violet • IBM Rational Rose
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