我真的觉得一定有办法解决这个问题。
假设我有大量的对象作为所有者类的组件。我想为这个所有者类的成员提供方便的客户端访问,所以我将所有这些对象设为公共。每个对象的所有成员都是public。但是组件的一个成员不能被其所有者的客户端访问,只能被其所有者自己访问:
public class ComponentObject
{
public int int_field;
public float float_field;
public Object object_field;
public Object public_method1()
{
//something;
}
public Object public_method2()
{
//something;
}
public Object restricted_to_owner_only()
{
//something;
}
}
//all clients of Owner should be able to access all the members of its components, except
//restricted_to_owner_only, which only Owner should be able to access
public class Owner
{
public ComponentObject component1;
public ComponentObject component2;
public ComponentObject component3;
//... lots of others
public ComponentObject component300;
}
有办法做到这一点吗?请注意,来自任何包的任何类都可以拥有ComponentObject
,因此在restricted_to_owner_only
上使用包级可见性似乎不是一种选择。ComponentObject
就像一个实用程序类,可以在其他应用中重用。
也许在一些不错的库中有一个注释在编译时强制执行?
EDIT:我忘了提到ComponentObject在现实生活中是一个参数化类型,并且Owner中的每个字段的参数化方式不同。我试图抽象出细节,这样我们就可以专注于设计问题本身,但我抽象得太多了。我将在下面发布一些更类似于实际问题的内容:
public class ComponentObject<T>
{
public int int_field;
public float float_field;
public T object_field;
//any method could return T or take T as an argument.
public T public_method1()
{
//something;
}
public Object public_method2()
{
//something;
}
public Object restricted_to_owner_only()
{
//something;
}
}
//all clients of Owner should be able to access all the members of its components, except
//restricted_to_owner_only, which only Owner should be able to access
public class Owner
{
public ComponentObject<String> component1;
public ComponentObject<File> component2;
public ComponentObject<Consumer<Boolean>> component3;
//... lots of others
public ComponentObject<Integer> component300;
}
编辑2(可能是一个解决方案):伙计们,受到罗密欧与朱丽叶爱情的启发,我写了这个解决方案,你能发现其中的错误吗?还是会如我所愿?
//add this class
public class OwnershipToken
{
private static int id_gen = 0;
public final int id = id_gen++;
@Override
public boolean equals(Object obj)
{
return (obj instanceof OwnershipToken) && ((OwnershipToken)obj).id == id;
}
@Override
public int hashCode()
{
return id;
}
}
//Then change this in ComponentObject<T>:
public class ComponentObject<T>
{
//add this field:
private final OwnershipToken ownershipToken;
//add this constructor
public ComponentObject(OwnershipToken onwershipToken)
{
this.ownershipToken = ownershipToken;
}
//change restricted_to_owner_only signature:
public Object restricted_to_owner_only(OwnershipToken ownershipToken)
{
//add this condition
if(this.ownershipToken.equals(ownershipToken)
//something;
}
}
//finally, Owner gains a field:
public class Owner
{
private final OwnershipToken ownershipToken = new OwnershipToken();
//... etc, remainder of the class
}
是否可以正常工作?
我明白你想要什么,我想那是不可能的。但是,仍然有一种方法可以做到!
在owner类中创建一个id:
private int id = new Random().nextInt(10000);
在ComponentObject: private id;
public ComponentObject(int id){
this.id = id;
}
public Object restricted(int id){
if(this.id != id)
return null;
else
return object;
}
在所有者:
private ComponentObject<String> string;
public Owner() {
string = new ComponentObject<>(id);
string.restricted(id);
//if the id is right it will return the restricted object, if not i will
//return null
}