View RSS Feed

Java Object

Design Principle: Choose Composition vs. Inheritance

Rate this Entry
by , 11-30-2011 at 02:48 AM (4142 Views)
With composition or inheritance you can place sub-objects inside your new class. If you do this explicitly than you are using composition otherwise if you do this implicitly, you are using inheritance. I will outline the difference between the two below as well as point out one of the key design patterns to use in your development. Prefer composition vs inheritance.

Composition is used when you want the features of an existing class to be included inside your new class, but not be parts of its interface. In this situation, you are looking to embed an object so that you can use it to implementationnt features in your new class, the user of your new class sees only the interface you’ve defined for the new class rather than the interface of the embedded objects. To realize this, you embed private objects of existing classes inside your new class. There might be other times, where you decide to allow the class user to directly access the composition of your new class (i.e.to make the member objects public). The member objects use implementation hiding themselves, in order to protect the inner workings of these objects. When the user knows you’re assembling a a series of components, this approach gives you flexibility while the the interface makes it easier to understand. A example is shown below:

Java Code:
public class Wheel {
	public void inflate(int psi) {
	}

	public void rollup() {
	}

	public void rolldown() {
	}
}


public class Door {
	public void open() {
	}

	public void close() {
	}
}


public class FrontWing {
	
	public void increaselift() {
	}

	public void increasedrag() {
	}
}


public class RearWing {
	
	public void increaselift() {
	}

	public void increasedrag() {
	}
}

public class Stablizer {

	public void rudderleft() {
	}

	public void rudderright() {
	}
}

public class Engine {
	public void start(){
	}
	
	public void stop(){
	}
	
	public void accelerate(){
	}
	
	public void decelerate(){
	}
}

public class Airplane {
	public Engine engine = new Engine();
	public Wheel frontWheel = new Wheel();
	public Wheel[] rearWheel = new Wheel[2];
	public Door door = new Door();
	public FrontWing leftFW = new FrontWing();
	public FrontWing rightFW = new FrontWing();
	public RearWing leftRW = new RearWing();
	public RearWing rightRW = new RearWing();

	public static void main(String[] args) {
		Airplane airplane = new Airplane();
		airplane.frontWheel.inflate(300);
		airplane.rearWheel[0].inflate(300);
		airplane.rearWheel[1].inflate(300);
		airplane.rightFW.increaselift();
		airplane.leftFW.increaselift();
		airplane.engine.accelerate();
	}
}
In this example the composition of a airplane is a key part of the analysis of the problem. Remember that the fields in this example are public for demonstration purposes. If you are actually developing a component, the principles of encapsulation and information hiding would apply and all of these fields would be kept private. Finally if you think about this component you will notice that composition expresses itself in terms of has-a relationships.

Submit "Design Principle: Choose Composition vs. Inheritance" to Facebook Submit "Design Principle: Choose Composition vs. Inheritance" to Digg Submit "Design Principle: Choose Composition vs. Inheritance" to del.icio.us Submit "Design Principle: Choose Composition vs. Inheritance" to StumbleUpon Submit "Design Principle: Choose Composition vs. Inheritance" to Google

Comments