-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFactoryPattern.java
82 lines (69 loc) · 1.7 KB
/
FactoryPattern.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package designPatternsAndPrinciples;
/**
*
* @author chengfeili
* Jun 5, 2017 10:21:53 PM
*
* The factory pattern, sometimes referred to as the factory method
* pattern, is a creational pattern based on the idea of using a factory
* class to produce instances of objects based on a set of input
* parameters. It is similar to the builder pattern, although it is
* focused on supporting class polymorphism.
*/
abstract class Food {
private int quantity;
public Food(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public abstract void consumed();
}
class Hay extends Food {
public Hay(int quantity) {
super(quantity);
}
public void consumed() {
System.out.println("Hay eaten: " + getQuantity());
}
}
class Pellets extends Food {
public Pellets(int quantity) {
super(quantity);
}
public void consumed() {
System.out.println("Pellets eaten: " + getQuantity());
}
}
class Fish extends Food {
public Fish(int quantity) {
super(quantity);
}
public void consumed() {
System.out.println("Fish eaten: " + getQuantity());
}
}
public class FactoryPattern {
public static Food getFood(String animalName) {
switch (animalName) {
case "zebra":
return new Hay(100);
case "rabbit":
return new Pellets(5);
case "goat":
return new Pellets(30);
case "polar bear":
return new Fish(10);
}
// Good practice to throw an exception if no matching subclass could be
// found
throw new UnsupportedOperationException("Unsupported animal: " + animalName);
}
}
class ZooKeeper {
public static void main(String[] args) {
final Food food = FactoryPattern.getFood("polar bear");
food.consumed();
}
}