Java Program: Use the Factory pattern to address the following challenge... Your
ID: 3868682 • Letter: J
Question
Java Program: Use the Factory pattern to address the following challenge...
Your job is to make it easy to obtain a Pet from a factory-like class:
Pet is an abstract class that has 2 attributes
Name
Sound
You should create 3 concrete types of pets
Parakeet (makes the sound "Tweet tweet")
Dog (makes the sound "Woof woof")
Lion (makes the sound "Roar roar")
You should create a factory class that allows the caller to create pets by specifying
Name
Acres of land where pets will live
The # of acres of land determines what kind of pet is assigned
1 acre or less: parakeet
4 acre or less: dog
9 acre or less: lion
Smaller pets are always preferred over larger ones
For example if the caller has 2 acres, then a new Dog should be returned.
*Your solution should minimally include 6 classes:
The abstract Pet and concrete classes (4 classes)
Factory class (1 class)
An additional Test class
Should demonstrate getting pets for the following acre sizes: 0, 3, 7
For each pet retrieved, print out its sound (to the output).
Explanation / Answer
public class SimpleClass {
private final Integer arg1;
private final Integer arg2;
SimpleClass(Integer arg1, Integer arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
public Integer getArg1(){
return arg1;
}
public Integer getArg2(){
return args;
}
}
...
public class BusinessClassXYZ {
public static void someFunction(){
SimpleClass mySimpleClass = new SimpleClass(1,2);
}
}
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return INSTANCE;
}
...
}
...
public class ClassXXX{
...
public static void someFunctionInClassXXX(){
Singleton instance = Singleton.getInstance();
//some stuff
}
}
...
public class ClassYYY{
...
public static void someFunctionInClassYYY(){
Singleton instance = Singleton.getInstance();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.