Skip to content

Commit 1f2149f

Browse files
Implementing Abstract-Facory design pattern
1 parent 251a416 commit 1f2149f

13 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package design.pattern.abstractFactory;
2+
3+
public abstract class AbstractFactory {
4+
5+
public abstract Color getColor(String type);
6+
7+
public abstract Shape getShape(String type);
8+
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class AbstractFactoryPatternDemo {
4+
5+
private static final String SHAPE = "SHAPE";
6+
private static final String CIRCLE = "CIRCLE";
7+
private static final String RECTANGLE = "RECTANGLE";
8+
private static final String SQUARE = "SQUARE";
9+
10+
private static final String COLOR = "COLOR";
11+
private static final String RED = "RED";
12+
private static final String GREEN = "GREEN";
13+
private static final String BLUE = "BLUE";
14+
15+
16+
public static void main(String[] args) {
17+
18+
AbstractFactory shapeFactory = FactoryProducer.getFactory(SHAPE);
19+
Shape shape = shapeFactory.getShape(CIRCLE);
20+
shape.draw();
21+
22+
FactoryProducer.getFactory(SHAPE).getShape(RECTANGLE).draw();
23+
FactoryProducer.getFactory(SHAPE).getShape(SQUARE).draw();
24+
25+
FactoryProducer.getFactory(COLOR).getColor(RED).fill();
26+
FactoryProducer.getFactory(COLOR).getColor(GREEN).fill();
27+
FactoryProducer.getFactory(COLOR).getColor(BLUE).fill();
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class Blue implements Color{
4+
5+
@Override
6+
public void fill() {
7+
System.out.println("Inside the Blue:fill() method");
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class Circle implements Shape{
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("Inside the Circle:draw() method");
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package design.pattern.abstractFactory;
2+
3+
public interface Color {
4+
5+
void fill();
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class ColorFactory extends AbstractFactory{
4+
5+
@Override
6+
public Color getColor(String color) {
7+
8+
if(color.equalsIgnoreCase("RED")){
9+
return new Red();
10+
}
11+
else if(color.equalsIgnoreCase("GREEN")){
12+
return new Green();
13+
}
14+
else if(color.equalsIgnoreCase("BLUE")){
15+
return new Blue();
16+
}
17+
else{
18+
return null;
19+
}
20+
}
21+
22+
@Override
23+
public Shape getShape(String type) {
24+
return null;
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class FactoryProducer {
4+
5+
public static AbstractFactory getFactory(String choice){
6+
7+
if(choice.equalsIgnoreCase("SHAPE")){
8+
return new ShapeFactory();
9+
}
10+
else if(choice.equalsIgnoreCase("COLOR")){
11+
return new ColorFactory();
12+
}
13+
else{
14+
return null;
15+
}
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class Green implements Color{
4+
5+
@Override
6+
public void fill() {
7+
System.out.println("Inside Green:fill() method");
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class Rectangle implements Shape{
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("Inside the Rectangle:draw() method");
8+
}
9+
}
10+
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package design.pattern.abstractFactory;
2+
3+
public class Red implements Color{
4+
5+
@Override
6+
public void fill() {
7+
System.out.println("Inside Red:fill() method.");
8+
}
9+
}

0 commit comments

Comments
 (0)