2016-10-27 3 views
-1

나는이 주위에 방법이 있어야하는 것처럼 정말로 느낀다.오브젝트의 멤버에 대한 액세스를 컴포지션 소유의 오브젝트로만 제한하는 방법이 있습니까?

내가 소유자 클래스의 구성 요소로 많은 수의 개체를 가지고 있다고 상상해보십시오. 이 소유자 클래스의 클라이언트에게 멤버에게 쉽게 액세스 할 수 있도록 제공하므로 모든 개체를 공개합니다. 이러한 객체는 모두 모든 멤버를 공개합니다. 그러나 구성 요소 중 한 구성 요소는 소유자의 고객이 액세스 할 수 없으며 소유자 자신 만 액세스 할 수 있습니다.

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 디렉토리에서 컴파일 할 때 주석을 적용 할 수 있습니까?

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 
} 

의도 한대로 작동합니까?

+0

을, 당신은 객체가 있다고 가정 할 수 있습니다 A와 B, C, D 필드가 있고 다른 사람들이 B, C, D를 할 수있게 만들지 만 그 방법은 A 만 가능해야합니까? –

+1

http://stackoverflow.com/questions/182278/is-there-a-way-to-simulate-the-c-friend-concept-in-java –

+1

나는 그것이 중복이라고 말하고 싶지 않지만, 이것은 친구 개념처럼 들린다. http://stackoverflow.com/questions/182278/is-there-a-way-to-simulate-the-c-friend-concept-in-java – Compass

답변

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 

} 그래서

+0

이것은 좋은 해결책이 될 것입니다, 문제는 제가 충분히 제 질문에 명확하지 않다는 것입니다. – FinnTheHuman

+0

편집을 요청하십시오 – Joza100

+0

응답을 편집했습니다 – Joza100

관련 문제