بسم الله الرحمن الرحيم ” اللهم أنت ربي لا إله إلا أنت خلقتني و أنا عبدك وأنا على عهدك ووعدك ما استطعت ، أعوذ بك من شر ما صنعت ، أبوء لك بنعمتك على و أبوء لك بذنبي فاغفر لي فإنه لا يغفر الذنوب إلا أنت ”.
Class Diagrams
What is a Class Diagrams? Class diagrams are the backbone of almost every object oriented method, including UML. They describe the static structure of a system. A class diagram describes the types of objects in the system and the various kinds of static relationships that exist among them.
Class Diagrams A class diagram shows: Classes Attributes Methods Interfaces Collaborations Relationships: Dependency, Generalization, Association A class diagram is a STATIC view of system
Classes A class is the description of a set of objects having similar attributes, operations (behavior), and relationships.
Basic Class Diagrams Window size: Size visibility: boolean display() hide() Class Name Attributes Operations
Basic Class Diagrams +public #protected -private / derived $ static Visibility Attribute Name [Multiplicity]:Type = Initial Value Visibility Method Name (Parameter List) : Return-List Class Scope Variable
Basic Class Diagram (Example) Person + name : String - ssn : String # birthday : Date / age : int +getName : String -calculateAge : int
Associations A semantic relationship between two or more classes that specifies connections among their instances. A structural relationship specifying that objects of one class are connected to objects of a second (possibly the same) class. Example: “ An Employee works for a Company ”.
Associations (cont.) An 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. Borrower Book 31 currBorrbk[]
Association (Java implementation) public class Borrower { Book bk[]; int numBooks; … public Borrower() { numBooks = 0; bk = new Book[3]; } // methods that update bk public void borrowBook( Book b ) { bk[numBooks] = b; numBooks++; b.setBorrower( this ); } public class Book { Borrower currBorr; public void setBorrower( Borrower bw ) { currBorr = bw; }
Aggregation A 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. Whole Part Car Wheel 4 wheels
Aggregation (Java implementation) public class Car { private Wheel wheels[];... // wheel objects are created externally and // passed to the constructor public Car( Wheel w1, Wheel w2, … ) { // we can check w1, w2, etc. for null // to make sure wheels exist wheels = new Wheel[4]; wheels[0] = w1; wheels[1] = w2; … }
Composition A strong form of aggregation: The whole is the sole owner of its part: The part object may belong to only one whole Multiplicity on the whole side must be one The life time of the part is dependent upon the whole The composite must manage the creation and destruction of its parts LinePoint 3..* 2 Polygon
Composition (Java implementations) public class Car { private Wheel wheels[];... public Car() { wheels = new Wheel[4]; // Wheels are created in Car … wheels[0] = new Wheel(); wheels[1] = new Wheel(); … }... }
Generalization Indicates that objects of the specialized class (subclass) are substitutable for objects of the generalized class (super-class) “ is kind of ” relationship Shape Circle Super Class Sub Class An abstract class Generalization relationship
Generalization A sub-class inherits from its super- class: Attributes. Operations. Relationships. A sub-class may: Add attributes and operations. Add relationships. Refine (override) inherited operations.
Generalization (Java implementation) public abstract class Shape { public abstract void draw();... } public class Circle extends Shape { public void draw() {... }... }
Dependency A dependency indicates a semantic relation between two or more classes in which a change in one may force changes in the other although there is no explicit association between them A stereotype may be used to denote the type of the dependency Parser getTransaction() uses Bank processTransactions ()
Dependency (Java implementation) public class Bank { … public void processTransactions() { // Parser p is a local variable … Parser p = new Parser( … ); … p.getTransaction(); … } … }
Realization A 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 > List LinkedList List
Realization (Java implementation) public interface List { boolean add(Object o);... } public class LinkedList implements List { public boolean add(Object o) {... }... }
Basic Class Diagrams Superclass Subclass Inheritance (Generalization) (is-a, kind-of) Class with parts Assembly Class Aggregation (Part-Of) Association (relationship) Class with parts Assembly Class Dependency Class A Class B name Interface Concrete Class Realization
Multiplicity The number of instances of the class, next to which the multiplicity expression appears, that are referenced by a single instance of the class that is at the other end of the association path. Indicates whether or not an association is mandatory. Provides a lower and upper bound on the number of instances.
Multiplicity Indicators Exactly one1 Zero or more (unlimited)* (0..*) One or more1..* Zero or one (optional association) 0..1 Specified range2..4
Computer: Computer PrintServer: PrintServer Printer: Printer print(file) User: User print (filename) Queue: Queue [printer free] print(file) [printer busy] store(file)
Computer print(filename) PrintServer status(): integer print(file) Printer print(file) Queue store(file) uses Managed by