Created
December 6, 2017 16:32
-
-
Save woogieboogie93/a036e7a3841085309d8acc92371e95a7 to your computer and use it in GitHub Desktop.
FactoryPatternExample created by woogie_boogie - https://repl.it/@woogie_boogie/FactoryPatternExample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Scanner; | |
| interface ShapeIF { | |
| public void display(); | |
| public void setColor(String s); | |
| } | |
| abstract class ShapeFactory { | |
| private static ArrayList<ShapeIF> list = new ArrayList<ShapeIF>(); | |
| public abstract ShapeIF createShape(); | |
| public static void add(ShapeIF s) { | |
| list.add(s); | |
| } | |
| public static void showInfo() { | |
| for (int i = 0; i < list.size(); i++) { | |
| ShapeIF o = list.get(i); | |
| o.display(); | |
| } | |
| } | |
| } | |
| class CircleFactory extends ShapeFactory { | |
| public Shape createShape() { | |
| Scanner input = new Scanner(System.in); | |
| double radius = 1; | |
| boolean radiusOkay = false; | |
| do { | |
| System.out.print("Enter the radius for a new circle: "); | |
| try { | |
| radius = input.nextDouble(); | |
| radiusOkay = true; | |
| } | |
| catch (Exception e) { | |
| System.out.println("Your input is illegal. Retry it!"); | |
| input.nextLine(); | |
| } | |
| } while (radiusOkay == false); | |
| return new Circle(radius); | |
| } | |
| } | |
| class RectangleFactory extends ShapeFactory { | |
| public Shape createShape() { | |
| Scanner input = new Scanner(System.in); | |
| double w = 1, h = 1; | |
| boolean inputOkay = false; | |
| do { | |
| System.out.print("Enter the width and height for a new rectangle: "); | |
| try { | |
| w = input.nextDouble(); | |
| h = input.nextDouble(); | |
| inputOkay = true; | |
| } | |
| catch (Exception e) { | |
| System.out.println("Your inputs are illegal. Retry it!"); | |
| input.nextLine(); | |
| } | |
| } while (inputOkay == false); | |
| return new Rectangle(w, h); | |
| } | |
| } | |
| abstract class Shape implements ShapeIF{ | |
| public String color = "Blue"; | |
| protected static int instanceCount = 0; | |
| protected int id; | |
| public Shape() { | |
| id = ++instanceCount; | |
| } | |
| public void setColor(String c) { | |
| color = c; | |
| } | |
| public abstract double getSize(); | |
| public void showInfo() { | |
| System.out.print(color + "(size=" + getSize() + ")"); | |
| } | |
| public void display() { | |
| this.showInfo(); | |
| } | |
| } | |
| class Rectangle extends Shape { | |
| private double width = 1; | |
| private double height = 1; | |
| public Rectangle(double w, double h) { | |
| super(); | |
| width = w; | |
| height = h; | |
| } | |
| public double getSize() { | |
| return width*height; | |
| } | |
| public void showInfo() { | |
| super.showInfo(); | |
| System.out.println("Rectangle #" + id + " with width=" + width + " and height=" + height); | |
| } | |
| } | |
| class Circle extends Shape { | |
| private double radius = 1; | |
| public Circle(double r) { | |
| super(); | |
| radius = r; | |
| } | |
| public double getSize() { | |
| return radius*radius*Math.PI; | |
| } | |
| public void showInfo() { | |
| super.showInfo(); | |
| System.out.println("Circle #" + id + " with radius=" + radius); | |
| } | |
| } | |
| public class Main { | |
| public static void main(String[] args) { | |
| Scanner input = new Scanner(System.in); | |
| ShapeIF s; | |
| ShapeFactory sf; | |
| while (true) { | |
| int choice = 1; | |
| boolean choiceOkay = false; | |
| do { | |
| System.out.print("Circle(1) or Rectangle(2)?: (0 for Exit)"); | |
| try { | |
| choice = input.nextInt(); | |
| choiceOkay = true; | |
| } | |
| catch (Exception e) { | |
| System.out.println("Your choice is illegal. Retry it!"); | |
| input.nextLine(); | |
| } | |
| } while (choiceOkay == false); | |
| if (choice == 1) { | |
| sf = new CircleFactory(); | |
| } else if (choice == 2) { | |
| sf = new RectangleFactory(); | |
| } else { | |
| break; | |
| } | |
| s = sf.createShape(); | |
| System.out.print("Enter its color: "); | |
| s.setColor(input.next()); | |
| ShapeFactory.add(s); | |
| } | |
| ShapeFactory.showInfo(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment