Add creational patterns, Interpreter; remove scripts; update README
This commit is contained in:
24
01-creational/prototype/Circle.java
Normal file
24
01-creational/prototype/Circle.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package prototype;
|
||||
|
||||
public class Circle extends Shape {
|
||||
private int radius;
|
||||
|
||||
public Circle(int radius, String color) {
|
||||
this.radius = radius;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
/** Copy constructor used by clone() */
|
||||
private Circle(Circle source) {
|
||||
super(source);
|
||||
this.radius = source.radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Circle clone() { return new Circle(this); }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Circle{color=" + color + ", radius=" + radius + ", pos=(" + x + "," + y + ")}";
|
||||
}
|
||||
}
|
||||
33
01-creational/prototype/Main.java
Normal file
33
01-creational/prototype/Main.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package prototype;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Prototype Pattern Demo ===\n");
|
||||
|
||||
List<Shape> originals = new ArrayList<>();
|
||||
originals.add(new Circle(10, "red"));
|
||||
originals.add(new Rectangle(20, 30, "blue"));
|
||||
|
||||
// Clone each shape - no need to know the concrete type
|
||||
List<Shape> copies = new ArrayList<>();
|
||||
for (Shape shape : originals) {
|
||||
copies.add(shape.clone());
|
||||
}
|
||||
|
||||
// Mutate copies - originals are unaffected
|
||||
copies.get(0).setColor("green");
|
||||
copies.get(0).move(5, 5);
|
||||
copies.get(1).setColor("yellow");
|
||||
|
||||
System.out.println("--- Originals ---");
|
||||
originals.forEach(System.out::println);
|
||||
|
||||
System.out.println("\n--- Clones (mutated) ---");
|
||||
copies.forEach(System.out::println);
|
||||
|
||||
System.out.println("\nSame instance? " + (originals.get(0) == copies.get(0)));
|
||||
}
|
||||
}
|
||||
26
01-creational/prototype/Rectangle.java
Normal file
26
01-creational/prototype/Rectangle.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package prototype;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public Rectangle(int width, int height, String color) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
private Rectangle(Rectangle source) {
|
||||
super(source);
|
||||
this.width = source.width;
|
||||
this.height = source.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle clone() { return new Rectangle(this); }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Rectangle{color=" + color + ", size=" + width + "x" + height + ", pos=(" + x + "," + y + ")}";
|
||||
}
|
||||
}
|
||||
22
01-creational/prototype/Shape.java
Normal file
22
01-creational/prototype/Shape.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package prototype;
|
||||
|
||||
/** Prototype - every shape can clone itself */
|
||||
public abstract class Shape {
|
||||
protected String color;
|
||||
protected int x;
|
||||
protected int y;
|
||||
|
||||
protected Shape(Shape source) {
|
||||
this.color = source.color;
|
||||
this.x = source.x;
|
||||
this.y = source.y;
|
||||
}
|
||||
|
||||
protected Shape() {}
|
||||
|
||||
public abstract Shape clone();
|
||||
|
||||
public void setColor(String color) { this.color = color; }
|
||||
public String getColor() { return color; }
|
||||
public void move(int x, int y) { this.x = x; this.y = y; }
|
||||
}
|
||||
Reference in New Issue
Block a user