-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecoratorExample.java
88 lines (74 loc) · 2.37 KB
/
DecoratorExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.javaexperiments;
/**
* An illustration of decorator design pattern
*/
public class DecoratorExample {
public static void main(String args[]) {
/**
* Creating new Margherita pizza
*/
Pizza margheritaPizza = new Margherita();
System.out.println(margheritaPizza.getDescription()
+ " Cost :$" + margheritaPizza.getCost());
/**
* Creating new FarmHouse pizza
*/
Pizza farmhousePizza = new FarmHouse();
/**
* Decorating with FreshTomato topping
*/
farmhousePizza = new FreshTomato(farmhousePizza);
System.out.println(farmhousePizza.getDescription()
+ " Cost :$" + farmhousePizza.getCost());
Pizza cheeseburstPizza = new CheeseBurst(margheritaPizza);
System.out.println(cheeseburstPizza.getDescription()
+ " Cost :$" + cheeseburstPizza.getCost());
}
}
/**
* Abstract pizza class
*/
abstract class Pizza {
String description = "";
public String getDescription() {
return description;
}
public abstract double getCost();
}
/**
* Concrete classes for abstract Pizza class where the pizza types are different
*/
class FarmHouse extends Pizza {
public FarmHouse() { description = "FarmHouse"; }
public double getCost() { return 200.00; }
}
class Margherita extends Pizza {
public Margherita() { description = "Margherita"; }
public double getCost() { return 100.00; }
}
/**
* Toppings is the decorator abstract class here that extends abstract Pizza class where
* the toppings like CheeseBurst, and FreshTomato are different
*/
abstract class Toppings extends Pizza {
public abstract String getDescription();
}
/**
* Decorator concrete classes extending abstract Toppings class
*/
class CheeseBurst extends Toppings {
Pizza pizza;
public CheeseBurst(Pizza pizza) { this.pizza = pizza; }
public String getDescription() {
return pizza.getDescription() + " with Cheese Burst topping ";
}
public double getCost() { return 50.00 + pizza.getCost(); }
}
class FreshTomato extends Toppings {
Pizza pizza;
public FreshTomato(Pizza pizza) { this.pizza = pizza; }
public String getDescription() {
return pizza.getDescription() + " with Fresh Tomato topping ";
}
public double getCost() { return 35.00 + pizza.getCost(); }
}