Skip to content

Commit a6b608c

Browse files
committed
Visitor Pattern Add: exercise
1 parent 4fa7949 commit a6b608c

8 files changed

Lines changed: 204 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public class BookElement implements Element {
4+
private int price;
5+
private int weight;
6+
7+
public BookElement(int price, int weight) {
8+
this.price = price;
9+
this.weight = weight;
10+
}
11+
12+
@Override
13+
public float accept(Visitor visitor) {
14+
return visitor.visit(this);
15+
}
16+
17+
@Override
18+
public int getPrice() {
19+
return price;
20+
}
21+
22+
public void setPrice(int price) {
23+
this.price = price;
24+
}
25+
26+
@Override
27+
public int getWeight() {
28+
return weight;
29+
}
30+
31+
32+
public void setWeight(int weight) {
33+
this.weight = weight;
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public class CdElement implements Element{
4+
private int price;
5+
private int weight;
6+
7+
public CdElement(int price, int weight) {
8+
this.price = price;
9+
this.weight = weight;
10+
}
11+
12+
@Override
13+
public float accept(Visitor visitor) {
14+
return visitor.visit(this);
15+
}
16+
17+
@Override
18+
public int getPrice() {
19+
return price;
20+
}
21+
22+
public void setPrice(int price) {
23+
this.price = price;
24+
}
25+
26+
@Override
27+
public int getWeight() {
28+
return weight;
29+
}
30+
31+
public void setWeight(int weight) {
32+
this.weight = weight;
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
import java.util.List;
4+
5+
public class Client {
6+
public static void main(String[] args) {
7+
List<Element> items = List.of(
8+
new BookElement(35, 5),
9+
new CdElement(3, 2),
10+
new DvdElement(4, 2)
11+
);
12+
UsPostageVisitor usPostageVisitor = new UsPostageVisitor();
13+
SouthAmericaPostageVisitor southAmericaPostageVisitor = new SouthAmericaPostageVisitor();
14+
items.forEach(i -> i.accept(usPostageVisitor));
15+
items.forEach(i -> i.accept(southAmericaPostageVisitor));
16+
System.out.println("The total postage for shipping my items to the us is: " + usPostageVisitor.getTotal());
17+
System.out.println("The total postage for shipping my items to the us is: " + southAmericaPostageVisitor.getTotal());
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public class DvdElement implements Element {
4+
private int price;
5+
private int weight;
6+
7+
public DvdElement(int price, int weight) {
8+
this.price = price;
9+
this.weight = weight;
10+
}
11+
12+
@Override
13+
public float accept(Visitor visitor) {
14+
return visitor.visit(this);
15+
}
16+
17+
@Override
18+
public int getPrice() {
19+
return price;
20+
}
21+
22+
public void setPrice(int price) {
23+
this.price = price;
24+
}
25+
26+
@Override
27+
public int getWeight() {
28+
return weight;
29+
}
30+
31+
public void setWeight(int weight) {
32+
this.weight = weight;
33+
}
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public interface Element {
4+
float accept(Visitor visitor);
5+
int getWeight();
6+
int getPrice();
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public class SouthAmericaPostageVisitor implements Visitor{
4+
private float total = 0;
5+
6+
@Override
7+
public float visit(BookElement element) {
8+
return visit(element, 4);
9+
}
10+
11+
@Override
12+
public float visit(CdElement element) {
13+
return visit(element, 5);
14+
}
15+
16+
@Override
17+
public float visit(DvdElement element) {
18+
return visit(element, 6);
19+
}
20+
21+
public float getTotal() {
22+
return total;
23+
}
24+
25+
private float visit(Element element, float costPerUnitWeight) {
26+
if (element.getPrice() > 30) {
27+
return 0;
28+
}
29+
float cost = element.getWeight() * costPerUnitWeight;
30+
total += cost;
31+
return cost;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public class UsPostageVisitor implements Visitor {
4+
private float total = 0;
5+
6+
@Override
7+
public float visit(BookElement element) {
8+
return visit(element, 2);
9+
}
10+
11+
@Override
12+
public float visit(CdElement element) {
13+
return visit(element, 2.5F);
14+
}
15+
16+
@Override
17+
public float visit(DvdElement element) {
18+
return visit(element, 3);
19+
}
20+
21+
public float getTotal() {
22+
return total;
23+
}
24+
25+
private float visit(Element element, float costPerUnitWeight) {
26+
if (element.getPrice() > 20) {
27+
return 0;
28+
}
29+
float cost = element.getWeight() * costPerUnitWeight;
30+
total += cost;
31+
return cost;
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lineate.bench.pattern.visitor.exercise;
2+
3+
public interface Visitor {
4+
float visit(BookElement element);
5+
6+
float visit(CdElement element);
7+
8+
float visit(DvdElement element);
9+
}

0 commit comments

Comments
 (0)