Explain classes and objects with an organization architecture diagram

@

Catalog

background

In the early stages of object-oriented development, understanding of classes and objects is often the first problem that programmers encounter. The purpose of this paper is to translate a specific example into JAVA code, and to make the basic concepts of classes and objects in OOP clear programmatically.

Organization Chart

stay Two Graphics a Programmer Must Master in His Life In this article, we refer to the organization chart, such as the following chart: the chart is divided into four departments hierarchically (see the General Manager's office as the highest department), each with a supervisor; each department is divided into two groups with several people in each group.

abstract


Class is an abstraction from the basic concepts of object-oriented development. Next, we try to make the most of our brain's abstract ability to make a complete abstraction of the above organization diagram so that we can extract classes:

First abstraction process

We divide the organizational structure into groups: companies, departments, groups, and members, with size-dependent relationships.

Second abstraction process

Again, we categorize the general similarity: the original department as a large department, the group as a small department, and the general manager, director, group leader, and group member as employees.

Define Classes
Static data

After two abstractions, we classify entities that are common in the organization structure into the following three categories:

  • Company: Represents the external presence of the organizational structure; the company is a complete entity composed of departments and staff.
  • Departments: Represents the units of operation in the organizational structure; Departments can be divided into different business units by type.
  • Employee: Represents the smallest unit in the organization structure; employees exist in different business units by position.

With these different descriptions of abstract classes, we can describe them in specific code.

/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Company Classes
 *  * @author  zhuhuix
 * @date 2020-05-22
 */
public class Company {

    //Company id
    private int id;
    //Corporate name
    private String name;
    //List of company departments
    private List<Department>  departments;
}

/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Department Classes
 *
 * @author  zhuhuix
 * @date 2020-05-22
 */
public class Department {
    
    //Department id
    private int id;
    //Department Name
    private String name;
    //Superior departments
    private Integer parentId;
     //Department Staff List
    private List<Employee> employees;
}

/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Staff Classes
 *
 * @author  zhuhuix
 * @date 2020-05-22
 */
public class Employee {

    //Employee id
    private int id;
    //Employee Name
    private String name;
    //Employee gender
    private String sex;
    //Employee age
    private int age;
    //Employee position
    private String position;
    //Date of entry
    private Date hireDate;
    //Current salary
    private BigDecimal salary;
}
dynamic behavior

Above we define the class from the static data level, that is, the class has its own attributes: such as the Employee class has the name, age and other attributes, the department class has the Department name, the superior department and other attributes, and the company class has the Department list and other attributes.
But these static attributes aren't enough; we need to graphically map the organization's architecture, that is, let these classes add member methods.

  • Companies will increase or remove departments
  • Departments may increase or remove staff
  • Employees will be promoted, transferred or transferred.
    We incorporate these behaviors, which involve dynamic changes in the organization's architecture, into the corresponding classes as member methods:
/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Company Classes
 *
 * @author  zhuhuix
 * @date 2020-05-22
 */
public class Company {
	...
    //Additional departments
    public void addDepartment(Department department){
        this.departments.add(department);
    }
    //Cancellation of Departments
    public void deleteDepartment(Department department){
        this.departments.remove(department);
    }
    //Location Department
    Department findDepartmentByName(String departmentName) {
        Optional<Department> optional = departments.stream().filter(department ->
                department.getName().equals(departmentName)).findFirst();
        return optional.get();
    }

}
/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Department Classes
 *  * @author  zhuhuix
 * @date 2020-05-22
 */
public class Department {

	...
    //Add staff
    public void addEmployee(Employee employee){
        this.employees.add(employee);
    }
    //Remove staff
    public void deleteEmployee(Employee employee){
        this.employees.remove(employee);
    }
    
}
/**
 * Understanding Classes and Objects with Organization Architecture Diagrams--Staff Classes
 *  * @author  zhuhuix
 * @date 2020-05-22
 */
public class Employee {

  	...
    //Promotion, transfer, transfer
    public void setPosition(String position){
        this.position = position;
    }
}
The complete structure of the class

create object

In all the above representations, we have done an abstraction of the organization architecture diagram, that is, we have completed the definition of the class.
Next we move from abstract concepts to the real world

  • The concrete implementation object of company abstract class is Internet Co., Ltd.
  • The specific implementation objects of department abstract class are Group A/Group B of products under General Manager, R&D, Marketing, Product and Product Departments..
  • The concrete implementation objects of the staff abstract class are General Manager Mike, R&D Director Jack, Product Group A Jerry...
    That is, the units, departments, and people that actually exist on the organization chart. Of course, these objects must be created by classes. Here is the sample code to create the organization structure:
Initialization of objects

The concept of class constructor must be introduced here. Some people don't understand what the constructor is really useful for. You can simply think of it as the initialization of the object, such as the name of the company when the company is established, the name of the Department when the Department is set up, the responsibilities of the department, and the salary of the position when people join the company.Let's first add corresponding constructors to the above classes.

public class Company {

    //Company id
    private int id;
    //Corporate name
    private String name;
	//List of company departments
    private List<Department> departments;
    //Initialization Constructor
    Company(int id,String name){
        this.id=id;
        this.name=name;
        this.departments = new ArrayList<>();
    }
}

public class Department {

    //Department id
    private int id;
    //Department Name
    private String name;
    //Superior departments
    private Integer parentId;
    //Department Staff List
    private List<Employee> employees;
    //Initialization Constructor
    Department(int id,String name,Integer parentId){
        this.id=id;
        this.name=name;
        this.parentId=parentId;
        this.employees = new ArrayList<>();
    }
    }
	...
}
public class Employee {

