Results 1 to 5 of 5
  1. #1
    ochrasy is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Aggregation: hidind the reference

    Hi
    I want to have methods of aggregated objects accessible, but the reference to the aggregated object not changable externally. Is there any way to do that?

    To illustrate the problem here's some example code:

    Java Code:
    class Test {
      public static void main(String[] args) {
    
      Car car = new Car(new Diesel()); //creates the aggregated object
     
      car.engine = new Otto();       // this must not be allowed!!
      car.installEngine(new Otto()); //this should be used instead
      car.engine.switchOff();          //and this should work, too!!
      }
    }
    
    //some more information...
    
    interface Engine { public void switchOff(); }
    class Otto implements Engine { .. }
    class Diesel implements Engine { .. }
    
    class Car {
        protected Engine engine;                  // The car might have an engine.
    
        Car(Engine e) {engine = e; return;};   // Constructor sets enginetype
        
        protected void installEngine(Engine e){// This method should be used to change the engine
            engine.switchOff();
            engine = e;
            return;
        }
    }
    Thank's in advance
    Chris

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Make the 'engine' member a private member of a Car object and implement a setEngine( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ochrasy is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks, but if engine is private member i cannot access car.engine.switchOff() externally. But i want to do so, because when there are a lot of methods implemented in Engine-Classes I don't want to repeat them all in Car.
    Last edited by ochrasy; 07-22-2011 at 12:19 PM. Reason: adding information

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ochrasy View Post
    Thanks, but if engine is private member i cannot access car.engine.switchOff() externally. But i want to do so, because when there are a lot of methods implemented in Engine-Classes I don't want to repeat them all in Car.
    car.getEngine().switchOff()

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ochrasy is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. How to implement aggregation relationship in java?
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 07-21-2011, 01:49 PM
  2. Trouble with aggregation
    By gto400no1 in forum New To Java
    Replies: 12
    Last Post: 05-05-2010, 03:41 AM
  3. Aggregation !!
    By Sary in forum New To Java
    Replies: 13
    Last Post: 04-14-2010, 06:57 AM
  4. UML association,aggregation etc generator
    By alexander.s in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 06:27 PM
  5. inheritance and aggregation
    By java_fun2007 in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •