-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeTest.java
More file actions
89 lines (70 loc) · 2.14 KB
/
BridgeTest.java
File metadata and controls
89 lines (70 loc) · 2.14 KB
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
/*
A Structural Pattern.
The Bridge is intended to decouple an abstraction from its implementation
so both can vary independently. For example, a significant maintenance headache
entails the coupling that occurs between custom classes and the class libraries they use.
Bridges are useful for minimizing this coupling by providing an abstract description
of the class libraries to the client.
*/
import java.util.StringTokenizer;
public class BridgeTest {
public static void main(String[] args){
System.out.println("-------------- BRIDGE ---------------");
View view = setupAbstraction("Twinkle;Twinkle;little;star;How;I;wonder;what;you;are;");
view.display();
}
public static View setupAbstraction(String data){
View view = new RefinedView(data);
// ViewImplementor impl = new ColumnWiseView();
ViewImplementor impl = new LineWiseView();
view.setImplementor(impl);
return view;
}
}
// abstraction
abstract class View {
ViewImplementor implementor = null;
public ViewImplementor getImplementor(){
return implementor;
}
public void setImplementor(ViewImplementor impl){
implementor = impl;
}
public abstract void display();
}
// concrete abstraction
class RefinedView extends View {
String str = null;
RefinedView(String str){
this.str = str;
}
public void display(){
implementor.implementView(str);
}
}
// Implementor interface
abstract class ViewImplementor {
public abstract void implementView(String str);
}
// concrete implementator 1
class ColumnWiseView extends ViewImplementor {
public void implementView(String str){
StringTokenizer tokens = new StringTokenizer(str, ";");
System.out.println("ColumnWiseViewer: ");
while (tokens.hasMoreTokens())
{
System.out.println(tokens.nextToken());
}
}
}
// concrete implementator 2
class LineWiseView extends ViewImplementor {
public void implementView (String str){
StringTokenizer tokens = new StringTokenizer(str, ";");
System.out.println("LineWiseViewer: ");
while (tokens.hasMoreTokens())
{
System.out.print(tokens.nextToken()+" ");
}
}
}