File tree Expand file tree Collapse file tree 8 files changed +112
-3
lines changed Expand file tree Collapse file tree 8 files changed +112
-3
lines changed Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public class CountDisplay implements Displaible {
4
+ private final Display display ;
5
+
6
+ public CountDisplay (DisplayImpl impl ) {
7
+ this .display = new Display (impl );
8
+ }
9
+
10
+ public void display () {
11
+ display .display ();
12
+ }
13
+
14
+ public void multiDisplay (int times ) {
15
+ display .open ();
16
+ for (int i = 0 ; i < times ; i ++) {
17
+ display .print ();
18
+ }
19
+ display .close ();
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public interface Displaible {
4
+ void display ();
5
+ }
Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public class Display implements Displaible {
4
+ private final DisplayImpl impl ;
5
+
6
+ public Display (DisplayImpl impl ) {
7
+ this .impl = impl ;
8
+ }
9
+
10
+ public void open () {
11
+ impl .rawOpen ();
12
+ }
13
+
14
+ public void print () {
15
+ impl .rawPrint ();
16
+ }
17
+
18
+ public void close () {
19
+ impl .rawClose ();
20
+ }
21
+
22
+ public final void display () {
23
+ open ();
24
+ print ();
25
+ close ();
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public interface DisplayImpl {
4
+ void rawOpen ();
5
+
6
+ void rawPrint ();
7
+
8
+ void rawClose ();
9
+ }
Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public class Main {
4
+ public static void main (String [] args ) {
5
+ Displaible d1 = new Display (new StringDisplayImpl ("Hello, Japan." ));
6
+ Displaible d2 = new CountDisplay (new StringDisplayImpl ("Hello, World." ));
7
+ CountDisplay d3 = new CountDisplay (new StringDisplayImpl ("Hello, Universe." ));
8
+ d1 .display ();
9
+ d2 .display ();
10
+ d3 .display ();
11
+ d3 .multiDisplay (5 );
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ package j .bridge .delegate ;
2
+
3
+ public class StringDisplayImpl implements DisplayImpl {
4
+ private final String string ;
5
+ private final int width ;
6
+
7
+ public StringDisplayImpl (String string ) {
8
+ this .string = string ;
9
+ this .width = string .length ();
10
+ }
11
+
12
+ @ Override
13
+ public void rawOpen () {
14
+ printLine ();
15
+ }
16
+
17
+ @ Override
18
+ public void rawPrint () {
19
+ System .out .println ("|" + string + "|" );
20
+ }
21
+
22
+ @ Override
23
+ public void rawClose () {
24
+ printLine ();
25
+ }
26
+
27
+ private void printLine () {
28
+ System .out .print ("+" );
29
+ for (int i = 0 ; i < width ; i ++) {
30
+ System .out .print ("-" );
31
+ }
32
+ System .out .println ("+" );
33
+ }
34
+ }
Original file line number Diff line number Diff line change 1
1
package j .bridge .inheritance ;
2
2
3
3
public class Display {
4
- private DisplayImpl impl ;
4
+ private final DisplayImpl impl ;
5
5
6
6
public Display (DisplayImpl impl ) {
7
7
this .impl = impl ;
Original file line number Diff line number Diff line change 1
1
package j .bridge .inheritance ;
2
2
3
3
public class StringDisplayImpl extends DisplayImpl {
4
- private String string ;
5
- private int width ;
4
+ private final String string ;
5
+ private final int width ;
6
6
7
7
public StringDisplayImpl (String string ) {
8
8
this .string = string ;
You can’t perform that action at this time.
0 commit comments