Skip to content

Commit 16c2629

Browse files
committed
prototype added
1 parent 26c383c commit 16c2629

File tree

12 files changed

+342
-0
lines changed

12 files changed

+342
-0
lines changed

prototype/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# prototype
2+
This is repository of http://androidcode.pl blog. It shows uses of Prototype in Android. It is a part of Design Patterns - Prototype method post in the blog.

prototype/example/Main.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Main {
2+
3+
public static void main() {
4+
5+
//new empty board initialize
6+
List<Figure> figures = new ArrayList();
7+
8+
//user creates some figures
9+
Circle prototypeCircle = new Circle(10, 5, 4);
10+
prototypeCircle.draw();
11+
figures.add(prototypeCircle);
12+
13+
Triangle prototypeTriangle = new Triangle(new Point(1, 1), new Point(1, 5), new Point(5, 1));
14+
prototypeTriangle.draw();
15+
figures.add(prototypeTriangle);
16+
17+
//after creation basic giures, user just copy them instead of creat new ones from the beginning
18+
//some of them are modifed
19+
Circle copy1 = prototypeCircle.copy();
20+
copy1.setColor(Color.BLUE);
21+
copy.setRadius(2);
22+
figures.add(copy1);
23+
24+
Triangle copy2 = prototypeTriangle.copy();
25+
copy2.setColor(Color.GREEN);
26+
triangle.setPoint1(new Point(5, 5));
27+
figures.add(copy2);
28+
29+
//user calculates themself area and perimeter of the figures and check them with real results
30+
//calculate area and delimeter for all figures on the board
31+
for(Figure figure : figures) {
32+
double area = figures.area();
33+
double perimeter = figures.perimeter();
34+
//check the answer
35+
}
36+
}
37+
}

prototype/example/dummy/Color.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public enum Color {
2+
3+
WHITE(0),
4+
BLACK(1),
5+
BLUE(2),
6+
GREEN(3);
7+
//etc
8+
9+
int color;
10+
11+
public Color(int color) {
12+
this.color = color;
13+
}
14+
}

