Скачать презентацию OO programming and UML Design Dr Zhen Jiang Скачать презентацию OO programming and UML Design Dr Zhen Jiang

83bbd1b8f6432ad459e60b9bc990d8f3.ppt

  • Количество слайдов: 71

OO programming and UML Design Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa. edu OO programming and UML Design Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa. edu

Outline l Introduction to object and class l Introduction to UML design Outline l Introduction to object and class l Introduction to UML design

Introduction to object and class l Why OO programming? – Look at this picture Introduction to object and class l Why OO programming? – Look at this picture carefully – You need to identify it from VERY similar pictures in the next slide

Introduction to object and class l Why OO programming? – Which one is shown Introduction to object and class l Why OO programming? – Which one is shown before? – In the next slide, your partner will see the selected picture and other two pictures together. Write down something to help your partner to pick up the same picture correctly.

Introduction to object and class l Why OO programming? – Partner’s job Introduction to object and class l Why OO programming? – Partner’s job

Introduction to object and class l Why OO programming? – Which one? Introduction to object and class l Why OO programming? – Which one?

Introduction to object and class l Why OO programming? – From snapshot to the Introduction to object and class l Why OO programming? – From snapshot to the action

Introduction to object and class l Why OO programming? – From snapshot to the Introduction to object and class l Why OO programming? – From snapshot to the action 3 2 1

Introduction to object and class l What’s object – Identity – State – Behavior Introduction to object and class l What’s object – Identity – State – Behavior Sequence Diagram l Statechart Diagram – Messages and methods l l What’s – – – class Objects and Classes Class Attributes Operation (Method) Interfaces and Implementation

What’s object l Fundamentals Programming of Object-Oriented What’s object l Fundamentals Programming of Object-Oriented

What’s object l Conceptually, there are many ways to think of an object – What’s object l Conceptually, there are many ways to think of an object – something that can be seen or touched – a thing to which some action is directed – something that performs an action l The structure and behaviour of similar objects are defined in their common class l Objects have three properties: identity , state, and behaviour

Object Property 1: Identity l Identity is that property of an object which distinguishes Object Property 1: Identity l Identity is that property of an object which distinguishes it from all other objects l Most programming languages use variable names to refer to objects l Keep in mind, however, that an object may not have a name; Similarly, an object might have multiple names (aliases) – For this reason, there is a subtle distinction made between the concepts of "name" and "identity"

Object Property 2: State l The state of an object encompasses all of the Object Property 2: State l The state of an object encompasses all of the descriptions of the object plus the current values of each of these descriptions

Object Property 3: Behaviour l Behaviour is how an object acts and reacts, in Object Property 3: Behaviour l Behaviour is how an object acts and reacts, in terms of its state changes and message passing l The state of an object represents the cumulative results of its behaviour l In object-oriented programming, a behaviour is invoked by sending a message to an object

Example Objects l There are many physical objects we can examine right in this Example Objects l There are many physical objects we can examine right in this room – each person is an object – any chair is not an object – each light bulb is an object – Any book is not an object – this room itself is an object (full or not)

l See if the followings are objects or not: – – – – – l See if the followings are objects or not: – – – – – l Desk Lamp Person Log The Earth Clock Machine Computer Saving account Answer: N, Y, Y, Y

What’s the relationship among objects: Messages and Methods l. A object executes a method What’s the relationship among objects: Messages and Methods l. A object executes a method when it sends/receives a message l For example, we can ask/tell a dog to sit by sending him the message "sit" l In object-oriented programming, objects send messages asking/telling others to perform behaviors -- those other objects invoke the method corresponding to the message in order to execute the desired behavior

What’s class l. A class describes the common structure (attributes/state) and behaviour of its What’s class l. A class describes the common structure (attributes/state) and behaviour of its instances l For example, – 3. 14, 2. 71, and 5. 5 can be classified as Floats – the following shapes can be classified as Circles

Classes and Objects l An object is called an Classes and Objects l An object is called an "instance" of a class l The terms instance and object are interchangeable l Creating an object from a class is often called instantiation l For example, there are many person objects in this room -- each person is an instance of the person class

Classes and objects l How would we describe the state, behaviour, and identity for Classes and objects l How would we describe the state, behaviour, and identity for each of these objects l We have looked at objects and we have seen that objects can be "classified" into classes l As programmers, we work with both classes and objects from those classes l For example, we might write a stack class and create multiple stack objects for use in our program

