-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstractFactory.java
More file actions
140 lines (109 loc) · 2.82 KB
/
abstractFactory.java
File metadata and controls
140 lines (109 loc) · 2.82 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Abstract factory pattern
//This programs creates shapes and their respective colors using factories.
public interface Shape{
void draw();
}
public interface Color{
void fill();
}
public class Rectangle implements Shape{
public void draw(){
System.out.println("In Rectangle::draw() method.");
}
}
public class Square implements Shape{
public void draw(){
System.out.println("In Square::draw() method.");
}
}
public class Circle implements Shape{
public void draw(){
System.out.println("In Circle::draw() method.");
}
}
public class Red implements Color{
public void fill(){
System.out.println("In Red::draw() method.");
}
}
public class Green implements Color{
public void fill(){
System.out.println("In Green::draw() method.");
}
}
public class Blue implements Color{
public void fill(){
System.out.println("In Blue::draw() method.");
}
}
public abstract class absFactory{
abstract color getColor(String color);
abstract Shape getShape(String shape);
}
public class ShapeFactory extends absFactory{
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else{
return null;
}
}
Color getColor(String color){
return null;
}
}
public class ColorFactory extends absFactory{
public Shape getShape(String shapeType){
return null;
}
Color getColor(String color){
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
}else if(color.equalsIgnoreCase("GREEN")){
return new Green();
}else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}else{
return null;
}
}
}
public class FactoryProducer{
public static absFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}else{
return null;
}
}
}
public class abstractFactory{
public static void main(String[] args){
absFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
Shape shape3 = shapeFactory.getShape("SQUARE");
shape3.draw();
absFactory colorFactory = FactoryProducer.getFactory("COLOR");
Color color1 = colorFactory.getColor("RED");
color1.fill();
Color color2 = colorFactory.getColor("GREEN");
color2.fill();
Color color3 = colorFactory.getColor("BLUE");
color3.fill();
}
}