-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from moksnow/issue297
resolving issue 297, create example for java bridge design pattern
- Loading branch information
Showing
7 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
|
||
/** | ||
* Created by trustgeek on 10/17/2017 {11:33 PM}. | ||
* Just like Adapter pattern, bridge design pattern is one of the Structural design pattern. | ||
* According to GoF bridge design pattern is: | ||
* Decouple an abstraction from its implementation so that the two can vary independently | ||
*/ | ||
public class BridgeDemo { | ||
public static void main(String[] args) { | ||
Shape tri = new Triangle(new RedColor()); | ||
tri.applyColor(); | ||
|
||
|
||
Shape pent = new Pentagon(new GreenColor()); | ||
pent.applyColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface Color { | ||
public void applyColor(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class GreenColor implements Color{ | ||
|
||
public void applyColor(){ | ||
System.out.println("green."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Pentagon extends Shape{ | ||
|
||
Pentagon(Color c) { | ||
super(c); | ||
} | ||
|
||
@Override | ||
public void applyColor() { | ||
System.out.print("Pentagon filled with color "); | ||
color.applyColor(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class RedColor implements Color{ | ||
|
||
public void applyColor(){ | ||
System.out.println("red."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public abstract class Shape { | ||
//Composition - implementor | ||
Color color; | ||
|
||
//constructor with implementor as input argument | ||
Shape(Color c) { | ||
this.color = c; | ||
} | ||
|
||
abstract public void applyColor(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Triangle extends Shape{ | ||
|
||
Triangle(Color c) { | ||
super(c); | ||
} | ||
|
||
@Override | ||
public void applyColor() { | ||
System.out.print("Triangle filled with color "); | ||
color.applyColor(); | ||
} | ||
|
||
} |