Sie sind auf Seite 1von 5

Design patterns

Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern. Lets take an example to understand this pattern. Example: Lets suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Ms <Name>. The skeleton of the code can be given here. public class Person { // name string public String name; // gender : M or F private String gender; public String getName() { return name; } public String getGender() { return gender; } }// End of class This is a simple class Person having methods for name and gender. Now, we will have two sub-classes, Male and Female which will print the welcome message on the screen. public class Male extends Person { public Male(String fullName) { System.out.println("Hello Mr. "+fullName); } }// End of class

Also, the class Female public class Female extends Person { public Female(String fullNname) { System.out.println("Hello Ms. "+fullNname); } }// End of class Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided. public class SalutationFactory { public static void main(String args[]) { SalutationFactory factory = new SalutationFactory(); factory.getPerson(args[0], args[1]); } public Person getPerson(String name, String gender) { if (gender.equals("M")) return new Male(name);

else if(gender.equals("F")) return new Female(name); else return null; } }// End of class This class accepts two arguments from the system at runtime and prints the names. Running the program: After compiling and running the code on my computer with the arguments Prashant and M: java Prashant M The result returned is: Hello Mr. Prashant. When to use a Factory Pattern? The Factory patterns can be used in following cases: 1. When a class does not know which class of objects it must create. 2. A class specifies its sub-classes to specify which objects to create. 3. In programmers language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

Prototype Design Patterns: Today we're going to look at the Prototype design pattern. Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. - Gof Type Object Creational Solution The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it is dealing with as long as the concrete prototype extends or implements the prototype interface/class. The concrete prototype object is responsible for cloning itself and returning the cloned object. The pattern enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at runtime as long as they conform to the abstract prototype.

Java Sample Code


Example 1: The following is an example of the Prototype Pattern. The prototype object is an Animal object. TheAnimal prototype contains two concrete prototype subclasses called Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains aretrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it. Animal.java The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes. Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.

Code: view source print? 01.public Animal clone() { 02. 03.Animal clonedAnimal = null; 04.try { 05.clonedAnimal = (Animal) super.clone(); 06.clonedAnimal.setDescription(description); 07.clonedAnimal.setNumberOfLegs(numberOfLegs); 08.clonedAnimal.setName(name); 09.} catch (CloneNotSupportedException e) { 10.e.printStackTrace(); 11.} // catch 12.return clonedAnimal; 13.} // method clone Sheep.java The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object. Code: view source print? 1.public class Sheep extends Animal { Chicken.java The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object. Code: public class Chicken extends Animal { AnimalCreator.java The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are 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 without the client knowing the type of the prototype. Code: view source print? 01.public Animal retrieveAnimal(String kindOfAnimal) { 02.if ("Chicken".equals(kindOfAnimal)) { 03.return (Animal) chicken.clone(); 04.} else if ("Sheep".equals(kindOfAnimal)) { 05.return (Animal) sheep.clone(); 06.} // if 07. 08.return null; 09.} // method retrieveAnimal AnimalClient.java The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of

typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype. Code: view source print? 01.AnimalCreator animalCreator = new AnimalCreator(); 02.Animal[] animalFarm = new Animal[8]; 03.animalFarm[0] = animalCreator.retrieveAnimal("Chicken"); 04.animalFarm[1] = animalCreator.retrieveAnimal("Chicken"); 05.animalFarm[2] = animalCreator.retrieveAnimal("Chicken"); 06.animalFarm[3] = animalCreator.retrieveAnimal("Chicken"); 07.animalFarm[4] = animalCreator.retrieveAnimal("Sheep"); 08.animalFarm[5] = animalCreator.retrieveAnimal("Sheep"); 09.animalFarm[6] = animalCreator.retrieveAnimal("Sheep"); 10.animalFarm[7] = animalCreator.retrieveAnimal("Sheep"); 11.for (int i= 0; i<=7; i++) { 12.System.out.println(animalFarm[i].helloAnimal()); 13.} // for Console The following is the output of the AnimalClient calling different concrete prototype objects. Cluck cluck World. I am Chicken1. I have 2 legs. Cluck cluck World. I am Chicken2. I have 2 legs. Cluck cluck World. I am Chicken3. I have 2 legs. Cluck cluck World. I am Chicken4. I have 2 legs. Meeeeeee World. I am Sheep1. I have 4 legs. Meeeeeee World. I am Sheep2. I have 4 legs. Meeeeeee World. I am Sheep3. I have 4 legs. Meeeeeee World. I am Sheep4. I have 4 legs.

Sequence Diagram

Das könnte Ihnen auch gefallen