Providedis a Java file called Shapes.java. It implements classes that “draw” var
ID: 3756800 • Letter: P
Question
Providedis a Java file called Shapes.java. It implements classes that “draw” various shapes, and implements the Factory Method design pattern (Note, it doesn’t actually do a graphical draw, it simple prints to the console).
For this part, do the following questions:
1. Add a class Triangle to Shapes.java, and submit it as ShapesTriangle.java (10 points)
2.Modify your ShapesTriangle.java so that it uses an Abstract Factory to create different sets of shapes (one particular type of factory object creates “thick shapes,” (for example ThickCircle), and another creates "thin shapes", but each factory object can create all the shapes: circles, squares, triangles, etc.) Submit it as ShapesTriangleAbstract.java.
Shapes.java
import java.util.*;
interface Shape {
void draw();
void erase();
}
class BadShapeCreation extends Exception {
public BadShapeCreation(String msg) {
super(msg);
}
}
abstract class ShapeFactory {
protected abstract Shape create();
private static Map<String, ShapeFactory> factories =
new HashMap<String,ShapeFactory>();
public static void addFactory(String id, ShapeFactory f) {
factories.put(id, f);
}
// A Template Method:
public static final Shape createShape(String id) throws BadShapeCreation {
if(!factories.containsKey(id))
throw new BadShapeCreation(id);
return ((ShapeFactory)factories.get(id)).create();
}
}
class CircleFactory extends ShapeFactory {
public Shape create() {
return new Circle();
}
}
class SquareFactory extends ShapeFactory {
public Shape create() {
return new Square();
}
}
class Circle implements Shape {
public Circle() {}
public void draw() {
System.out.println("Circle.draw"); }
public void erase() {
System.out.println("Circle.erase"); }
public static void addFactory() {
ShapeFactory.addFactory( "Circle", new CircleFactory());
}
}
class Square implements Shape {
public Square() {}
public void draw() {
System.out.println("Square.draw");
}
public void erase() {
System.out.println("Square.erase");
}
public static void addFactory() {
ShapeFactory.addFactory( "Square", new SquareFactory());
}
}
public class Shapes {
String shlist[] = { "Circle", "Square", "Square",
"Circle", "Circle", "Square" };
List<Shape> shapes = new ArrayList<Shape>();
public void test() {
Circle.addFactory();
Square.addFactory();
// This just makes sure it will complete
// without throwing an exception.
try {
for(int i = 0; i < shlist.length; i++)
shapes.add(ShapeFactory.createShape(shlist[i]));
}
catch(BadShapeCreation e) {
e.printStackTrace(System.err);
assert(false); // Fail the unit test
}
Iterator i = shapes.iterator();
while(i.hasNext()) {
Shape s = (Shape)i.next();
s.draw();
s.erase();
}
}
public static void main(String args[]) {
new Shapes().test();
}
}
Explanation / Answer
Note: Please change the test() method accordingly, if you want to change the testcases. I have not changed it bcause not in requirement.
Ans 1:
/**
* ShapesTriangle.java
*/
import java.util.*;
interface Shape {
void draw();
void erase();
}
class BadShapeCreation extends Exception {
public BadShapeCreation(String msg) {
super(msg);
}
}
abstract class ShapeFactory {
protected abstract Shape create();
private static Map<String, ShapeFactory> factories =
new HashMap<String,ShapeFactory>();
public static void addFactory(String id, ShapeFactory f) {
factories.put(id, f);
}
// A Template Method:
public static final Shape createShape(String id) throws BadShapeCreation {
if(!factories.containsKey(id))
throw new BadShapeCreation(id);
return ((ShapeFactory)factories.get(id)).create();
}
}
class CircleFactory extends ShapeFactory {
public Shape create() {
return new Circle();
}
}
class SquareFactory extends ShapeFactory {
public Shape create() {
return new Square();
}
}
class Circle implements Shape {
public Circle() {}
public void draw() {
System.out.println("Circle.draw"); }
public void erase() {
System.out.println("Circle.erase"); }
public static void addFactory() {
ShapeFactory.addFactory( "Circle", new CircleFactory());
}
}
class Square implements Shape {
public Square() {}
public void draw() {
System.out.println("Square.draw");
}
public void erase() {
System.out.println("Square.erase");
}
public static void addFactory() {
ShapeFactory.addFactory( "Square", new SquareFactory());
}
}
class Triangle implements Shape {
public Triangle() {}
public void draw() {
System.out.println("Triangle.draw"); }
public void erase() {
System.out.println("Triangle.erase"); }
}
public class Shapes {
String shlist[] = { "Circle", "Square", "Square",
"Circle", "Circle", "Square" };
List<Shape> shapes = new ArrayList<Shape>();
public void test() {
Circle.addFactory();
Square.addFactory();
// This just makes sure it will complete
// without throwing an exception.
try {
for(int i = 0; i < shlist.length; i++)
shapes.add(ShapeFactory.createShape(shlist[i]));
}
catch(BadShapeCreation e) {
e.printStackTrace(System.err);
assert(false); // Fail the unit test
}
Iterator i = shapes.iterator();
while(i.hasNext()) {
Shape s = (Shape)i.next();
s.draw();
s.erase();
}
}
public static void main(String args[]) {
new Shapes().test();
}
}
Ans 2:
/**
* ShapesTriangleAbstract.java
*/
import java.util.*;
interface Shape {
void draw();
void erase();
}
class BadShapeCreation extends Exception {
public BadShapeCreation(String msg) {
super(msg);
}
}
abstract class ShapeFactory {
protected abstract Shape create();
private static Map<String, ShapeFactory> factories =
new HashMap<String,ShapeFactory>();
public static void addFactory(String id, ShapeFactory f) {
factories.put(id, f);
}
// A Template Method:
public static final Shape createShape(String id) throws BadShapeCreation {
if(!factories.containsKey(id))
throw new BadShapeCreation(id);
return ((ShapeFactory)factories.get(id)).create();
}
}
class CircleFactory extends ShapeFactory {
public Shape create() {
return new Circle();
}
}
class SquareFactory extends ShapeFactory {
public Shape create() {
return new Square();
}
}
class TriangleFactory extends ShapeFactory {
public Shape create() {
return new Triangle();
}
}
class Circle implements Shape {
public Circle() {}
public void draw() {
System.out.println("Circle.draw"); }
public void erase() {
System.out.println("Circle.erase"); }
public static void addFactory() {
ShapeFactory.addFactory( "Circle", new CircleFactory());
}
}
class Square implements Shape {
public Square() {}
public void draw() {
System.out.println("Square.draw");
}
public void erase() {
System.out.println("Square.erase");
}
public static void addFactory() {
ShapeFactory.addFactory( "Square", new SquareFactory());
}
}
class Triangle implements Shape {
public Triangle() {}
public void draw() {
System.out.println("Triangle.draw"); }
public void erase() {
System.out.println("Triangle.erase"); }
public static void addFactory() {
ShapeFactory.addFactory( "Triangle", new TriangleFactory());
}
}
public class Shapes {
String shlist[] = { "Circle", "Square", "Square",
"Circle", "Circle", "Square" };
List<Shape> shapes = new ArrayList<Shape>();
public void test() {
Circle.addFactory();
Square.addFactory();
// This just makes sure it will complete
// without throwing an exception.
try {
for(int i = 0; i < shlist.length; i++)
shapes.add(ShapeFactory.createShape(shlist[i]));
}
catch(BadShapeCreation e) {
e.printStackTrace(System.err);
assert(false); // Fail the unit test
}
Iterator i = shapes.iterator();
while(i.hasNext()) {
Shape s = (Shape)i.next();
s.draw();
s.erase();
}
}
public static void main(String args[]) {
new Shapes().test();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.