Design Patterns: Prototype

Design Paterns, Java, OOP Add comments

Prototype pattern: used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:

  • avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
  • avoid the inherent cost of creating a new object in the standard way (e.g., using the ‘new’ keyword) when it is prohibitively expensive for a given application.
    Here is UML diagram for Prototype pattern.
    Design Patterns - Prototype

    Design Patterns - Prototype


    Here is the code examples:

    /**
     * The Animal abstract class is the prototype in the "Prototype" Pattern. The
     * animal class contains properties that describe on a abstract level the different
     * prototypes that can exist in the pattern example.
     */
    public abstract class Animal implements Cloneable {
    	protected int numberOfLegs = 0;
    	protected String description = "";
    	protected String name = "";
    	public abstract String helloAnimal();	
    
    	/**
    	 * The clone method creates a clone of the current Animal object.
    	 */
    	public Animal clone() {
    		Animal clonedAnimal = null;
    		try {
    			clonedAnimal = (Animal) super.clone();
    			clonedAnimal.setDescription(description);
    			clonedAnimal.setNumberOfLegs(numberOfLegs);
    			clonedAnimal.setName(name);
    
    		} catch (CloneNotSupportedException e) {
    			e.printStackTrace();
    		}
    		return clonedAnimal;
    	}  
    
    	public String getDescription() {
    		return description;
    	}
    
    	public void setDescription(String description) {
    		this.description = description;
    	}
    
    	public int getNumberOfLegs() {
    		return numberOfLegs;
    	}
    
    	public void setNumberOfLegs(int numberOfLegs) {
    		this.numberOfLegs = numberOfLegs;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    
    /**
     * The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator
     * class contains two concrete prototypes that is initialized during the initialization of
     * the class. The AnimalCreator class forms part of the "Prototype" pattern by returning
     * a cloned object (Animal) to the client, withou the client knowing the type of the prototype.
     */
    public class AnimalCreator {
    
    	private Animal sheep = new Sheep();	
    
    	private Animal chicken = new Chicken();
    
    	public AnimalCreator() {
    		sheep.setName("Sheep");
    		sheep.setNumberOfLegs(4);
    		sheep.setDescription("Four legged creature that makes wool.");
    
    		chicken.setName("Chicken");
    		chicken.setNumberOfLegs(2);
    		chicken.setDescription("Two legged creature that crosses roads.");
    	} 
    
    	public Animal retrieveAnimal(String kindOfAnimal) {
    		if ("Chicken".equals(kindOfAnimal)) {
    			return (Animal) chicken.clone();
    		} else if ("Sheep".equals(kindOfAnimal)) {
    			return (Animal) sheep.clone();
    		} // if
    
    		return null;
    	} // method retrieveAnimal
    }
    
    public class Chicken extends Animal {
    	private int numberOfClones = 0;
    	public String helloAnimal() {
    	StringBuffer chickenTalk = new StringBuffer();
    	chickenTalk.append("Cluck cluck World. I am ");
    	chickenTalk.append(name);
    	chickenTalk.append(". I have ");
    	chickenTalk.append(numberOfLegs);
    	chickenTalk.append(" legs.");
    	return chickenTalk.toString();
    	} // helloAnimal
    
    	public Chicken clone() {
    		Chicken clonedChicken = (Chicken) super.clone();
    		String chickenName = clonedChicken.getName();
    		numberOfClones++;
    		clonedChicken.setName(chickenName + numberOfClones);
    		return clonedChicken;
    	} // method clone
    }
    
    public class Sheep extends Animal {
    	private int numberOfClones = 0;
    	public String helloAnimal() {
    	StringBuffer sheepTalk = new StringBuffer();
    	sheepTalk.append("Meeeeeee World. I am ");
    	sheepTalk.append(name);
    	sheepTalk.append(". I have ");
    	sheepTalk.append(numberOfLegs);
    	sheepTalk.append(" legs.");
    	return sheepTalk.toString();
    	}
    
    	public Sheep clone() {
    		Sheep clonedSheep = (Sheep) super.clone();
    		String sheepName = clonedSheep.getName();
    		numberOfClones++;
    		clonedSheep.setName(sheepName + numberOfClones);
    		return clonedSheep;
    	} 
    
    }
    

    and for the end client class

    public class AnimalClient {
    	public static void main(String[] args) {
    		AnimalCreator animalCreator = new AnimalCreator();
    		Animal[] animalFarm = new Animal[8];
    		animalFarm[0] = animalCreator.retrieveAnimal("Chicken");
    		animalFarm[1] = animalCreator.retrieveAnimal("Chicken");
    		animalFarm[2] = animalCreator.retrieveAnimal("Chicken");
    		animalFarm[3] = animalCreator.retrieveAnimal("Chicken");
    
    		animalFarm[4] = animalCreator.retrieveAnimal("Sheep");
    		animalFarm[5] = animalCreator.retrieveAnimal("Sheep");
    		animalFarm[6] = animalCreator.retrieveAnimal("Sheep");
    		animalFarm[7] = animalCreator.retrieveAnimal("Sheep");
    
    		for (int i= 0; i< =7; i++) {
    			System.out.println(animalFarm[i].helloAnimal());
    		} // for
    	}
    }
    

Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in