-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstractFactoryT1.java
More file actions
144 lines (116 loc) · 2.78 KB
/
abstractFactoryT1.java
File metadata and controls
144 lines (116 loc) · 2.78 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
141
142
143
144
public interface Shape{
void draw();
}
//Concrete classes
public class Rectangle implements Shape{
@Override
public void draw(){
System.out.println("In the Rectangle class.");
}
}
public class Square implements Shape{
@Override
public void draw(){
System.out.println("in the Square class.");
}
}
public class Circle implements Shape{
@Override
public void draw(){
System.out.println("In the Circle class");
}
}
//interface colors
public interface Color{
void fill();
}
public class Red implements Color{
@Override
public void fill(){
System.out.println("In Red class.");
}
}
public class Green implements Color{
@Override
public void fill(){
System.out.println("In Green class");
}
}
public class Blue implements Color{
@Override
public void fill(){
System.out.println("In Blue class.");
}
}
public abstract class AbstractFactory{
abstract Color getColor(String color);
abstract Shape getShape(String shape);
}
public class ShapeFactory extends AbstractFactory{
@Override
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
}else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
@Override
Color getColor(String color){
return null;
}
}
public class colorFactory extends AbstractFactory{
@Override
public Shape getShape(String shapeType){
return null;
}
public 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();
}
return null;
}
}
public class FactoryProducer{
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")){
return new colorFactory();
}
return null;
}
}
//driver
public class AbstractFactoryT1{
public static void main(String[] args){
AbstractFactory factory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = factory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = factory.getShape("RECTANGLE");
shape2.draw();
Shape shape3 = factory.getShape("SQUARE");
shape3.draw();
AbstractFactory color = FactoryProducer.getFactory("COLOR");
Color color1 = color.getColor("GREEN");
color1.fill();
Color color2 = color.getColor("RED");
color2.fill():
Color color3 = color.getColor("BLUE");
color3.fill();
}
}