l In a 2 D drawing package, circles have a radius, a line thickness, l In a 2 D drawing package, circles have a radius, a line thickness, a line colour, and a fill colour l Each individual circle (instance) drawn by the user has its own value for each attribute l The programmer writes a Circle class and the program instantiates a Circle object every time the user draws a Circle a snowman made from 9 Circle instances

l Simply, a class is a group of objects with common parts or behaviors l Simply, a class is a group of objects with common parts or behaviors (similar, not necessary the exact same). That common thing will be defined in class definition. l An object is an instance which meets all the description of its class.

Class Attributes An attribute is a named property of a class that describes the Class Attributes An attribute is a named property of a class that describes the range of values that instances of the property may hold. (Booch, 1999) l An attribute has a type that defines the type of its instances. l Only the object itself should be able to change the value of its attributes. l The values of the attributes define the state of the object l

Operation (Methods) l An operation is the implementation of a service that can be Operation (Methods) l An operation is the implementation of a service that can be requested from any object of the class to affect behavior (Booch, 1999) l An operation can be: – Question (does not change the value of the object) – Command (may change the value of the object)

Interface and Implementation l The class interface is its external (public) view l The Interface and Implementation l The class interface is its external (public) view l The class implementation is its internal (private) view l It is convenient to think of the interface as describing "what the objects of this class can do" and the implementation as "how the objects of this class do it" l The implementation of a class consists of all the "behind the scenes" operations defined in the interface of the class

Introduction to UML What’s UML l Goals of UML l Overview l – Class Introduction to UML What’s UML l Goals of UML l Overview l – Class diagram l l Introduction Class icon Class relationship Constraints – Other diagrams

UML: Unified Modeling Language l The Unified Modeling Language (UML) is an industry-standard language UML: Unified Modeling Language l The Unified Modeling Language (UML) is an industry-standard language for specifying, visualizing, constructing, and documenting the artifacts of software systems l The UML definition was led by Grady Booch, Ivar Jacobson, and Jim Rumbaugh (all now at Rational Software)

l. UML simplifies the process of software design, making a l. UML simplifies the process of software design, making a "blueprint" for construction In Essence: a tool used through the analysis and design phases of system development for expressing the constructs and relationships of complex systems Target Usage: for building object-oriented and component-based systems

Goals of UML 1. Provide users with a ready-to-use, expressive visual modeling language so Goals of UML 1. Provide users with a ready-to-use, expressive visual modeling language so they can develop and exchange meaningful models 2. Provide extensibility and specialization mechanisms to extend the core concepts. 3. Be independent of particular programming languages and development processes. 4. Provide a formal basis for understanding the modeling language. 5. Encourage the growth of the Object-Oriented tools market. 6. Support higher-level development concepts such as collaborations, frameworks, patterns and components. 7. Integrate best practices.

l. Many free learning materials on the web – e. g. www. rational. com/uml l. Many free learning materials on the web – e. g. www. rational. com/uml l. Standard of UML www. cs. wcupa. edu/~zjiang/01 -09 -67. pdf l. Some recommended texts on UML: – The Unified Modeling Language User Guide, [G. Booch, J. Rumbaugh, I. Jacobson, 2000] – UML Explained, [Kendall Scott, 2001] – Applying UML and Patterns 2 nd Ed. , [Craig Larman, 2002] – UML Distilled 2 nd Ed. , [Martin Fowler with K. Scott, 2000] – UML and C++, [R. Lee & W. Tepfenhart, 2001]

UML Overview l UML is a language for visualizing, specifying, constructing and documenting the UML Overview l UML is a language for visualizing, specifying, constructing and documenting the artifacts of a software system l The artifacts might include requirements, architecture, design, source code, project plans, tests, prototypes, releases

Review something Object action state 1 state 2 operations/methods attributes: {attribute 1, attribute 2} Review something Object action state 1 state 2 operations/methods attributes: {attribute 1, attribute 2} structure operation/method attribute 1 attribute 2 value

Light On Turn on/off Off operations/methods: Turn_on/off ( ) attributes: {True, False} structure Turn_on Light On Turn on/off Off operations/methods: Turn_on/off ( ) attributes: {True, False} structure Turn_on True False Turn_off value

