Skip to content

Commit

Permalink
Merge pull request #307 from moksnow/issue297
Browse files Browse the repository at this point in the history
resolving issue 297, create example for java bridge design pattern
  • Loading branch information
ZoranPandovski committed Oct 19, 2019
2 parents cb81c0b + e3cf602 commit 65c30d3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Structural/Bridge/java/BridgeDemo.java
@@ -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();
}
}
3 changes: 3 additions & 0 deletions Structural/Bridge/java/Color.java
@@ -0,0 +1,3 @@
public interface Color {
public void applyColor();
}
6 changes: 6 additions & 0 deletions Structural/Bridge/java/GreenColor.java
@@ -0,0 +1,6 @@
public class GreenColor implements Color{

public void applyColor(){
System.out.println("green.");
}
}
13 changes: 13 additions & 0 deletions Structural/Bridge/java/Pentagon.java
@@ -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();
}

}
6 changes: 6 additions & 0 deletions Structural/Bridge/java/RedColor.java
@@ -0,0 +1,6 @@
public class RedColor implements Color{

public void applyColor(){
System.out.println("red.");
}
}
11 changes: 11 additions & 0 deletions Structural/Bridge/java/Shape.java
@@ -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();
}
13 changes: 13 additions & 0 deletions Structural/Bridge/java/Triangle.java
@@ -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();
}

}

0 comments on commit 65c30d3

Please sign in to comment.