1 / 48

Object-Oriented Programming (Java)

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.

lauritsen
Download Presentation

Object-Oriented Programming (Java)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming (Java)

  2. 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

  3. 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

  4. 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.

  5. 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.

  6. 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

  7. 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 (部署图)

  8. 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.

  9. UML Class Diagram name attribute method • - private • + public • # protected

  10. 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

  11. Representation of Employee

  12. Abbreviated Representation (1)

  13. Abbreviated Representation (2)

  14. 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

  15. 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

  16. One-way Associations (单向关联) • A class has associations with more than one class. Country -capital -goverment Goverment capital

  17. One-way Associations (单向关联) • A class can contain more than one association with another class (一个类和另一个类可以有多个关联) Flight -coPilot -pilot Pilot

  18. Two-way Associations(双向关联) • A two-way association indicates bi-directional navigation between objects of two classes. • 双向关联中每个类都有另外一个类的对象的引用。 Student Course -courses -students

  19. 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代表最大值

  20. Common multiplicities

  21. 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

  22. -engine Car Engine Country -capital -goverment Goverment capital One-to-one Association(一对一关联) 1 1 1 1 1 1

  23. -account Client BankAccount Month Week Year -weeks -months -days Day One-to-many Association(一对多关联) 1 1…* 1 12 1 4…5 1 7

  24. 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…*

  25. Many-to-many Association • Find the association between Person and Company -employee -employer Person Company 1…* 0…1

  26. -employee -employer Person Company 1…* 0…1 Many-to-many Association 1 “One person can work forat most one employer.”

  27. -employee -employer Person Company 1…* 0…1 Many-to-many Association 1 “One company hasat least one employee.”

  28. Many-to-many Association • Find associations between Employee, Department and Project. 1…* 1…* Employee Department -employees -departments -participants 1…* -projects 1…* Project

  29. Aggregation(聚集) • Aggregation is a special form of association. • Models a whole/part relationship. • Represents a “has-a” relationship

  30. 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.

  31. Aggregation hierarchy of a book

  32. 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.

  33. Specialization/Generalization • 具体化/概括化 • It is a “is a ” relationship • For example: • Client is a person.

  34. Specialization/Generalization notation • Specialization/generalization is represented in UML by adding a triangle next to the generalization class:

  35. Specialization/Generalization (cont.) • How do we structure these classes?

  36. Specialization/Generalization (cont.) • Employee and Manager are Person • Manager is an Employee

  37. Specialization/Generalization (cont.) • The associate is a inheritance relationship.

  38. 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

  39. 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(关系环)

  40. Collection • A collection models a one-to-many relationship. • Collections store many instances of one class. • For example:

  41. Collections • 在该类中有一个属性是BankAccout类的实例集合; • 每个BankAccout类的实例都有一个索引值; • 该类中提供了增加和获取BankAccout类的实例的方法; 集合

  42. Self-containing • A class can have an association with itself. • 一个类有一个与其自身的关联,即该类包含一个同类对象的引用作为其属性;

  43. 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.

  44. 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

  45. Class Diagram of a Files System

  46. 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”.

  47. UML Class Diagram Tools • Eclipse • PPT • Microsoft Visio • Violet • IBM Rational Rose

  48. 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

More Related