forked from ramram43210/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimalCache.java
More file actions
32 lines (26 loc) · 866 Bytes
/
Copy pathAnimalCache.java
File metadata and controls
32 lines (26 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Hashtable;
public class AnimalCache
{
private static Hashtable<String, Animal> AnimalMap = new Hashtable<String, Animal>();
public static Animal getAnimal( String animalType )
{
Dog dog = (Dog) AnimalMap.get(animalType);
if( dog == null )
{
Dogleg dogleg = new Dogleg();
dogleg.setNumberOflegs(4);
Dogear dogear = new Dogear();
dogear.setNumberOfEars(2);
Dogtail dogtail = new Dogtail();
dogtail.setTailLength("long");
Dogeye dogeye = new Dogeye();
dogeye.setNumberOfEyes(2);
dog = new Dog("Tommy", 3, "White", dogleg, dogtail, dogear, dogeye);
AnimalMap.put("dog", dog);
System.out.println("New Dog Object is created and return\n");
return dog;
}
System.out.println("\nCloned Dog Object is created and return\n");
return dog.clone();
}
}