File tree Expand file tree Collapse file tree 7 files changed +87
-0
lines changed
src/BehavioralPatterns/Visitor Expand file tree Collapse file tree 7 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Visitor ;
2+
3+ public interface Visitable {
4+ void accept (Visitor visitor );
5+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .Visitor ;
2+
3+ public interface Visitor {
4+ void visit (Book book );
5+ void visit (DVD dvd );
6+ }
7+
You can’t perform that action at this time.
0 commit comments