简体   繁体   中英

Limit the scope of a method to one other class only in oop

This is a simple scenario in a office between employee and his manager. In actual world manager can manage many employees but for simplicity sake in here consider the situation between one employee and his manager

in here both manager and employee have name attribute

  • manager can only change his name
  • employee can only change his name

only employee has a designation attribute

  • designation can only change by the manger

For the above scenario i created this Class diagram

在此处输入图片说明

implement the above scenario using java code as shown below ( please note this code as pseudo code and also orchestration code not include,syntax can be wrong )

class Manager{
  private String name;
  private Employee employee;


    //assign name and create association link between employee 
    Manager(Employee employee,String managerName ){
        this.employee    = employee;
        this.name        = managerName;
    }

    //manager can change his name
    private void changeName(String managerName){
        this.name = managerName;
    }


    //only manager can change Employee designation
    private void changeEmpDesignation(String empDesignation){
        this.employee.changeDesignation(empDesignation);
    }

}

/* --------------------------- */


class Employee{
    private String name;
    private String designation;

    Employee(String name ,String designation ){
        this.designation = designation;
        this.name        = name;
    }

    //only Employee can change his name 
    private void changeName(String empName){
        this.name = empName;
    }

    //only manager can change Employee designation
    public void changeDesignation(String empDesignation){
        this.designation = empDesignation;
    }

}

But

  1. In here I make Employee.changeName() method private for prevent the manager changing employee name is it correct approach?
  2. changeDesignation() method is in Employee class can access both Employee class it self and also Manager class this means manager can change designation of the employee and employee can also change his/her designation, I want to prevent this happening and make manager is the only one be able to change employee designation.how to implement it in code?

Well, there are some things to consider in this scenario. Even though it makes a lot of sense if you think as real world entities, it's usually not a good OO practice to let an object change another object's state.

That being said, if you don't want any other class to change one class' property you should make the field private and provide no setter at all.

As for the designation, you can't really stop an object from modifying its properties, unless it's final, but then the manager couldn't change it either.

I suppose you could have an Designation class, and make it so that only the manager can instantiate it. But even then the employee could set it to null.

Or you could have the Designation class hold the reference to one or many employees, then only the manager could access the designation objects.

As I said, I think that's one of those times where OO modeling doesn't really fit the real world. Classes should be responsible for its own state, and hold its own set of rules for any state changes. Any other class should be able only o ask or request a change.

Hope it helps!

Well, I don't know the domain or in what context you are asking, but the name shall be part of the constructor, rather than the parameter of an operation. There "might" be reasons one can change the name. But in real world, an object is named on creation and keeps this name until the very end. A private changeName just seems pointless.

For the 2nd bullet you need to add a constraint like { can only be invoked by Manager instances } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM