开发者

Beginner object references question

If I instantiate an object in a main class, say:

SomeObject aRef = new SomeObject();

Then I instantiate another object from the main class, say:

AnotherObject xRef = new AnotherObject();

How can the instance of AnotherObject make use of the aRef reference to access the methods in SomeObject? (To use the sa开发者_JAVA技巧me instance of SomeObject)


Why not instantiate AnotherObject with a reference to the original SomeObject ?

e.g.

SomeObject obj = new SomeObject();
AnotherObject obj2 = new AnotherObject(obj);

and AnotherObject would look like:

// final used to avoid misreferencing variables and enforcing immutability
private final SomeObject obj;

public AnotherObject(final SomeObject obj) {
   this.obj = obj;
}

so AnotherObject has a reference to the previously created SomeObject. It can then use this reference to call methods on. If the original object is not required outside the scope of AnotherObject, then create it inside AnotherObject and enforce encapsulation that way.


I think what you are asking is a question about scope. You're asking how can xRef use aRef during execution? The answer is that the aRef reference needs to be passed into the xRef object when it's being instantiated

xRef = new AnotherObject(aRef)

or after the instantiation you could have

xRef.setSomeObject(aRef)


The answer to his question is making the first class a static class.


xRef.SetSomeObject(aRef);

where SetSomeObject has a signature like

public void SetSomeObject(SomeObject obj)
{
    obj.DoStuff();
}

and is a member function of the type AnotherObject.


The strategy design pattern and Decorator design pattern are 2 different ways you can do this.

For instance you can have:

class AnotherObject
{
    private SomeObject mySomeObject;
    public AnotherObject(SomeObject mySomeObject)
    {
        this.mySomeObject = mySomeObject;
    }

    function doSomethingUsingStrategy()
    {
        mySomeObject.doItTheMySomeObjectWay();
    }

    function setMySomeObject(SomeObject mySomeObject)
    {
        this.mySomeObject = mySomeObject;
    }
}

Then later on, you can use a different strategy:

myAnotherObject.setMySomeObject(new ExtendsSomeObject);
myAnotherObject.doSomethingUsingStrategy()


You need to provide the reference to aRef to instances of AnotherObject either in the constructor: AnotherObject xRef = new AnotherObject(aRef) or using setter methods: xRex.setSomeObject(aRef). In this case AnotherObject needs to have an instance variable to store aRef that can be used internally like:

class AnotherObject {
    SomeObject aRef;
    public AnotherObject(SomeObject aRef) {
        this.aRef = aRef;
    }
    public void doSomethingWithSomeObject() {
        aRef.doSomething();
    }
}

You could also pass instances of SomeObject to methods on AnotherObject that require them like xRef.doSomethingWithSomeObject(aRef).

class AnotherObject {
    public void doSomethingWithSomeObject(SomeObject aRef) {
        aRef.doSomething();
    }
}


There are a bunch of ways to do it (as pointed out by others). You really want to think about your object structure though...

Perhaps your main method shouldn't even be instantiating aRef, perhaps it should be instantiated inside xRef's constructor (this is the case where xRef tends to be a "part" of the functionality of aRef.

If aRef can have multiple instances at some point you may not want to store it off at all, you may want to pass it in whenever an xRef method uses it.

This is where you need to consider your object model at a business logic level. What are the relationships between the objects, etc.

(My guess is that you want xRef to instantiate aRef and keep the reference itself, then if your "main" really needed to talk with aRef it could either ask xRef to forward the message or ask xRef for it's instance of aRef.)


You have to pass the ref and then do something with it.

class AnotherObject {
     SomeObject someObject;

     public void setSomeObject( SomeObject some ) {
         this.someObject = some;
     }

     public void doSomethingWithSomeObject() {
         this.someObject.someMethod();
     }

     ..... rest of your code 
 }

That way you can use it like this in the main method

 public static void main( String [] args ) {
     SomeObject xRef = new SomeObject();
     AnotherObject aRef = new AnotherObject();
     // pass the ref... 
     aRef.setSomeObject( xRef );
     // use it
     aRef.doSomethingWithSomeObject();
 }

Is that what you need?


Could AnotherObject have a member or property that has a type of SomeObject? That would be another way to handle this too.

So, if there was a "SomeObjectMember" member of the AnotherObject class:

xRef.SomeObjectMember = aRef;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