Lecture
Class
Class (class) in the UML language is used to designate a set of objects that have the same structure, behavior and relationships with objects from other classes. Graphically, the class is depicted as a rectangle, which can additionally be divided by horizontal lines into sections or sections (Fig. 5.1). In these sections you can specify the class name, attributes (variables) and operations (methods).
Fig. 5.1. Graphic image of the class in the class diagram
Mandatory elements of class designation is its name. At the initial stages of diagram development, individual classes can be denoted by a simple rectangle indicating only the name of the corresponding class (Fig. 5.1, a). As the individual components of the class description chart are developed, attributes are added (Fig. 5.1, b) and operations (Fig. 5.1, c).
It is assumed that the final version of the diagram contains the most complete description of classes, which consist of three sections or sections. Sometimes an additional fourth section is used in the notation of classes, in which semantic information of a reference nature is given or exceptions are explicitly indicated.
Even if the attributes and operations section is empty, it is highlighted with a horizontal line in the class notation to immediately distinguish the class from other UML elements. Examples of the graphic representation of classes on the class diagram are shown in Fig. 5.2. In the first case, for the class "Rectangle" (Fig. 5.2, a), only its attributes are indicated - points on the coordinate plane that define its location. For the class "Window" (Fig. 5.2, b) only its operations are indicated, the attributes section is left blank. For the class "Account" (Fig. 5.2, c), the fourth section is additionally depicted, in which the exception is indicated - refusal to process an overdue credit card.
Fig.5.2. Graphic examples of classes in the diagram
Class name
The class name must be unique within the package, which is described by a certain set of class diagrams (possibly a single diagram). It is indicated in the first upper section of the rectangle. In addition to the general rule for naming UML elements, the class name is written in the center of the name section in bold and must begin with a capital letter. It is recommended to use nouns that are written for practical reasons without gaps as class names. It must be remembered that it is the names of the classes that form the vocabulary of the subject area when OOAP.
In the first section of the class notation, there may be references to standard templates or abstract classes from which this class is formed and, accordingly, from which it inherits properties and methods. This section can provide information about the developer of this class and the status of the development status, as well as other general properties of this class that are related to other diagram classes or standard UML elements.
Examples of class names can be such nouns as “Employee”, “Company”, “Manager”, “Customer”, “Seller”, “Manager”, “Office” and many others that are directly related to the modeled domain and functionality. projected system.
A class may not have instances or objects. In this case, it is called an abstract class, and italic is used to indicate its name. In UML, there is a general agreement that any text relating to an abstract element is written in italics. This circumstance is a semantic aspect of the description of the corresponding elements of the UML language.
public class Man{ protected String name; protected String surname; public void setName(String newName){ name = newName; } public String getName(){ return name; } public void setSurname(String newSurname){ name = newSurname; } public String getSurname(){ return surname; } } // наследуем класс Man public class Employee extends Man{ private String position; // создаем и конструктор public Employee(String n, String s, String p){ name = n; surname = s; position = p; } public void setPosition(String newProfession){ position = newProfession; } public String getPosition(){ return position; } }
public class Employee extends Man{ private String position; private IdCard iCard; public Employee(String n, String s, String p){ name = n; surname = s; position = p; } public void setPosition(String newPosition){ position = newPosition; } public String getPosition(){ return position; } public void setIdCard(IdCard c){ iCard = c; } public IdCard getIdCard(){ return iCard; } } public class IdCard{ private Date dateExpire; private int number; public IdCard(int n){ number = n; } public void setNumber(int newNumber){ number = newNumber; } public int getNumber(){ return number; } public void setDateExpire(Date newDateExpire){ dateExpire = newDateExpire; } public Date getDateExpire(){ return dateExpire; } }
IdCard card = new IdCard(123); card.setDateExpire(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-31")); sysEngineer.setIdCard(card); System.out.println(sysEngineer.getName() +" работает в должности "+ sysEngineer.getPosition()); System.out.println("Удостовирение действует до " + new SimpleDateFormat("yyyy-MM-dd").format(sysEngineer.getIdCard().getDateExpire()) );
public class Room{ private int number; public Room(int n){ number = n; } public void setNumber(int newNumber){ number = newNumber; } public int getNumber(){ return number; } }
... private Set room = new HashSet(); ... public void setRoom(Room newRoom){ room.add(newRoom); } public Set getRoom(){ return room; } public void deleteRoom(Room r){ room.remove(r); } ...
public static void main(String[] args){ Employee sysEngineer = new Employee("John", "Connor", "Manager"); IdCard card = new IdCard(123); card.setDateExpire(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-31")); sysEngineer.setIdCard(card); Room room101 = new Room(101); Room room321 = new Room(321); sysEngineer.setRoom(room101); sysEngineer.setRoom(room321); System.out.println(sysEngineer.getName() +" работает в должности "+ sysEngineer.getPosition()); System.out.println("Удостовирение действует до " + sysEngineer.getIdCard().getDateExpire()); System.out.println("Может находиться в помещеньях:"); Iterator iter = sysEngineer.getRoom().iterator(); while(iter.hasNext()){ System.out.println( ((Room) iter.next()).getNumber()); } }
public class Department{ private String name; private Set employees = new HashSet(); public Department(String n){ name = n; } public void setName(String newName){ name = newName; } public String getName(){ return name; } public void addEmployee(Employee newEmployee){ employees.add(newEmployee); // связываем сотрудника с этим отделом newEmployee.setDepartment(this); } public Set getEmployees(){ return employees; } public void removeEmployee(Employee e){ employees.remove(e); } }
... private Department department; ... public void setDepartment(Department d){ department = d; } public Department getDepartment(){ return department; }
Department programmersDepartment = new Department("Программисты"); programmersDepartment.addEmployee(sysEngineer); System.out.println("Относится к отделу "+sysEngineer.getDepartment().getName());
private class PastPosition{ private String name; private Department department; public PastPosition(String position, Department dep){ name = position; department = dep; } public void setName(String newName){ name = newName; } public String getName(){ return name; } public void setDepartment(Department d){ department = d; } public Department getDepartment(){ return department; } }
... private Set pastPosition = new HashSet(); ... public void setPastPosition(PastPosition p){ pastPosition.add(p); } public Set getPastPosition(){ return pastPosition; } public void deletePastPosition(PastPosition p){ pastPosition.remove(p); } ...
// изменяем должность sysEngineer.setPosition("Сторож"); // смотрим ранее занимаемые должности: System.out.println("В прошлом работал как:"); Iterator iter = sysEngineer.getPastPosition().iterator(); while(iter.hasNext()){ System.out.println( ((PastPosition) iter.next()).getName()); }
public class Menu{ private static int i=0; public static void showEmployees(Employee[] employees){ System.out.println("Список сотрудников:"); for (i=0; i<employees.length; i++){ if(employees[i] instanceof Employee){ System.out.println(employees[i].getName() +" - " + employees[i].getPosition()); } } } }
// добавим еще одного сотрудника Employee director = new Employee("Федор", "Дубов", "Директор"); Menu menu = new Menu(); Employee employees[] = new Employee[10]; employees[0]= sysEngineer; employees[1] = director; Menu.showEmployees(employees);
public interface Unit{ int getPersonCount(); }
public class Department implements Unit{ ... public int getPersonCount(){ return getEmployees().size(); }
System.out.println("В отделе "+sysEngineer.getDepartment().getName()+" работает " +sysEngineer.getDepartment().getPersonCount()+" человек.");
The UML modeling language has a set of relationships for building a class model, but even a developed OOP language like Java has only two explicit constructs to reflect relationships: extends (extension) and interface / implements (implementation).
As a result of the simulation we obtained the following diagram:
Fig. 8 - class diagram
Mini reference book on designations that are used in UML class diagrams for relationships between different classes.
Denoted by arrows.
The most common inheritance: class A extends B {}
Interface implementation: class A implements I {}
Family relationship between class objects. "Student" - "Teacher", "Buyer" - "Seller", etc. It may be indicated without an arrow at all.
Aggregation and Composition - association subtypes.
Association subtype. For example, one class contains (aggregates) objects of another class.
It looks like an aggregation only a stronger connection. Therefore, a filled diamond. For example: if a composer is destroyed, then his class objects to which he refers also cease to exist.
Classes "in some way" depend on each other. For example, if one class has methods, constructors, or fields, and therefore another class has to be rewritten, then they are dependent. One of the weakest links. For example, objects of one class are passed as a parameter to methods of another class, etc.
Comments
To leave a comment
Object oriented programming
Terms: Object oriented programming