How to apply Strategy Pattern using Spring?
Hello,
I have the following situation (this example is pretty much like the one in the book "Head First Design Pattern"):
//Interface Behavior
interface BehaviorOne {
void credit()
}
//Interface Implementation
class BehaviorOneImpl implements BehaviorOne{
void credit () {
System.out.println("do something");
}
}
//class to use Behavior
class A {
double amount;
BehaviorOne behaviorOne;
executeCredit() {
behaviorOne.credit();
}
void setAmount(double amount) {
this.amount = amount;
}
double getAmount() {
return amount;
}
}
//class extends abstract class
class Aconcrete {
Aconcrete() {
behaviorOne = new BehaviorOneImpl();
}
}
//Client call
class Test {
public static void main(String[] args) {
A _aConcrete = new Aconcrete();
_aConcrete.executeCredit();
}
}
The result should be (I didn't test but this example should works):
"do something"
My problems:
------------
1st:
If in the BehaviorOneImpl class I need an information that was loaded in the A class, how do I get it? I have to do a composition like this:
class BehaviorOneImpl {
A _aMainClass = new A()
void credit () {
System.out.println("do something "+
"show amount = "+ _aMainClass.getAmount);
}
}
2nd:
How could I integrate the Strategy using Spring?
Thank you so much in advance for any help!