仅在oop中将方法的范围限制为另一个类

时间:2022-09-02 09:31:08

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

对于上面的场景,我创建了这个类图

仅在oop中将方法的范围限制为另一个类

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 )

使用java代码实现上面的场景如下图所示(请注意此代码为伪代码,也不包括编排代码,语法可能有误)

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. 在这里我将Employee.changeName()方法设为私有,以防止经理更改员工姓名是否正确?

  3. 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?
  4. changeDesignation()方法在Employee类中既可以访问Employee类,也可以访问Manager类,这意味着管理员可以更改员工的姓名,员工也可以更改自己的名称,我想防止这种情况发生并使管理者成为唯一的一个能够改变员工名称如何在代码中实现它?

2 个解决方案

#1


1  

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.

嗯,在这种情况下有一些事情需要考虑。即使你认为它是真实世界的实体也很有意义,但让对象改变另一个对象的状态通常不是一个好的OO实践。

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.

话虽这么说,如果你不希望任何其他类改变一个类的属性,你应该将该字段设为私有,并且根本不提供任何setter。

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.

我想你可以有一个Designation类,并使它只有管理器可以实例化它。但即使这样,员工也可以将其设置为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.

或者您可以让Designation类保留对一个或多个员工的引用,然后只有管理员才能访问指定对象。

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.

正如我所说,我认为这是OO建模并不真实适合现实世界的时代之一。类应该对自己的状态负责,并为任何状态更改保留自己的一套规则。任何其他类应该只能询问或要求更改。

Hope it helps!

希望能帮助到你!

#2


1  

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 }.

对于第二个项目符号,您需要添加一个约束,例如{只能由Manager实例调用}。

#1


1  

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.

嗯,在这种情况下有一些事情需要考虑。即使你认为它是真实世界的实体也很有意义,但让对象改变另一个对象的状态通常不是一个好的OO实践。

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.

话虽这么说,如果你不希望任何其他类改变一个类的属性,你应该将该字段设为私有,并且根本不提供任何setter。

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.

我想你可以有一个Designation类,并使它只有管理器可以实例化它。但即使这样,员工也可以将其设置为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.

或者您可以让Designation类保留对一个或多个员工的引用,然后只有管理员才能访问指定对象。

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.

正如我所说,我认为这是OO建模并不真实适合现实世界的时代之一。类应该对自己的状态负责,并为任何状态更改保留自己的一套规则。任何其他类应该只能询问或要求更改。

Hope it helps!

希望能帮助到你!

#2


1  

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 }.

对于第二个项目符号,您需要添加一个约束,例如{只能由Manager实例调用}。