prototype/example/dummy/Point.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Point {
2+
3+
private int x, y;
4+
5+
public Point(int x, int y) {
6+
this.x = x;
7+
this.y = y;;
8+
}
9+
10+
public int getX() {
11+
return x;
12+
}
13+
14+
public int getY() {
15+
return y;
16+
}
17+
18+
public void setX(int x) {
19+
this.x = x;
20+
}
21+
22+
public void setY(int y) {
23+
this.y = y;
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Circle extends Figure {
2+
3+
private int x, y, radius;
4+
5+
public Circle() {
6+
this(0, 0, 0);
7+
}
8+
9+
public Circle(int x, int y, int radius) {
10+
super();
11+
this.x = x;
12+
this.y = y;
13+
this.radius = radius;
14+
}
15+
16+
public Circle(Circle prototype) {
17+
super(prototype);
18+
if(prototype != null) {
19+
this.x = prototype.x;
20+
this.y = prototype.y;
21+
this.radius = prototype.radius;
22+
}
23+
}
24+
25+
@Override
26+
public Figure copy() {
27+
//shallow copy
28+
return new Circle(this);
29+
}
30+
31+
@Override
32+
public void draw() {
33+
//draw Circle on the board in own way
34+
}
35+
36+
@Override
37+
public double area() {
38+
return Math.PI * Math.pow(radius);
39+
}
40+
41+
@Override
42+
public double perimeter() {
43+
return 4 * Math.PI * radius;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public abstract class Figure {
2+
3+
private int color;
4+
private int borderColor;
5+
6+
public Figure() {
7+
this.color = Color.WHITE;
8+
this.borderColor = Color.BLACK;
9+
}
10+
11+
public Figure(int color, int borderColor) {
12+
this.color = color;
13+
this.borderColor = borderColor;
14+
}
15+
16+
public Figure(Figure prototype) {
17+
this.color = prototype.color;
18+
this.borderColor = prototype.borderColor;
19+
}
20+
21+
public abstract Prototype copy();
22+
public abstract void draw();
23+
public abstract double area();
24+
public abstract double perimeter();
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Triangle {
2+
3+
private Point point1, point2, point3;
4+
5+
public Triangle() {
6+
this(new Point(0, 0), new Point(0, 0), new Point(0, 0));
7+
}
8+
9+
public Triangle(Point point1, Point point2, Point point3) {
10+
super();
11+
this.point1 = point1;
12+
this.point2 = point2;
13+
this.point3 = point3;
14+
}
15+
16+
public Triangle(Triangle prototype) {
17+
super(prototype);
18+
if(prototype != null) {
19+
this.point1 = new Point(prototype.point1.getX(), prototype.point1.getY());
20+
this.point2 = new Point(prototype.point2.getX(), prototype.point2.getY());
21+
this.radius = new Point(prototype.point3.getX(), prototype.point3.getY());
22+
}
23+
}
24+
25+
@Override
26+
public Figure copy() {
27+
//deep copy
28+
return new Triangle(this);
29+
}
30+
31+
@Override
32+
public void draw() {
33+
//draw Triangle on the board in own way
34+
}
35+
36+
@Override
37+
public double area() {
38+
double p = perimeter() / 2;
39+
double a = getSideLength(point1, point2);
40+
double b = getSideLength(point1, point3);
41+
double c = getSideLength(point2, point3);
42+
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
43+
}
44+
45+
@Override
46+
public double perimeter() {
47+
double a = getSideLength(point1, point2);
48+
double b = getSideLength(point1, point3);
49+
double c = getSideLength(point2, point3);
50+
return a + b + c;
51+
}
52+
53+
private double getSideLength(Point point1, Point point2) {
54+
double size = Math.pow(point1.getX() - point2.getX()) + Math.pow(point1.getY() - point2.getY());
55+
return Math.sqrt(size);
56+
}
57+
}

prototype/pattern/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//create prototype to copy
5+
Prototype1 prototype1 = new Prototype1(10.0);
6+
Prototype2 prototype2 = new Prototype2(5, new Complex(3, true));
7+
8+
//copy prototypes to save time and resources
9+
List<Prototype> clones = new ArrayList<>();
10+
Prototype1 clone1 = prototype1.copy();
11+
clones.add(clone1);
12+
Prototype2 clone2 = prototype2.copy();
13+
clones.add(clone2);
14+
//do more clones
15+
16+
//do some action for every copies
17+
for(Prototype copy : clones)
18+
copy.action();
19+
}
20+
}

prototype/pattern/dummy/Complex.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Complex {
2+
3+
private int number;
4+
private boolean availability;
5+
6+
public Complex(int number, boolean availability) {
7+
this.number = number;
8+
this.availability = availability;
9+
}
10+
11+
public int getNumber() {
12+
return number;
13+
}
14+
15+
public boolean getAvailability() {
16+
return availability;
17+
}
18+
19+
public void setNumber(int number) {
20+
this.number = number;
21+
}
22+
23+
public void setAvailability(boolean availability) {
24+
this.availability = availability;
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public abstract class Prototype {
2+
3+
//some fields
4+
//some common methods
5+
6+
public Prototype() {
7+
//initialize fields
8+
}
9+
10+
public Prototype(Prototype prototype) {
11+
//copy all fields from prototype into this instance
12+
}
13+
14+
//in Java clone is abstract method of Object class so no need copy extra method
15+
public abstract Prototype copy();
16+
public abstract void action();
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Prototype1 extends Prototype {
2+
3+
private double price;
4+
//other primitive fields
5+
6+
public Prototype1() {
7+
this(0);
8+
}
9+
10+
public Prototype1(int price) {
11+
super();
12+
this.price = price;
13+
}
14+
15+
public Prototype1(Prototype1 prototype) {
16+
super(prototype);
17+
if(prototype != null) {
18+
this.price = prototype.price;
19+
//do the same for all fields
20+
}
21+
}
22+
23+
@Override
24+
public Prototype copy() {
25+
//shallow copy
26+
return new Prototype1(this);
27+
}
28+
29+
@Override
30+
public void action() {
31+
//do specific action for Prototype1
32+
}
33+
34+
//some other methods
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Prototype2 extends Prototype {
2+
3+
private int value;
4+
private Complex complex; //has only int and boolean fields
5+
//other object and primitive types
6+
7+
public Prototype2() {
8+
this(0, new Complex(0, false));
9+
}
10+
11+
public Prototype2(int value, Complex complex) {
12+
super();
13+
this.value = value;
14+
this.complex = complex;
15+
}
16+
17+
public Prototype2(Prototype2 prototype) {
18+
super(prototype);
19+
if(prototype != null) {
20+
this.value = prototype.value;
21+
//copy complex object by getting all primitive types or provide copy method/copy constructor
22+
this.complex = new Complex(prototype.complex.getNumber(), prototype.complex.getAvailability());
23+
//do the same for all fields
24+
}
25+
}
26+
27+
@Override
28+
public Prototype copy() {
29+
//deep copy
30+
return new Prototype2(this);
31+
}
32+
33+
@Override
34+
public void action() {
35+
//do specific action for Prototype2
36+
}
37+
38+
//some other methods
39+
}

0 commit comments

Comments
 (0)