Skip to content

Commit

Permalink
Add Bridge pattern and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
peterm85 committed Nov 21, 2019
1 parent 192a940 commit 3ae21e1
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Un **Proxy** o sustituto:
* Usar un nivel extra de indirección para permitir el acceso distribuido, controlado e inteligente.
* Agregar un *"wrapper"* para proteger el componente real de la complejidad innecesaria. Este *wrapper* permite agregar funcionalidad al objeto de interés sin cambiar el código del objeto.

Aunque tenga similitudes al patrón Facade, ambos son diferentes.
Aunque a simple vista tenga similitudes al patrón Facade, ambos tiene matices diferentes.
* Mientras que los objetos Proxy representan a un objeto, los objetos Facade representan a un subsistema de objetos.
* Un objeto cliente Proxy no puede acceder a un objeto interno directamente, mientras que Facade no lo impide.
* Un objeto Proxy provee control de acceso a un único objeto de interés. Sin embargo, un objeto Facade provee una interface de alto nivel a un subsistema de objetos.
Expand Down Expand Up @@ -308,3 +308,23 @@ En algunos casos los recursos pueden ser limitados por lo que se especifica un n
* *Share resources*: Diferentes clientes necesitarán el mismo recurso en diferentes momentos.

**Referencia:** [https://www.javatpoint.com/object-pool-pattern](https://www.javatpoint.com/object-pool-pattern)

## <a name="bridge">Bridge</a> [&#8593;](#lista-de-patrones)

**Propósito:** Permite desacoplar una abstracción de su implementación, de manera que ambas puedan ser modificadas independientemente sin necesidad de alterar por ello la otra.

**Aplicación:** Usamos el patrón [Bridge](https://github.com/peterm85/design-patterns/tree/master/src/bridge/pattern) cuando...
* Se desea evitar un enlace permanente entre la abstracción y su implementación. Esto puede ser debido a que la implementación debe ser seleccionada o cambiada en tiempo de ejecución.
* Tanto las abstracciones como sus implementaciones deben ser extensibles por medio de subclases. En este caso, el patrón Bridge permite combinar abstracciones e implementaciones diferentes y extenderlas independientemente.
* Cambios en la implementación de una abstracción no deben impactar en los clientes, es decir, su código no debe tener que ser recompilado.
* Se desea compartir una implementación entre múltiples objetos (quizá usando contadores), y este hecho debe ser escondido a los clientes.

**Usos típicos:**
* *Desacoplar* interfaz e implementación.
* Mejorar la *extensibilidad*.
* Esconder los detalles de la implementación a los clientes.

**Ejemplos:**
* [Taller](https://github.com/peterm85/design-patterns/tree/master/src/bridge/examples/workshop)

**Referencia:** [https://www.geeksforgeeks.org/bridge-design-pattern/](https://www.geeksforgeeks.org/bridge-design-pattern/)
10 changes: 10 additions & 0 deletions src/bridge/examples/workshop/Assemble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bridge.examples.workshop;

public class Assemble implements Workshop {

@Override
public void work() {
System.out.print(" And");
System.out.println(" Assembled.");
}
}
15 changes: 15 additions & 0 deletions src/bridge/examples/workshop/Bike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bridge.examples.workshop;

public class Bike extends Vehicle {

public Bike(Workshop workShop1, Workshop workShop2) {
super(workShop1, workShop2);
}

@Override
public void manufacture() {
System.out.print("Bike ");
workShop1.work();
workShop2.work();
}
}
15 changes: 15 additions & 0 deletions src/bridge/examples/workshop/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bridge.examples.workshop;

public class Car extends Vehicle {

public Car(Workshop workShop1, Workshop workShop2){
super(workShop1, workShop2);
}

@Override
public void manufacture() {
System.out.print("Car ");
workShop1.work();
workShop2.work();
}
}
11 changes: 11 additions & 0 deletions src/bridge/examples/workshop/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bridge.examples.workshop;

public class Client {

public static void main(String[] args) {
Vehicle vehicle1 = new Car(new Produce(), new Assemble());
vehicle1.manufacture();
Vehicle vehicle2 = new Bike(new Produce(), new Assemble());
vehicle2.manufacture();
}
}
9 changes: 9 additions & 0 deletions src/bridge/examples/workshop/Produce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bridge.examples.workshop;

public class Produce implements Workshop {

@Override
public void work() {
System.out.print("Produced");
}
}
14 changes: 14 additions & 0 deletions src/bridge/examples/workshop/Vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bridge.examples.workshop;

public abstract class Vehicle {

protected Workshop workShop1;
protected Workshop workShop2;

protected Vehicle(Workshop workShop1, Workshop workShop2) {
this.workShop1 = workShop1;
this.workShop2 = workShop2;
}

abstract public void manufacture();
}
5 changes: 5 additions & 0 deletions src/bridge/examples/workshop/Workshop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bridge.examples.workshop;

public interface Workshop {
abstract public void work();
}
5 changes: 5 additions & 0 deletions src/bridge/pattern/Abstraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bridge.pattern;

public interface Abstraction {
void operacion();
}
15 changes: 15 additions & 0 deletions src/bridge/pattern/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bridge.pattern;

public class Client {

public static void main(String[] args) {

Abstraction[] abstracciones = new Abstraction[2];
abstracciones[0] = new RefinedAbstraction(new ImplementorA());
abstracciones[1] = new RefinedAbstraction(new ImplementorB());

for(Abstraction abstraccion:abstracciones) {
abstraccion.operacion();
}
}
}
5 changes: 5 additions & 0 deletions src/bridge/pattern/Implementor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bridge.pattern;

public interface Implementor {
void operacion();
}
8 changes: 8 additions & 0 deletions src/bridge/pattern/ImplementorA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bridge.pattern;

public class ImplementorA implements Implementor{

public void operacion() {
System.out.println("Esta es la implementacion A");
}
}
9 changes: 9 additions & 0 deletions src/bridge/pattern/ImplementorB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bridge.pattern;

public class ImplementorB implements Implementor{

public void operacion() {
System.out.println("Esta es la implementacion B");
}

}
14 changes: 14 additions & 0 deletions src/bridge/pattern/RefinedAbstraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bridge.pattern;

public class RefinedAbstraction implements Abstraction{

private Implementor implementador;

public RefinedAbstraction(Implementor implementador){
this.implementador = implementador;
}
public void operacion(){
implementador.operacion();
}

}

0 comments on commit 3ae21e1

Please sign in to comment.