Class Diagram l Introduction l Class Icon l Relationships l Constraints Class Diagram l Introduction l Class Icon l Relationships l Constraints

Introduction l The class diagram is fundamental to objectoriented programming l UML’s class diagrams Introduction l The class diagram is fundamental to objectoriented programming l UML’s class diagrams capture the attributes and operations of each class as well the relationships that exist between classes

Class Icon l l l Class Icon Hiding Details Visibility Notation Attribute Specification Operation Class Icon l l l Class Icon Hiding Details Visibility Notation Attribute Specification Operation Specification

Class Icon l The UML class icon is a rectangle with three compartments: – Class Icon l The UML class icon is a rectangle with three compartments: – class name – class attributes – class operations l Attributes are specified in the following form: – object: class name Circle radius: float center_x: int center_y: int area() display()

Hiding Detail l You can optionally leave out the attributes, operations, or both in Hiding Detail l You can optionally leave out the attributes, operations, or both in a class icon: Circle area() display() Circle radius: float center_x: int center_y: int Circle

UML Member Visibility Notation l. UML has three visibility prefixes for members: + for UML Member Visibility Notation l. UML has three visibility prefixes for members: + for public, # for protected, and – for private – e. g. Circle -radius: float #area() +display()

Full UML Attribute Specification l The full form of a UML attribute is as Full UML Attribute Specification l The full form of a UML attribute is as follows: [visibility] name [multiplicity] [: type] [= initial value] [{property}] – The property choices are changeable, add. Only, and frozen Student Info -id: string -has. Graduated: bool = false

l. The Full UML Operation Specification full form of a UML operation is as l. The Full UML Operation Specification full form of a UML operation is as follows: [visibility] name [(parameter-list)] [: return-type] [{property}] – The property choices are sequential, concurrent, guarded, and is. Query l. The full form of a UML parameter is: [direction] name : type [= default-value] – The direction choices are in, out, and inout

UML Class Relationships l. A class relationship is a connection between two (or more) UML Class Relationships l. A class relationship is a connection between two (or more) classes l The three most important class relationships are generalizations, associations, and aggregations l UML provides a graphical representation for each of the relationships using a different line type for each relationship

Class Relationships l Generalization l Association Class l Qualified Association l Ternary Association l Class Relationships l Generalization l Association Class l Qualified Association l Ternary Association l Aggregation

Generalization A generalization is a relationship between a general thing (superclass) and a more Generalization A generalization is a relationship between a general thing (superclass) and a more specific kind of that thing (subclass) l In the UML, generalization requires that objects of the subclass may be used anywhere an object of the superclass appears l Person Student

Association l. An association is a structural relationship that specifies that objects of one Association l. An association is a structural relationship that specifies that objects of one thing are connected to objects of another Faculty Student

Association l. Associations can be adorned with a name. Teaching Faculty Student Association l. Associations can be adorned with a name. Teaching Faculty Student

Association l. Associations Faculty can be adorned with the roles. teacher learner Student Association l. Associations Faculty can be adorned with the roles. teacher learner Student

Association l. Associations Faculty can be adorned with the multiplicity. 1 4. . * Association l. Associations Faculty can be adorned with the multiplicity. 1 4. . * Student

Association Class (relation attributes) l Each object of association class is one instance of Association Class (relation attributes) l Each object of association class is one instance of relationship (link) in an association. Faculty 1. . * 4. . * Course Student

Qualified Association l Qualified association relates two classes and a qualifier. l The qualifier Qualified Association l Qualified association relates two classes and a qualifier. l The qualifier is a special attribute that reduces the effective multiplicity of an association. Directory file name File

Ternary Association l A ternary association is a structural relationship that specifies that objects Ternary Association l A ternary association is a structural relationship that specifies that objects of one thing are connected to objects of other two’s. Project Language Developer

Aggregation l An aggregation is an association that represents whole/part relationship l The “whole” Aggregation l An aggregation is an association that represents whole/part relationship l The “whole” end of the association relationship is adorned with an open diamond shape (e. g. X is part of Y) l e. g. door: Door is part of car: Car X Y

Generalization, Aggregation, and Association l See if the following pairs of classes have generalization, Generalization, Aggregation, and Association l See if the following pairs of classes have generalization, aggregation or association l l l l l Faculty & student (as) Hospital & doctor (as) Door & Car (ag --<>) Member & Organization (ag --<>) People & student (ge <|-- ) Circle & point (ge --|>) Department & Faculty (as) Employee & Faculty (ge <|--) Item & Printer (ge <|--) Account & Checking account (ge <|-- )

