[英]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 在这里,经理和员工都有名称属性
only employee has a designation attribute 只有员工具有指定属性
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 ) 如下所示,使用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 但
Employee.changeName()
method private for prevent the manager changing employee name is it correct approach? Employee.changeName()
方法设为私有,以防止经理更改员工姓名,这是正确的方法吗? 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? changeDesignation()
方法位于Employee
类中,既可以自己访问Employee
类,也可以访问Manager
类,这意味着经理可以更改员工的指定,而员工也可以更改其指定,我想防止这种情况的发生,并且使经理是唯一的一个可以更改员工名称的人。如何在代码中实现它? 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. 话虽如此,如果您不希望任何其他类更改一个类的属性,则应将该字段设为私有,并且完全不提供设置器。
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. 我想您可以拥有一个Designation类,并使其成为仅管理员可以实例化的类。 But even then the employee could set it to null.
但是即使那样,员工也可以将其设置为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类持有对一个或多个员工的引用,那么只有管理者才能访问该Designation对象。
As I said, I think that's one of those times where OO modeling doesn't really fit the real world. 正如我所说的,我认为这是OO建模并不真正适合现实世界的时代之一。 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. 私有的
changeName
似乎毫无意义。
For the 2nd bullet you need to add a constraint like { can only be invoked by Manager instances }
. 对于第二个项目符号,您需要添加一个约束,例如
{ can only be invoked by Manager instances }
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.