package
com.exmpl.abstractFactoryPattern;
public interface Animal {
void getVoice();
}
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package
com.exmpl.abstractFactoryPattern;
public class Cat implements Animal {
@Override
public void getVoice() {
System.out.println("Cat voice ---
Meow Meow Meow Meow Meow");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class Dog implements Animal {
@Override
public void getVoice() {
System.out.println("Dog voice ---
Bark Bark Bark Bark Bark");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class Tiger implements Animal {
@Override
public void getVoice() {
System.out.println("Tiger voice
--- Growl Growl Growl Growl Growl");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public interface Bird {
void getColour();
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class Eagle implements Bird {
@Override
public void getColour() {
System.out.println("Eagle is
golden colour.");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class Peacock implements Bird {
@Override
public void getColour() {
System.out.println("Peacock is
full of different colours.");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class Sparrow implements Bird {
@Override
public void getColour() {
System.out.println("Sparrow is
gray colour.");
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public abstract class AbstractFactory {
abstract Animal
getAnimal(String animalType);
abstract Bird getBird(String
birdType);
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
/*This is the factory class, which
create specific animal based on animal type supplied.*/
public class AnimalFactory extends AbstractFactory{
final static String TIGER = "tiger";
final static String DOG = "dog";
final static String CAT = "cat";
public Animal
getAnimal(String animalType) {
Animal
returnAnimal = null;
if (animalType != null) {
if
(animalType.equalsIgnoreCase(TIGER)) {
returnAnimal
= new Tiger();
}
else if
(animalType.equalsIgnoreCase(DOG)) {
returnAnimal
= new Dog();
}
else if
(animalType.equalsIgnoreCase(CAT)) {
returnAnimal
= new Cat();
}
}
return returnAnimal;
}
@Override
Bird
getBird(String birdType) {
return null;
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
/*This is the factory class, which
create specific bird based on bird type supplied.*/
public class BirdFactory extends AbstractFactory{
final static String EAGLE = "eagle";
final static String PEACOCK = "peacock";
final static String SPARROW = "sparrow";
public Bird getBird(String
birdType) {
Bird
returnBird = null;
if (birdType != null) {
if
(birdType.equalsIgnoreCase(EAGLE)) {
returnBird
= new Eagle();
}
else if
(birdType.equalsIgnoreCase(PEACOCK)) {
returnBird
= new Peacock();
}
else if
(birdType.equalsIgnoreCase(SPARROW)) {
returnBird
= new Sparrow();
}
}
return returnBird;
}
@Override
Animal
getAnimal(String animalType) {
return null;
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
/*
* This is the factory of factory classes
(AnimalFactory and BirdFactory),
* which create specific factory based on
supplied parameter.
*/
public class FactoryProducer {
final static String ANIMAL = "animal";
final static String BIRD = "bird";
public static AbstractFactory
getFactory(String choice) {
if
(choice.equalsIgnoreCase(ANIMAL)) {
return new AnimalFactory();
}
else if
(choice.equalsIgnoreCase(BIRD)) {
return new BirdFactory();
}
return null;
}
}
---------------------------------------------------------------------------------package
com.exmpl.abstractFactoryPattern;
public class ClientClass {
public static void main(String[] args)
{
// Get Animal
factory
AbstractFactory
animalFactory = FactoryProducer.getFactory("Animal");
// Get tiger object
from AnimalFactory and check it's voice.
Animal
animal1 = animalFactory.getAnimal("tiger");
animal1.getVoice();
// Get dog object
from AnimalFactory and check it's voice.
Animal
animal2 = animalFactory.getAnimal("dog");
animal2.getVoice();
// Get cat object
from AnimalFactory and check it's voice.
Animal
animal3 = animalFactory.getAnimal("cat");
animal3.getVoice();
// Get Bird factory
AbstractFactory
birdFactory = FactoryProducer.getFactory("Bird");
// Get eagle object
from BirdFactory and check it's color.
Bird
bird1 = birdFactory.getBird("eagle");
bird1.getColour();
// Get peacock
object from BirdFactory and check it's color.
Bird
bird2 = birdFactory.getBird("peacock");
bird2.getColour();
// Get sparrow
object from BirdFactory and check it's color.
Bird
bird3 = birdFactory.getBird("sparrow");
bird3.getColour();
}
}
---------------------------------------------------------------------------------
Output รจ
Tiger voice --- Growl Growl Growl
Growl Growl
Dog voice --- Bark Bark Bark Bark Bark
Cat voice --- Meow Meow Meow Meow Meow
Eagle is golden colour.
Peacock is full of different colours.
Sparrow is gray colour.
No comments:
Post a Comment