Skip to content

Commit 0dddb33

Browse files
committed
Added Visitor
1 parent 315200e commit 0dddb33

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public class Book implements Visitable{
4+
private double price;
5+
private double weight;
6+
public Book(double price, double weight){
7+
this.price = price;
8+
this.weight = weight;
9+
}
10+
public void accept(Visitor visitor){
11+
visitor.visit(this);
12+
}
13+
public double getPrice() {return price;}
14+
public double getWeight() {return weight;}
15+
}
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public class DVD implements Visitable{
4+
private double price;
5+
private double weight;
6+
public DVD(double price, double weight){
7+
this.price = price;
8+
this.weight = weight;
9+
}
10+
public void accept(Visitor visitor){
11+
visitor.visit(this);
12+
}
13+
public double getPrice() {return price;}
14+
public double getWeight() {return weight;}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
ShoppingCart cart = new ShoppingCart();
6+
cart.addItem(new Book(20, 5));
7+
cart.addItem(new Book(5, 12));
8+
cart.addItem(new DVD(50, 5));
9+
System.out.println(cart.calculatePostage());
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public class PostageVisitor implements Visitor {
4+
private double totalPostageForCart;
5+
public void visit(Book book) {
6+
if(book.getPrice() < 10.0)
7+
totalPostageForCart += book.getWeight() * 2;
8+
}
9+
public void visit(DVD dvd) {
10+
totalPostageForCart += dvd.getWeight() * 2;
11+
}
12+
public double getTotalPostage() {return totalPostageForCart;}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
import java.util.ArrayList;
4+
5+
public class ShoppingCart {
6+
private ArrayList<Visitable> items = new ArrayList<>();
7+
8+
public void addItem(Visitable item) {items.add(item);}
9+
public void removeItem(Visitable item) {items.remove(item);}
10+
11+
public double calculatePostage() {
12+
PostageVisitor visitor = new PostageVisitor();
13+
for(Visitable item: items) {
14+
item.accept(visitor);
15+
}
16+
double postage = visitor.getTotalPostage();
17+
return postage;
18+
}
19+
}
20+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public interface Visitable{
4+
void accept(Visitor visitor);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package BehavioralPatterns.Visitor;
2+
3+
public interface Visitor{
4+
void visit(Book book);
5+
void visit(DVD dvd);
6+
}
7+

0 commit comments

Comments
 (0)