    //Employee id
    private int id;
    //Employee Name
    private String name;
    //Employee gender
    private String sex;
    //Employee age
    private int age;
    //Employee position
    private String position;
    //Date of entry
    private Date hireDate;
    //Current salary
    private BigDecimal salary;

    //Initialization Constructor
    public Employee(int id, String name, String sex, int age, String position, Date hireDate, BigDecimal salary) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.position = position;
        this.hireDate = hireDate;
        this.salary = salary;
    }
 }
Establishment of organization structure

With classes and constructors that complete the initialization of objects, we have the ability to build an entire organizational structure. Next, we build a complete organizational structure for the company:

/**
 * Understanding classes and objects with an organization architecture diagram
 *
 * @author zhuhuix
 * @date 2020-05-22
 */
public class OrganizationBuild {
    //Define a global static variable as the id that controls the organization's architecture
    public static int id = 0;
    ;

    public static void main(String[] args) {

        //establishment of company
        Company company = new Company(id++, "Internet Co., Ltd.");

        //The company establishes the general manager's Office
        Department manageDept = new Department(id++, "General Manager Office", null);
        company.addDepartment(manageDept);

        //Set up Product Department under General Manager
        Department productDept = new Department(id++, "Product Department", manageDept.getId());
        company.addDepartment(productDept);
        //Set up Group A and Group B under Product Department
        company.addDepartment(new Department(id++, "product A group", productDept.getId()));
        company.addDepartment(new Department(id++, "product B group", productDept.getId()));

        //Set up R&D department under General Manager's Office
        Department developmentDept = new Department(id++, "R&D Department", manageDept.getId());
        company.addDepartment(developmentDept);
        //Set up software and hardware groups under R&D Department
        company.addDepartment(new Department(id++, "Software Group", developmentDept.getId()));
        company.addDepartment(new Department(id++, "Hardware Group", developmentDept.getId()));

        //Set up Marketing Department under General Manager
        Department marketDept = new Department(id++, "Marketing Department", manageDept.getId());
        company.addDepartment(marketDept);
        //Set up Creative Group and Channel Group under Marketing Department
        company.addDepartment(new Department(id++, "Creative Group", marketDept.getId()));
        company.addDepartment(new Department(id++, "Channel Group", marketDept.getId()));

        //Personnel appointment in general manager's Office
        manageDept.addEmployee(new Employee(id++, "Mike", "male", 35, "General manager",
                new Date(), BigDecimal.valueOf(100000)));
        manageDept.addEmployee(new Employee(id++, "Tom", "male", 34, "Vice General Manager",
                new Date(), BigDecimal.valueOf(60000)));

        //R&D Personnel Appointment
        developmentDept.addEmployee(new Employee(id++, "Jack", "male", 30, "Director of R&D",
                new Date(), BigDecimal.valueOf(40000)));
        company.findDepartmentByName("Software Group")
                .addEmployee(new Employee(id++, "Kate", "female", 26, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));
        company.findDepartmentByName("Hardware Group")
                .addEmployee(new Employee(id++, "Will", "male", 24, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));

        //Personnel Appointment for Product Department
        productDept.addEmployee(new Employee(id++, "Jerry", "male", 28, "Product Director",
                new Date(), BigDecimal.valueOf(40000)));
        company.findDepartmentByName("product A group")
                .addEmployee(new Employee(id++, "Merry", "female", 28, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));
        company.findDepartmentByName("product B group")
                .addEmployee(new Employee(id++, "Leo", "male", 27, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));

        //Marketing Personnel Appointment
        marketDept.addEmployee(new Employee(id++, "Rose", "female", 29, "Marketing Director",
                new Date(), BigDecimal.valueOf(40000)));
        company.findDepartmentByName("Creative Group")
                .addEmployee(new Employee(id++, "Amy", "", 25, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));
        company.findDepartmentByName("Channel Group")
                .addEmployee(new Employee(id++, "Tony", "male", 23, "Group members",
                        new Date(), BigDecimal.valueOf(20000)));

        //Traverse company organization structure
        int deptCount = 0;
        int empCount = 0;
        Iterator<Department> deptIterator = company.getDepartments().iterator();
        while (deptIterator.hasNext()) {
            deptCount++;
            Department department = deptIterator.next();
            System.out.println("department:" + department.getName());
            if (department.getEmployees() != null) {
                Iterator<Employee> empIterator = department.getEmployees().iterator();
                while (empIterator.hasNext()) {
                    empCount++;
                    Employee employee = empIterator.next();
                    System.out.print(" personnel:" + employee.getName() + " position:" + employee.getPosition() + ",");
                }
                System.out.println();
            }
        }
        System.out.println("Total number of departments:" + deptCount);
        System.out.println("Total number of employees:" + empCount);

    }

}

The output is as follows:

epilogue

Object-oriented programming, we mainly exercise a kind of ability and master a skill:
One is the ability to understand abstractly: to identify specific objects through classification, to understand abstractly, to find common static data and dynamic behavior, and to form a complete class definition.
One technique is to extract the attribute nouns of abstract things from static state to dynamic state, and then to form a member method based on the specific behavior of these attribute nouns.

Tags: Java Attribute supervisor Programming

Posted on Tue, 26 May 2020 20:46:04 -0400 by duall