Constraints l Constraints on Object l Constraints on Relations (Ordering) l General Constraints (Dependency) Constraints l Constraints on Object l Constraints on Relations (Ordering) l General Constraints (Dependency)

Constraints on Object l. The constraints restricts the values that objects can be. l. Constraints on Object l. The constraints restricts the values that objects can be. l. Example: No employee’s salary can exceed the salary of the employee’s boss. {salary <=boss. salary} Employee Salary Employer Salary

Constraints on Relations (Ordering) l{Order} indicates that the elements of the “many” end of Constraints on Relations (Ordering) l{Order} indicates that the elements of the “many” end of an association have an explicit order that must be preserved. {ordered} Paper 1 1. . * Author

General Constraints (Dependency) l A dependency is a using relationship that states that a General Constraints (Dependency) l A dependency is a using relationship that states that a change in specification of one thing may affect another thing that uses it (but not necessarily the reverse)

General Constraints (Dependency) l Dependencies are often used when one class uses another class General Constraints (Dependency) l Dependencies are often used when one class uses another class as an argument of the operation Aerodrome check. RVR( r: Runway ) l Runway Dependencies are also often used to express general constraints. member-of Person {subset} chair-of Committee

UML Design l UML class diagram is not object diagram. l UML class diagram UML Design l UML class diagram is not object diagram. l UML class diagram is not data flow chart. l How to describe the behaviors/actions of objects in UML class diagram? l What kind of relationship is generalization? l How to keep the consistency of all the constraints? l Common mistakes in Time Sequence Diagram l Samples

UML class diagram is not object diagram Other-Loan Customer Bank Loan • Should we UML class diagram is not object diagram Other-Loan Customer Bank Loan • Should we add relation between Bank and Other. Loans? • Can Bank change Otherloans? • Instance of Loan and Other. Loans?

UML class diagram is not object diagram Customer Bank Loan Bank-Loan Other-Loan UML class diagram is not object diagram Customer Bank Loan Bank-Loan Other-Loan

UML class diagram is not data flow chart bargain Customer Dealer sign buy Product UML class diagram is not data flow chart bargain Customer Dealer sign buy Product sell authorize report Manager

UML class diagram is not data flow chart • Customer, Dealer, and Manager • UML class diagram is not data flow chart • Customer, Dealer, and Manager • Bargain Product Customer Salesman Dealer Manager

How to describe the behaviors/actions of objects in UML class diagram Person s. SN How to describe the behaviors/actions of objects in UML class diagram Person s. SN address ? email Customer Faculty Casher Student

How to describe the behaviors/actions of objects in UML class diagram Use case (stories): How to describe the behaviors/actions of objects in UML class diagram Use case (stories): Each use case defines an interaction between an actor and the system Use case

Scenarios of a use case Check Order ? Reception Waiting Failure Authorized Success Failure Scenarios of a use case Check Order ? Reception Waiting Failure Authorized Success Failure

How to describe the behaviors/actions of objects in UML class diagram – In design, How to describe the behaviors/actions of objects in UML class diagram – In design, see www. cs. wcupa. edu/~zjiang/intro_uc. ppt. – In programming, see time sequence diagram, see www. cs. wcupa. edu/~zjiang/intro_TS. ppt.

What kind of relationship is generalization Position Point x: int y: int People Student What kind of relationship is generalization Position Point x: int y: int People Student Circle radius: double Point Position Rectangle *Tip 4: Class A is a super class of class B if and only if any object of class B can also play the role as an object of class A.

How to keep the consistency of all the constraints Ticket-buyer 1 1 Traveler 1 How to keep the consistency of all the constraints Ticket-buyer 1 1 Traveler 1 0. . 6 Dependent 1. . 6 1

How to keep the consistency of all the constraints Ticket-buyer 0. . 1 1 How to keep the consistency of all the constraints Ticket-buyer 0. . 1 1 Traveler 1 0. . 6 Dependent 0. . 1

How to keep the consistency of all the constraints Traveler 0. . 1 Ticket-buyer How to keep the consistency of all the constraints Traveler 0. . 1 Ticket-buyer 1 0. . 6 Dependent