Skip to content

Commit

Permalink
Add Template Method pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
peterm85 committed Nov 22, 2019
1 parent b7c7515 commit 1c573a2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Forma en que las clases u objetos interaccionan y distribuyen funcionalidades.
* [Chain of Responsability](#chain) [**O**]
* [Memento](#memento) [**O**]
* [Mediator](#mediator) [**O**]
* [Templete method] [**C**]
* [Template method](#template) [**C**]
* [Iterator] [**O**]
* [Visitor] [**O**]
* [State] [**O**]
Expand Down Expand Up @@ -387,3 +387,19 @@ La comunicación entre objetos es encapsulada con un objeto mediador. Los objeto
* [Air Traffic Controller](https://github.com/peterm85/design-patterns/tree/master/src/mediator/examples/airtrafficcontroller)

**Referencia:** [https://www.geeksforgeeks.org/mediator-design-pattern/](https://www.geeksforgeeks.org/mediator-design-pattern/)

## <a name="template">Template Method</a> [&#8593;](#lista-de-patrones)

**Propósito:** Define el esqueleto de programa de un algoritmo en un método, llamado método de plantilla, el cual difiere algunos pasos a las subclases.

Permite redefinir ciertos pasos seguros de un algoritmo sin cambiar la estructura del algoritmo.

**Aplicación:** Usamos el patrón Template Method cuando ...
* Se desea dejar que las subclases que se implementan (a través del método primordial) tengan un comportamiento que puede variar.
* Evitar duplicación en el código: la estructura general de flujo de trabajo, está implementada una vez en el algoritmo de clase abstracta, y variaciones necesarias son implementadas en cada de las subclases.
* Controlar en qué punto(s) la subclassing está permitida. En oposición a una sencilla sobrecarga polimórfica, donde el método de base sería enteramente reescrito, permitiendo un cambio radical en el flujo. Sólo los detalles específicos del flujo se pueden cambiar.

**Ejemplos:**
* [Order Managing](https://github.com/peterm85/design-patterns/tree/master/src/template/examples/ordermanaging)

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

public class Client {

public static void main(String[] args) {
OrderProcessTemplate netOrder = new NetOrder();
netOrder.processOrder(true);
System.out.println();
OrderProcessTemplate storeOrder = new StoreOrder();
storeOrder.processOrder(true);
}
}
21 changes: 21 additions & 0 deletions src/template/examples/ordermanaging/NetOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package template.examples.ordermanaging;

public class NetOrder extends OrderProcessTemplate {
@Override
public void doSelect() {
System.out.println("Item added to online shopping cart");
System.out.println("Get gift wrap preference");
System.out.println("Get delivery address.");
}

@Override
public void doPayment() {
System.out.println("Online Payment through Netbanking, card or Paytm");
}

@Override
public void doDelivery() {
System.out.println("Ship the item through post to delivery address");
}

}
29 changes: 29 additions & 0 deletions src/template/examples/ordermanaging/OrderProcessTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package template.examples.ordermanaging;

public abstract class OrderProcessTemplate {
public boolean isGift;

public abstract void doSelect();

public abstract void doPayment();

public abstract void doDelivery();

public final void processOrder(boolean isGift) {
doSelect();
doPayment();
if (isGift) {
giftWrap();
}
doDelivery();
}

public final void giftWrap() {
try {
System.out.println("Gift wrap successfull");
}
catch (Exception e) {
System.out.println("Gift wrap unsuccessful");
}
}
}
19 changes: 19 additions & 0 deletions src/template/examples/ordermanaging/StoreOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package template.examples.ordermanaging;

public class StoreOrder extends OrderProcessTemplate {

@Override
public void doSelect() {
System.out.println("Customer chooses the item from shelf.");
}

@Override
public void doPayment() {
System.out.println("Pays at counter through cash/POS");
}

@Override
public void doDelivery() {
System.out.println("Item deliverd to in delivery counter.");
}
}

0 comments on commit 1c573a2

Please sign in to comment.