forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
255 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
32 changes: 32 additions & 0 deletions
32
src/visitor/examples/airportsecuritycontrol/InternationalPassenger.java
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,32 @@ | ||
package visitor.examples.airportsecuritycontrol; | ||
|
||
import java.util.List; | ||
|
||
public class InternationalPassenger implements Passenger{ | ||
private String passport; | ||
private Boolean visa; | ||
private List<String> belongings; | ||
|
||
public InternationalPassenger(String passport, Boolean visa, List<String> belongings) { | ||
this.passport = passport; | ||
this.visa = visa; | ||
this.belongings = belongings; | ||
} | ||
|
||
public String getPassport() { | ||
return passport; | ||
} | ||
|
||
public Boolean getVisa() { | ||
return visa; | ||
} | ||
|
||
public List<String> getBelongings() { | ||
return belongings; | ||
} | ||
|
||
@Override | ||
public boolean accept(PoliceOfficer visitor) { | ||
return visitor.visit(this); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/visitor/examples/airportsecuritycontrol/NationalPassenger.java
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,26 @@ | ||
package visitor.examples.airportsecuritycontrol; | ||
|
||
import java.util.List; | ||
|
||
public class NationalPassenger implements Passenger{ | ||
private String identityDocument; | ||
private List<String> belongings; | ||
|
||
public NationalPassenger(String identityDocument, List<String> belongings) { | ||
this.identityDocument = identityDocument; | ||
this.belongings = belongings; | ||
} | ||
|
||
public String getIdentityDocument() { | ||
return identityDocument; | ||
} | ||
|
||
public List<String> getBelongings() { | ||
return belongings; | ||
} | ||
|
||
@Override | ||
public boolean accept(PoliceOfficer visitor) { | ||
return visitor.visit(this); | ||
} | ||
} |
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,5 @@ | ||
package visitor.examples.airportsecuritycontrol; | ||
|
||
public interface Passenger { | ||
boolean accept(PoliceOfficer visitor); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/visitor/examples/airportsecuritycontrol/PoliceOfficer.java
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,31 @@ | ||
package visitor.examples.airportsecuritycontrol; | ||
|
||
import java.util.List; | ||
|
||
public class PoliceOfficer { | ||
|
||
public boolean visit(NationalPassenger passenger) { | ||
boolean checked=false; | ||
if(checkIdentification(passenger.getIdentityDocument()) && checkBelongings(passenger.getBelongings())) { | ||
checked=true; | ||
} | ||
|
||
return checked; | ||
} | ||
|
||
private boolean checkIdentification(String id) { | ||
return id!=null && !id.equals(""); | ||
} | ||
|
||
private boolean checkBelongings(List<String> belongings) { | ||
return !belongings.contains("Liquids"); | ||
} | ||
|
||
public boolean visit(InternationalPassenger passenger) { | ||
boolean checked=false; | ||
if(checkIdentification(passenger.getPassport()) && checkBelongings(passenger.getBelongings()) && passenger.getVisa()) { | ||
checked=true; | ||
} | ||
return checked; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/visitor/examples/airportsecuritycontrol/SecurityControlClient.java
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,25 @@ | ||
package visitor.examples.airportsecuritycontrol; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
public class SecurityControlClient { | ||
|
||
public static void main(String[] args) { | ||
Passenger[] passengers = new Passenger[] {new NationalPassenger("45874005H", Arrays.asList("Coins","Bag","Sun glasses")), | ||
new NationalPassenger(null, Collections.EMPTY_LIST), | ||
new InternationalPassenger("4A4585BC", true, Arrays.asList("Cap","False beard","Gun")), | ||
new InternationalPassenger("11AB8564", false, Arrays.asList("Suitcase","Coat")), | ||
new InternationalPassenger("269LZF74", false, Arrays.asList("Liquids"))}; | ||
|
||
PoliceOfficer officer = new PoliceOfficer(); | ||
|
||
for(Passenger p: passengers) { | ||
if(p.accept(officer)) { | ||
System.out.println("Access granted"); | ||
}else { | ||
System.out.println("Access NOT granted"); | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class Client { | ||
|
||
public static void main(String argv[]) { | ||
// Construcción de una expresión (a+5)*(b+1) | ||
Expression expresion = new Mult( new Sum( new Variable("a"), | ||
new Constant(5) ), | ||
new Sum( new Variable("b"), | ||
new Constant(1) )); | ||
|
||
VisitorExpression expr = new VisitorExpression(); | ||
expresion.aceptar(expr); | ||
|
||
// Visualizacion de resultados | ||
System.out.println("Resultado: " + expr.obtenerResultado()); | ||
} | ||
} |
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 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class Constant extends Expression { | ||
int _valor; | ||
|
||
public Constant(int valor) { | ||
_valor = valor; | ||
} | ||
|
||
public void aceptar(VisitorExpression v) { | ||
v.visitConstant(this); | ||
} | ||
} |
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,5 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public abstract class Expression { | ||
abstract public void aceptar(VisitorExpression v); | ||
} |
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 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class Mult extends OpBinary { | ||
public Mult(Expression izq, Expression der) { | ||
super(izq, der); | ||
} | ||
|
||
public void aceptar(VisitorExpression v) { | ||
v.visitMult(this); | ||
} | ||
} |
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,9 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public abstract class OpBinary extends Expression { | ||
Expression _izq, _der; | ||
|
||
public OpBinary(Expression izq, Expression der) { | ||
_izq = izq; _der = der; | ||
} | ||
} |
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 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class Sum extends OpBinary { | ||
public Sum(Expression izq, Expression der) { | ||
super(izq, der); | ||
} | ||
|
||
public void aceptar(VisitorExpression v) { | ||
v.visitSum(this); | ||
} | ||
} |
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 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class Variable extends Expression { | ||
String _variable; | ||
|
||
public Variable(String variable) { | ||
_variable = variable; | ||
} | ||
|
||
public void aceptar(VisitorExpression v) { | ||
v.visitVariable(this); | ||
} | ||
} |
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,36 @@ | ||
package visitor.examples.arithmetic; | ||
|
||
public class VisitorExpression { | ||
|
||
private String _resultado; | ||
|
||
public void visitVariable(Variable v) { | ||
_resultado = v._variable; | ||
} | ||
|
||
public void visitConstant(Constant c) { | ||
_resultado = String.valueOf(c._valor); | ||
} | ||
|
||
private void visitOpBinary(OpBinary op, String pOperacion) { | ||
op._izq.aceptar(this); | ||
String pIzq = obtenerResultado(); | ||
|
||
op._der.aceptar(this); | ||
String pDer = obtenerResultado(); | ||
|
||
_resultado = "(" + pIzq + pOperacion + pDer + ")"; | ||
} | ||
|
||
public void visitSum(Sum s) { | ||
visitOpBinary(s, "+"); | ||
} | ||
|
||
public void visitMult(Mult m) { | ||
visitOpBinary(m, "*"); | ||
} | ||
|
||
public String obtenerResultado() { | ||
return _resultado; | ||
} | ||
} |