forked from mouredev/roadmap-retos-programacion
-
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.
Merge pull request mouredev#4385 from AmadorQuispe/aqh
mouredev#24 - JAVA
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
ConcreteComponent component = new ConcreteComponent(); | ||
ConcreteDecoratorA componentDecorateA = new ConcreteDecoratorA(component); | ||
ConcreteDecoratorB componentDecorateB = new ConcreteDecoratorB(componentDecorateA); | ||
componentDecorateB.operation(); | ||
System.out.println("EXTRA"); | ||
System.out.println("========================"); | ||
Arithmetic operable = new Arithmetic(); | ||
//Llamada sin el decorador | ||
double result = operable.sumTwoNumbers(5.6,7.8); | ||
System.out.println("Resultado llamada sin decorar :" + result); | ||
//Llamada con decorador | ||
CountCallDecorator operationDecorated = new CountCallDecorator(operable); | ||
result = operationDecorated.sumTwoNumbers(2d,4d); | ||
System.out.println("Resultado llamada con decorador :" + result); | ||
result = operationDecorated.sumTwoNumbers(4.5,6.7); | ||
System.out.println("Resultado llamada con decorador :" + result); | ||
} | ||
|
||
} | ||
//Classes Generic | ||
interface Component{ | ||
void operation(); | ||
} | ||
class ConcreteComponent implements Component{ | ||
@Override | ||
public void operation() { | ||
System.out.println("Concrete Component Operation"); | ||
} | ||
} | ||
abstract class Decorator implements Component{ | ||
protected Component component; | ||
public Decorator(Component component){ | ||
this.component = component; | ||
} | ||
@Override | ||
public void operation() { | ||
component.operation(); | ||
} | ||
} | ||
class ConcreteDecoratorA extends Decorator{ | ||
public ConcreteDecoratorA(Component component) { | ||
super(component); | ||
} | ||
protected void otherOperation(){ | ||
System.out.println("Concrete Decorator A"); | ||
} | ||
|
||
@Override | ||
public void operation() { | ||
otherOperation(); | ||
super.operation(); | ||
} | ||
} | ||
class ConcreteDecoratorB extends Decorator{ | ||
public ConcreteDecoratorB(Component component) { | ||
super(component); | ||
} | ||
protected void otherOperation(){ | ||
System.out.println("Concrete Decorator B"); | ||
} | ||
|
||
@Override | ||
public void operation() { | ||
otherOperation(); | ||
super.operation(); | ||
} | ||
} | ||
|
||
//EXTRA | ||
interface Operable{ | ||
double sumTwoNumbers(Double num1, Double num2); | ||
} | ||
class Arithmetic implements Operable { | ||
@Override | ||
public double sumTwoNumbers(Double num1, Double num2) { | ||
return num1 + num2; | ||
} | ||
} | ||
abstract class OperableDecorator implements Operable{ | ||
protected Operable operable; | ||
OperableDecorator(Operable operable){ | ||
this.operable = operable; | ||
} | ||
} | ||
|
||
class CountCallDecorator extends OperableDecorator{ | ||
private static int countCall = 0; | ||
public CountCallDecorator(Operable operable) { | ||
super(operable); | ||
} | ||
|
||
|
||
@Override | ||
public double sumTwoNumbers(Double num1, Double num2) { | ||
countCall++; | ||
System.out.println("La función se ha llamado : " + countCall + (countCall>1?" veces":" vez")); | ||
return operable.sumTwoNumbers(num1,num2); | ||
} | ||
} |