-
Class DiagramModeling 2019. 8. 25. 11:53
1. Overview
A class diagram in the Unified Modeling Language(UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations(or methods), and the relationships among objects.
2. Class Notation
public class Temperature { private int current; private int desired = 70; public int getCurrent() { return current; } public int getDesired() { return desired; } public void setCurrent(int current) { this.current = current; } public void setDesired(int desired) { this.desired = desired; } }
2.1 Notation Components
2.1.1 Class Name
The name of the class appears in the first partition.
2.1.2 Class Attributes
Attributes are shown in the second partition.
The attribute type is shown after the colon.
Attributes map onto member variables (data members) in code.
2.1.3 Class Operations (Methods)
Operations are shown in the third partition. They are services the class provides.
The return type of a method is shown after the colon at the end of the method signature.
The return type of method parameters is shown after the colon following the parameter name.
Operations map onto class methods in code
Above example
- MyClass has 3 attributes and 3 operations
- Parameter p3 of op2 is of type int
- op2 returns a float
- op3 returns a pointer (denoted by a *) to Class6
2.2 Visibility of Class attributes and Operations
- denotes public attributes or operations
- - denotes private attributes or operations
- # denotes protected attributes or operations
- ~ denotes package attributes or operations
Access Right Public(+) private(-) protected(#) Package(~) Members of the same class yes yes yes yes Members of derived classes yes no yes yes Members of any other class yes no no in the same package Above examples
- attribute1 and op1 of MyClassName are public
- attribute3 and op3 are protected.
- attribute2 and op2 are private.
2.3 Perspectives of Class Diagram
- Conceptual: represents the concepts in the domain
- Specification: focus is on the interfaces of Abstract Data Type (ADTs) in the software
- Implementation: describes how classes will implement their interfaces
2.4 Relationship Type
2.4.1 Relationship Types
2.4.2 Inheritance
“is-a” relationship
2.4.3 Association (has-a)
Association only means that the objects “know” each other. For example, a mother and her child.
class Child {} class Mother { List<Child> children; }
class Child { Mother mother; } class Mother { List<Child> children; }
how can we tell if a reference means aggregation or association?
Well, we can't. The difference is only logical: whether one of the objects is part of the other or not.
Also, we have to maintain the references manually on both ends as we did with aggregation:
2.4.4 Aggregation (has-a + whole-part)
Aggregation is also a “has-a” relationship. What distinguishes it from composition, that it doesn't involve owning. As a result, the lifecycles of the objects aren't tied: every one of them can exist independently of each other.
For example, a car and its wheels. We can take off the wheels, and they'll still exist. We can mount other (preexisting) wheels, or install these to another car and everything will work just fine.
class Wheel {} class Car { List<Wheel> wheels; }
class Wheel { Car car; } class Car { List<Wheel> wheels; }
2.4.5 Composition (has-a + whole-part + ownership)
Composition is a “belongs-to” type of relationship. It means that one of the objects is a logically larger structure, which contains the other object. In other words, it's part or member of the other object. Alternatively, we often call it a “has-a” relationship
Composition is a strong kind of “has-a” relationship because the containing object owns it. Therefore, the objects' lifecycles are tied. It means that if we destroy the owner object, its members also will be destroyed with it. For example, the room is destroyed with the building in our previous example.
class Building { class Room {} }
class Building { List<Room> rooms; class Room {} }
class Building { Room createAnonymousRoom() { return new Room() { @Override void doInRoom() {} }; } Room createInlineRoom() { class InlineRoom implements Room { @Override void doInRoom() {} } return new InlineRoom(); } Room createLambdaRoom() { return () -> {}; } interface Room { void doInRoom(); } }
2.4.6 Dependency
A dependency is a semantic connection between dependent and independent model elements.[6] It exists between two elements if changes to the definition of one element (the server or target) may cause changes to the other (the client or source). This association is uni-directional. A dependency is displayed as a dashed line with an open arrow that points from the client to the supplier.
public class EnrollmentService { public void enroll(Student s, Course c){} }
The EnrollmentService class may have a method called enroll that expects a Student object and a Course object as input
4. Example
4.1 Multiplicity
4.2 Aggregation
4.3 Inheritance
4.4 Diagram Tool
4.5 Complex example
class University { List<Department> department; } class Department { List<Professor> professors; } class Professor { List<Department> department; List<Professor> friends; }
4.6 Code Mapping
public class User { public static void main(String[] args) { System.out.println("Enter your choice: 1)Receptionist 2)Patient 3)Doctor "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); do { switch (choice) { case 1: Receptionist R = new Receptionist(); R.Choice(); break; case 2: Patient P = new Patient(); break; case 3: Doctor D = new Doctor(); break; } } while (choice != 4); } }
public class Receptionist extends User { private int r_id; private String r_name; public void Choice(){ System.out.println("Enter your choice:1)GiveAppoint 2)GenerateBill"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); switch(choice){ case 1: GiveAppoint(); break; case 2: GenerateBill(); break; case 3: main(null); } } public void GenerateBill(){ Billing B = new Billing(); int b_no =B.getB_no(); String p_name =B.getP_name(); int p_id = B.getP_id(); int b_amt = B.getB_amt(); System.out.println("BillNO: "+b_no +"\n PatientID: "+p_id+"\nPatientName: "+p_name+"\nBillAmount: "+b_amt); } public void GiveAppoint(){ Patient P = new Patient(); String p_name =P.getP_name(); int p_id = P.getP_id(); Doctor D = new Doctor(); int d_id = D.getD_id(); String d_name = D.getD_name(); System.out.println("\n PatientID: "+p_id+"\nPatientName: "+p_name+"\nAppointment with Doctor \nDoctorID: "+d_id+ "\nDoctorName"+d_name); } }
public class Patient extends User{ private int p_id=101; private String p_name="Anushka"; public void Choice(){ System.out.println("Enter your choice:1)PayBill"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); switch(choice){ case 1: PayBill(); break; case 2: main(null); } } public int getP_id() { return p_id; } public void setP_id(int p_id) { this.p_id = p_id; } public String getP_name() { return p_name; } public void setP_name(String p_name) { this.p_name = p_name; } public void PayBill(){ System.out.println("Thanks For Paying Bill"); } }
public class Doctor extends User{ private int d_id=301; private String d_name="Raju Rastogi"; public int getD_id() { return d_id; } public void setD_id(int d_id) { this.d_id = d_id; } public String getD_name() { return d_name; } public void setD_name(String d_name) { this.d_name = d_name; } public void Choice(){ System.out.println("Enter your choice:1)CheckPatient"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); switch(choice){ case 1: CheckPatient(); break; case 2: main(null); } } public void CheckPatient(){ Patient P = new Patient(); String p_name =P.getP_name(); int p_id = P.getP_id(); System.out.println("Doctor: "+d_name+" has check patient "+p_name); } }
public class Billing{ private int b_no=201; private int b_amt=1000; Patient P = new Patient(); String p_name =P.getP_name(); int p_id = P.getP_id(); public String getP_name() { return p_name; } public int getB_no() { return b_no; } public int getB_amt() { return b_amt; } public Patient getP() { return P; } public int getP_id() { return p_id; } }
5. References
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/
http://www.cs.sjsu.edu/~pearce/modules/lectures/uml/class/association
https://en.wikipedia.org/wiki/Class_diagram#Dependency
https://medium.com/@smagid_allThings/uml-class-diagrams-tutorial-step-by-step-520fd83b300b
https://www.geeksforgeeks.org/unified-modeling-language-uml-class-diagrams/
https://www.guru99.com/uml-class-diagram.html
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/what-is-class-diagram/
https://www.lucidchart.com/pages/uml-class-diagram
https://en.wikipedia.org/wiki/Class_diagram
http://www.professionalcipher.com/2017/10/uml-class-diagram-and-java-hospital-mgnt-system.html
https://www.youtube.com/watch?v=rtqQeYKHHNg
https://ase.in.tum.de/lehrstuhl_1/files/teaching/ss07/SE/SE2007_Lecture16.pdf
https://www.baeldung.com/java-composition-aggregation-association
'Modeling' 카테고리의 다른 글
Entity Relationship Diagram(ERD) (0) 2019.08.25