View RSS Feed

fatabass

Factory Pattern Example

Rating: 1 votes, 1.00 average.
by , 03-04-2012 at 09:10 PM (690 Views)
Java Code:
package factoryPattern2;

public abstract class Product {
	
	private String productName;
	
	public void setName(String name) {
		this.productName = name;
	}
	
	public String getName() {
		return productName;
	}

	public void putInABox() {
		System.out.println("Putting the product in a box..");
	}

}


package factoryPattern2;

public class ConcreteProductTypeA extends Product {
	
	ConcreteProductTypeA(){
		this.setName("Concrete Product Type A");
	}

}


package factoryPattern2;

public abstract class Creator {
	
	public Product operationsOnCreatedProduct() {
		
		Product productBeingCreated;
		productBeingCreated = this.create();
		productBeingCreated.putInABox();
		return productBeingCreated;
	
	}
	
	abstract Product create();

}


package factoryPattern2;

public class ConcreteCreatorTypeA extends Creator {

	@Override
	Product create() {
		
		Product productBeingCreated = new ConcreteProductTypeA();
		return productBeingCreated;
		
	}
}


package factoryPattern2;

public class TestClass {

	public static void main(String[] args) {
		Creator myCreator = new ConcreteCreatorTypeA();
		Product myProduct = myCreator.operationsOnCreatedProduct();
		System.out.println(myProduct.getName());
	}
}

Submit "Factory Pattern Example" to Facebook Submit "Factory Pattern Example" to Digg Submit "Factory Pattern Example" to del.icio.us Submit "Factory Pattern Example" to StumbleUpon Submit "Factory Pattern Example" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments