forked from ZoranPandovski/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.java
91 lines (61 loc) · 1.49 KB
/
decorator.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
89
90
91
// The Decorator Design Pattern : The DrawingBoard object is wrapped by
// the DrawHouse,DrawTree,DrawRiver objects.A call to Picdescription()
// function unwraps bottom up from the DrawingBoard to the DrawRiver object.
import java.util.*;
public class DrawingBoard {
public DrawingBoard()
{
}
public String Picdescription()
{
return "This is a picture on the Drawing Board";
}
public static void main(String[] args)
{
DrawingBoard d1 = new DrawingBoard();
d1 = new DrawHouse(d1);
d1 = new DrawTree(d1);
d1 = new DrawRiver(d1);
System.out.println(d1.Picdescription());
}
}
abstract class DrawingBoardAdapter extends DrawingBoard
{
public abstract String Picdescription() ;
}
class DrawTree extends DrawingBoardAdapter
{
DrawingBoard drawingboard;
public DrawTree(DrawingBoard d)
{
drawingboard = d;
}
public String Picdescription()
{
return (drawingboard.Picdescription() + " with a Mangoo Tree");
}
}
class DrawHouse extends DrawingBoardAdapter
{
DrawingBoard drawingboard;
public DrawHouse (DrawingBoard d)
{
drawingboard = d;
}
public String Picdescription()
{
return (drawingboard.Picdescription() + " of a small house");
}
}
class DrawRiver extends DrawingBoardAdapter
{
DrawingBoard drawingboard;
public DrawRiver (DrawingBoard d)
{
drawingboard = d;
}
public String Picdescription()
{
return (drawingboard.Picdescription() + " and flowing river.");
}
}