Guest User

Untitled

a guest
Oct 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4.  
  5. interface Freight {
  6.  
  7. void accept(FreightVisitor visitor);
  8. }
  9.  
  10. interface FreightVisitor {
  11.  
  12. default void visit(Freight freight) {
  13. }
  14.  
  15. default void visit(FixedFreight freight) {
  16. this.visit((Freight) freight);
  17. }
  18.  
  19. default void visit(ByPriceFreight freight) {
  20. this.visit((Freight) freight);
  21. }
  22.  
  23. default void visit(ByWeightFreight freight) {
  24. this.visit((Freight) freight);
  25. }
  26. }
  27.  
  28. @Data
  29. class FixedFreight implements Freight {
  30.  
  31. int freight;
  32.  
  33. @Override
  34. public void accept(FreightVisitor visitor) {
  35. visitor.visit(this);
  36. }
  37. }
  38.  
  39. @Data
  40. class ByPriceFreight implements Freight {
  41.  
  42. int start = 0;
  43. int startLimit = 0;
  44. int priceUnit;
  45. int freightPerUnit;
  46.  
  47. @Override
  48. public void accept(FreightVisitor visitor) {
  49. visitor.visit(this);
  50. }
  51. }
  52.  
  53. @Data
  54. @AllArgsConstructor
  55. @NoArgsConstructor
  56. class ByWeightFreight implements Freight {
  57.  
  58. int start = 0;
  59. int startLimit = 0;
  60. int weightLimit;
  61. int freightPerUnit;
  62.  
  63. @Override
  64. public void accept(FreightVisitor visitor) {
  65. visitor.visit(this);
  66. }
  67. }
  68.  
  69.  
  70. @Data
  71. class ByWeightCalculateVisitor implements FreightVisitor {
  72.  
  73. int weight;
  74. int freight = 0;
  75.  
  76. ByWeightCalculateVisitor(int weight) {
  77. this.weight = weight;
  78. }
  79.  
  80. @Override
  81. public void visit(ByWeightFreight freight) {
  82. this.freight = // do the calculation
  83. }
  84. }
  85.  
  86.  
  87. public class App {
  88.  
  89. public static void main(String[] args) {
  90. ByWeightCalculateVisitor visitor = new ByWeightCalculateVisitor(10);
  91. ByWeightFreight byWeightFreight = new ByWeightFreight(0, 0, 10, 10);
  92. byWeightFreight.accept(visitor);
  93.  
  94. System.out.println(visitor.getFreight());
  95. }
  96. }
Add Comment
Please, Sign In to add comment