Results 1 to 2 of 2
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default Composition in Java

    I'm having a hard time implementing a simple composition example in Java:

    Java Code:
    public class CompositionPattern {
    
    	public static void main(String[] args) {
    		Udp udp = new Udp();
    		Imap imap = new Imap();
    		udp.startService();
    		imap.startService();
    	}
    
    }
    
    public class Imap {
    	private Responder responder;
    	
    	public Imap(){
    		this.responder = new Responder(this);
    	}
    	
    	public void startService(){
    		responder.doStuff();
    	}
    	
    	public boolean supportsMe(){
    		return true;
    	}
    }
    
    public class Udp {
    	private Responder responder;
    	
    	public Udp(){
    		this.responder = new Responder(this);
    	}
    	
    	public void startService(){
    		responder.doStuff();
    	}
    	
    	public boolean supportsMe(){
    		return false;
    	}
    }
    
    public class Responder {
    	Object parent;
    	
    	public Responder(Object parent){
    		this.parent = parent;
    	}
    	
    	public void doStuff(){
    		if(parent.supportsMe()){
    			System.out.println("Parent supports me.");
    		} else {
    			System.out.println("Parent doesn't support me.");
    		}
    	}
    }
    The above won't compile. It says "The method supportsMe() is undefined for the type Object". I understand that I stored parent as an Object. But in reality it is not simply Object, but a Udp object or Imap object. The point is to make Responder generic. I don't want to have to cast the Object to Udp or Imap. Any solutions so I can keep this generic?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Composition in Java

    This is where interfaces come in.
    Define an interface (named something sensible) that represents the functionality you want to be able to accomplish on the parent attribute in Responder.

    Currently that is the supportsMe() method.
    Then the 'parent' attribute should be of that type.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. A Question on Composition
    By typedef in forum AWT / Swing
    Replies: 3
    Last Post: 03-03-2014, 07:05 AM
  2. Composition JAVA Programming HELP (Im new to java)
    By judemartin99 in forum New To Java
    Replies: 1
    Last Post: 03-29-2013, 02:42 AM
  3. Composition and Inheritance
    By BeyondWorldsEnd in forum New To Java
    Replies: 3
    Last Post: 01-08-2012, 11:19 AM
  4. I need some help with composition, i think
    By Nerijus in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 07:24 PM
  5. What is the Composition in Java
    By Santoshbk in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2008, 12:04 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
  •