Skip to content

Commit

Permalink
Add Bridge and Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 24, 2020
1 parent 7a4955f commit a95db58
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Project that implements some design patterns using Java 13.
- Flyweight
- Memento
- Interpreter
- Visitor
- Visitor
- Bridges and Adapters
109 changes: 109 additions & 0 deletions src/bridgesAndAdapters/Annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
###############################
# Bridge #
###############################



##### Language - EN

- We have, for example, an external system, like a Google Maps, I need to make my program reach this price, we use a "BRIDGE"
to get to him;

- It is a "bridge" of one program and another;

- He is very similar to other standards, but we have to understand his 'MOTIVATION', which in this case is:
- >>> I need a way to get to the other system, I need to get to a place using this 'bridge'

- When you create an interface that the concrete implementation takes you to another system, you are creating a "BRIDGE"


##### Language - PT-BR

- Temos por exemplo um sistema externo, como um Google Maps, preciso fazer com que o meu programa chegue até esse caro, usamos uma "PONTE"
para chegar até ele;

- É uma "ponte" de um programa e outro;

- Ele é bem parecido com outros padrões, porém temos que entender a 'MOTIVAÇÃO' dele, que nesse caso é:
->>> Eu preciso de um caminho para chegar no outro sistema, preciso chegar em um lugar utilizando essa 'ponte'

- Quando você cria uma interface que a implementação concreta te leva para outro sistema, você está criando uma "BRIDGE"





###############################
# Adapter #
###############################



##### Language - EN

- When I have an interface that hides legacy code, it 'ADAPTS' the legacy system to my new world, we call it 'ADAPTER'

- It hides the complexity of dealing with a legacy API;

- His implementation is the same as 'Bridge', what changes is 'Motivation', when to use one, when to use the other;

- 'Adapter' it adapts an old interface to new ones, different from the 'Bridge' which is a BRIDGE.

- The Adapter is very useful when we are rewriting a legacy system, for a new system.


##### Language - PT-BR

- Quando eu tenho uma interface que esconde um código legado, ela 'ADAPTA' o sistema legado ao meu novo mundo, nós chamamos de 'ADAPTER'

- Ele esconde a complexidade de lidar com uma API legada;

- A implementação dele é igual a do 'Bridge', o que muda é a 'Motivação', quando usar um, quando usar o outro;

- 'Adapter' ele adapta uma interface antiga a interface novas, diferente da 'Bridge' que é uma PONTE.

- O Adapter é muito útil quando estamos reescrevendo um sistema legado, para um sistema novo.






###############################
# Bridge X Adapter #
###############################



##### Language - EN

- In terms of implementation, none. In both, we create an interface and create an implementation underneath.

- The difference is semantic. The idea of Bridge is precisely to be a bridge in two worlds / systems.

- The idea of the Adapter is to hide some "dirt", or adapt something that is different and does not match the current system.

- It is quite common that the Adapter interface has already been pre-defined and already exists in the system.
In this situation, the programmer goes there to implement an "adapter" for the old system, in order to fit the existing model.


##### Language - PT-BR

- Em termos de implementação, nenhuma. Em ambas, criamos uma interface e criamos uma implementação por baixo.

- A diferença é semântica. A ideia da Bridge é justamente ser uma ponte em dois mundos/sistemas.

- A ideia do Adapter é esconder alguma "sujeira", ou adaptar algo que é diferente e não bate com o sistema atual.

- É bem comum inclusive que a interface do Adapter já tenha sido pré-definida e já até exista no sistema.
Nessa situação, o programador vai lá para implementar um "adaptador" para o sistema antigo, de forma a caber no modelo já existente.










16 changes: 16 additions & 0 deletions src/bridgesAndAdapters/adapter/AdapterMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bridgesAndAdapters.adapter;

public class AdapterMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test Adapter");


IClock clock = new ClockOfSystem();//it hides the complexity of dealing with a legacy API.
System.out.println("Date Today: " + clock.today());


System.out.println("--------------------------------------------- End Test Adapter");
}

}
12 changes: 12 additions & 0 deletions src/bridgesAndAdapters/adapter/ClockOfSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bridgesAndAdapters.adapter;

import java.time.LocalDate;

public class ClockOfSystem implements IClock {

@Override
public LocalDate today() {
return LocalDate.now();
}

}
9 changes: 9 additions & 0 deletions src/bridgesAndAdapters/adapter/IClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bridgesAndAdapters.adapter;

import java.time.LocalDate;

public interface IClock {

public LocalDate today();

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

public class BridgeMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test Bridge");


IMap map = new GoogleMap();//Class 'GoogleMap' hides the complexity of getting on google maps
map.returnMap("Rua Sérgio Ruiz de Albuquerque");


System.out.println("--------------------------------------------- End Test Bridge");
}

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

import java.io.InputStream;
import java.net.URL;

public class GoogleMap implements IMap {

@Override
public String returnMap(String street) {
try {
String googleMaps = "https://www.google.com/maps/place/Reading/";
URL url = new URL(googleMaps);
InputStream openStrem = url.openStream();
// ....
return "map..";
} catch (Exception e) {
throw new RuntimeException(e);
}
}

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

public interface IMap {

public String returnMap(String street);

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

public class LinkMap implements IMap{

@Override
public String returnMap(String street) {
return "Link Map...";
}

}

0 comments on commit a95db58

Please sign in to comment.