unit1 class diagram

83
UML Class Diagram

Upload: candi-middleton

Post on 12-Sep-2015

63 views

Category:

Documents


1 download

DESCRIPTION

Unit1 Class Diagram

TRANSCRIPT

  • UML Class Diagram

  • UMLUML is a language used for object-oriented analysis and design. UML includes a set of graphical notation techniques to create visual models of software systems. Three developers of UML are Grady Booch, Ivar Jacobson and James Rumbaugh. UML is a language for visualizing, specifying, constructing, documenting. UML is not a development method. It is designed to be compatible with the object-oriented software development methods.

  • UMLUML can be used to construct nine different types of diagrams to capture five different views of a system. Just as a building can be modeled from several views (or perspectives) such as ventilation perspective, electrical perspective, lighting perspective, heating perspective, etc.; the different UML diagrams provide different perspectives of the software system to be developed and facilitate a comprehensive understanding of the system. Such models can be refined to get the actual implementation of the system.

  • UML

  • Views in UMLFunctional View: This view describes functional requirements of the system. Use case diagrams give static functional view for functions and static relationships. Activity diagrams give dynamic functional view.

    Static Structural View: The class and object diagrams give the structural view of system.

  • Views in UMLBehavioral (dynamic structural) view: Interaction diagrams- collaboration diagrams and sequence diagrams describe sequences of interactions between objects. State transition diagrams show state-based behavior of objects.

    Architectural View: This view describes logical and physical structure. Component diagrams and deployment diagrams are used in this view.

  • Views in UML

  • What is a Class Diagram?A Class Diagram is a diagram describing the structure of a system shows the system'sclassesAttributesoperations (or methods),Relationships among the classes.

  • Essential Elements of a UML Class DiagramClassAttributesOperationsRelationshipsAssociationsGeneralizationRealizationDependencyConstraint Rules and Notes

  • ClassDescribes a set of objects having similar:Attributes (status)Operations (behavior)Relationships with other classesAttributes and operations mayhave their visibility marked:"+" for public"#" for protected"" for private"~" for packageClass NameAttributesOperations

  • AssociationsAn association between two classes indicates that objects at one end of an association recognize objects at the other end and may send messages to them.

    Example: An Employee works for a Company

  • Associations (cont.)StaffMemberStudent1..**instructsinstructorAssociation nameRole nameMultiplicityNavigable (uni-directional) associationCoursespre - requisites0..3Reflexive associationRole*

  • Associations (cont.)To clarify its meaning, an association may be named.The name is represented as a label placed midway along the association line.Usually a verb or a verb phrase.A role is an end of an association where it connects to a class.May be named to indicate the role played by the class attached to the end of the association path.Usually a noun or noun phraseMandatory for reflexive associations

  • Associations (cont.)Multiplicitythe number of objects that participate in the association.Indicates whether or not an association is mandatory.Multiplicity Indicators

    Exactly one1Zero or more (unlimited)* (0..*)One or more1..*Zero or one (optional association)0..1Specified range2..4Multiple, disjoint ranges2, 4..6, 8

  • AggregationA special form of association that models a whole-part relationship between an aggregate (the whole) and its parts.Models a is a part-part of relationship.WholePart

  • Aggregation (cont.)Aggregation tests:Is the phrase part of used to describe the relationship?A door is part of a carAre some operations on the whole automatically applied to its parts?Move the car, move the door.Are some attribute values propagated from the whole to all or some of its parts?The car is blue, therefore the door is blue.Is there an intrinsic asymmetry to the relationship where one class is subordinate to the other?A door is part of a car. A car is not part of a door.

  • CompositionA strong form of aggregationThe whole is the sole owner of its part.The part object may belong to only one wholeMultiplicity on the whole side must be zero or one.The life time of the part is dependent upon the whole. The composite must manage the creation and destruction of its parts.

  • GeneralizationIndicates that objects of the specialized class (subclass) are substitutable for objects of the generalized class (super-class).is kind of relationship.{abstract} is a tagged value that indicates that the class is abstract. The name of an abstract class should be italicized

  • GeneralizationA sub-class inherits from its super-classAttributesOperationsRelationshipsA sub-class mayAdd attributes and operationsAdd relationshipsRefine (override) inherited operationsA generalization relationship may not be used to model interface implementation.

  • RealizationA realization relationship indicates that one class implements a behavior specified by another class (an interface or protocol).An interface can be realized by many classes.A class may realize many interfaces.LinkedList ListArrayList

  • DependencyDependencyis a weaker form of relationship which indicates that one class depends on another because it uses it at some point in time. One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class. This is different from an association, where an attribute of the dependent class is an instance of the independent class.IteratorVector

  • Constraint Rules and NotesConstraints and notes annotate among other things associations, attributes, operations and classes.Constraints are semantic restrictions noted as Boolean expressions.UML offers many pre-defined constraints.ConstraintNote

  • STANDARD CLASS STEREOTYPES Standard class stereotypes include: focus auxiliary type utility

  • focus Focus is class that defines the core logic or control flow for one or more supporting classes. The supporting classes may be defined either explicitly using auxiliary classes or implicitly by dependency relationships. Focus classes are typically used for specifying the core business logic or control flow of components during design phase.

  • auxiliary Auxiliary is class that supports another more central or fundamental class, typically by implementing secondary logic or control flow. The class that the auxiliary supports may be defined either explicitly using a focus class or implicitly by a dependency relationship. Auxiliary classes are typically used for specifying the secondary business logic or control flow of components during design phase.

  • type Type is class that specifies a domain of objects together with the operations applicable to the objects, without defining the physical implementation of those objects. Type may have attributes and associations. Behavioral specifications for type operations may be expressed using, for example, activity diagrams. An object may have at most one implementation class, however it may conform to multiple different types.

  • utility Utility is class that has only class scoped static attributes and operations.

    As such, utility class usually has no instances.

    Math is utility class - having static attributes and operations (underlined)

  • EXAMPLE

  • Creating a Class DiagramIdentify ClassesThese are the abstract or physical things in our system which we wish to describe.

    Find all the nouns and noun phrases in the domain descriptions you have obtained through your analysis.

    Consider these class candidates.

  • Creating a Class DiagramFind AssociationsNow find the verbs that join the nouns. e.g., The professor (noun) teaches (verb) students (noun).

    The verb in this case, defines an association between the two nouns.

    Identify the type of association. Use a matrix to define the associations between classes.

  • Creating a Class DiagramDraw Rough Class DiagramPut classes in rectangles and draw the associations connecting the classes.

    Fill in MultiplicityDetermine the number of occurrences of one class for a single occurrence of the associated class.

  • Creating a Class DiagramIdentify AttributesName the information details (fields) which are relevant to the application domain for each class.

    Identify Behaviors -Specify the operations that are required for each class. (assume getter and setter methods for each attribute.)

  • Creating a Class DiagramReview your diagram and fine tune it.Look for inconsistencies and errors.

    Fix them.

    Make sure you have captured everything required from the domain you are studying - that your diagram is complete.

  • The University has several departments. Each department is managed by a chair, and at least one professor. Professors must be assigned to one, but possibly more departments. At least one professor teaches each course, but a professor may be on sabbatical and not teach any course. Each course may be taught more than once by different professors. We know of the department name, the professor name, the professor employee id, the course names, the course schedule, the term/year that the course is taught, the departments the professor is assigned to, the department that offers the course.

  • Identify ClassesThese are the abstract or physical things in our system which we wish to describe.Find all the nouns and noun phrases in the domain descriptions you have obtained through your analysis. Consider these class candidates.The class candidates are departments, chair, professor, course, and course section.Since there is only one instance of the University, we exclude it from our consideration.

  • Find AssociationsNow find the verbs that join the nouns. e.g., The professor (noun) teaches (verb) students (noun). The verb in this case, defines an association between the two nouns.Identify the type of association. Use a matrix to define the associations between classes.

  • Draw Rough Class DiagramPut classes in rectangles and draw the associations connecting the classes.

  • Fill in MultiplicityDetermine the number of occurrences of one class for a single occurrence of the associated class.

  • Identify AttributesName the information details (fields) which are relevant to the application domain for each class. Add these attributes.We have the department name, the professor name, the professor employee id, the course names, the course schedule, the term/year that the course is taught, the departments the professor is assigned to, the department that offers the course.

  • Identify BehaviorsSpecify the operations that are required for each class. (assume getter and setter methods for each attribute.) In this example - we are not given any behaviors, so we will have to make them up. What are some behaviors of these classes?

  • *Class Diagram - ExampleDraw a class diagram for a information modeling system for a school.School has one or more Departments. Department offers one or more Subjects. A particular subject will be offered by only one department.Department has instructors and instructors can work for one or more departments.Student can enrol in upto 5 subjects in a School.Instructors can teach upto 3 subjects.The same subject can be taught by different instructors. Students can be enrolled in more than one school.

  • *Class Diagram - ExampleSchool has one or more Departments. Department offers one or more Subjects. A particular subject will be offered by only one department.

  • *Class Diagram - ExampleDepartment has Instructors and instructors can work for one or more departments.

    Student can enrol in upto 5 Subjects.

  • *Class Diagram - ExampleInstructors can teach up to 3 subjects.The same subject can be taught by different instructors.

  • *Class Diagram - ExampleStudents can be enrolled in more than one school.

  • *Class Diagram Example1**member*1..5 attends1..31..*teaches1..*111..*has1..*1..*assignedToofferes

  • Example: Problem Report ToolA CASE tool for storing and tracking problem reportsEach report contains a problem description and a statusEach problem can be assigned to someoneProblem reports are made on one of the artifacts of a projectEmployees are assigned to a projectA manager may add new artifacts and assign problem reports to team members

  • Class Diagram for Prob. Rep. Tool

  • Lab Assignments

  • Class Diagram

  • Traffic Violation Report SystemTraffic police is responsible to maintain and control the traffic and need to submit whole day report.

    TP can raise a complaint against Person who is violating rules and can issue a violation report.

    Identify the classes

  • Traffic Violation Report System Example

  • The class diagram below models a customer order from a retail catalog. The central class is the Order. Associated with it are the Customer making the purchase and the Payment. A Payment is one of three kinds: Cash, Check, or Credit. The order contains OrderDetails (line items), each with its associated Item.

  • UML class notation is a rectangle divided into three parts: class name, attributes, and operations. Names of abstract classes, such as Payment, are in italics. Relationships between classes are the connecting links.

  • Association a relationship between instances of the two classes. There is an association between two classes if an instance of one class must know about the other in order to perform its work. In a diagram, an association is a link connecting two classes.

  • Aggregation an association in which one class belongs to a collection. An aggregation has a diamond end pointing to the part containing the whole. In our diagram, Order has a collection of OrderDetails.

  • Generalization an inheritance link indicating one class is a superclass of the other. A generalization has a triangle pointing to the superclass. Payment is a superclass of Cash, Check, and Credit.

  • An association has two ends. An end may have a role name to clarify the nature of the association. For example, an OrderDetail is a line item of each Order.

  • A navigability arrow on an association shows which direction the association can be traversed or queried.

    An OrderDetail can be queried about its Item, but not the other way around.

  • The arrow also lets you know who "owns" the association's implementation; in this case, OrderDetail has an Item.

    Associations with no navigability arrows are bidirectional.

  • The multiplicity of an association:In our example, there can be only one Customer for each Order, but a Customer can have any number of Orders.

  • Order Processing SystemCustomer wants some item so he will place an order. A quotation of ordered items should be generated by sales staff and same should be given to customer. After placing order successfully invoice copy should be given to the customer

  • Class Diagram Using Java

  • Car.java

  • Representing Static variable or static operation

  • Unidirectional Association

  • Bidirectional AssociationIn the bidirectional association each of the class in this relationship refers to each other by calling each others method. In the above Java example it is depicted as instance variable of Car class in called inside the Customer class and vice versa.In the above example the car and owner refers to the roles and is depicted by the name of instance variable in the code.

  • Multiplicity in associationThe above diagram explains a unidirectional association with a one to may relationship. Both use of ArrayList and Array is for illustration purposes only.

  • Multiplicity in association

  • Multiplicity in association

  • Generalization

  • RealizationThis is related to the relationship between the class and the interface. The realization is equivalent to the implements keyword in Java.

    This is very straight forward implementation so hopefully there will ne no code provided.

    Unlike Generalization in this case the arrow is dashed.

  • Dependencyshows that a class is dependent on another class for its existence or implementation.

    Dependency relationship is shown as a dotted line with an arrow from source class to the dependent class.

  • DependencyIn Java we can consider the dependency relationship if the source class has a reference to the dependent class directly or source class has methods through which the dependent objects are passed as a parameter or refers to the static operations of the dependent class or source class has a local variable referring to the dependent class etc.

  • The above diagram satisfies dependency relationship as the source class Order passes the PaymentSystem reference through the processPayment().

  • AggregationThis shows has a relationship. It is a form of association relationship. This relationship highlights that a whole is made of its parts. So if a whole is destroyed the part still remains.In UML this is represented through a hollow diamond with the diamond symbol pointing towards the whole.In case of Java the aggregation follows the same structure as association. It is represented through the instance variables of a class.

  • In this case a student is a part of the School. However during design it is preferred to use association instead of aggregation as it is not a recommended option.Aggregation

  • CompositionThis is again a whole or part relationship where if the whole is destroyed then the part cannot exist independently.

    Another important point about Composition is that the part at any point in time can have only one owner.

    E.g. A person can be an employee of one company at any point in time due to contractual obligations. That person cannot hold dual work authorization. If the Company goes bankrupt the employee of this company does not exist and will be fired.

  • CompositionThe composition is represented as a filled diamond with data flowing in single direction from the whole to the part.

    The composition in Java is represented in the same form as aggregation with help of instance variables.

  • *