有没有办法将访问对象成员的权限仅限于通过组合拥有它的对象?

时间:2016-10-27 15:51:08

标签: java oop design-patterns composition class-visibility

我真的觉得必须有办法解决这个问题。

想象一下,我有大量对象作为所有者类的组件。我希望为其成员提供对此所有者类的客户端的轻松访问,因此我将所有这些对象公开。每个对象也都公开其所有成员。但是,组件的一个成员不应该由其所有者的客户访问,只能由其所有者自己访问:

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就像一个实用工具类,可以在其他应用程序中重用。

也许有一个注释在编译时强制执行一些好的lib吗?

编辑:我忘了提到ComponentObject是现实生活中的参数化类型,并且所有者中的每个字段都以不同方式参数化。我试图抽象细节,所以我们可以专注于设计问题本身,但我抽象了太多。我会发布一些更类似于真实问题的内容:

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
}

这会按预期工作吗?

1 个答案:

答案 0 :(得分:1)

我明白你想要什么,我认为这是不可能的。 但是,还有一种方法可以做到这一点!

在所有者类中创建一个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   

}