Results 1 to 6 of 6

Thread: How to apply Strategy Pattern using Spring?

  1. #1

    Default 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!

  2. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    This was discussed in the forums before.
    For example, http://forum.springframework.org/sho...rategy+pattern

  3. #3
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    1) I would recommend you change your BehaviorOne interface so that credit would take an argument. It really isn't a good practice to have circular dependencies like you describe. I
    Bill

  4. #4

    Default

    Guys, to put it simple.. thanks for save my life!
    Last edited by DistillingSpring; Feb 22nd, 2007 at 03:48 AM.

  5. #5

    Default

    I would like to take advantage of this thread to make sure that's correct a design that I implemented:

    Let's say that for BehaviorOneImpl I have two other behaviors and two other concrete classes to access those behaviors. So,
    I applied the Strategy pattern once again for BehaviorOneImpl and I have the following:

    Code:
    //Previous implementation "First Level Behavior"---------------------
    ...
    //Interface Implementation
    class BehaviorOneImpl implements BehaviorOne{
    void credit () {
    System.out.println("do something");
    }
    ..
    //------------------------------------------
    
    //classes
    class BehaviorOneImplClassOne extends BehaviorOneImpl {
    ...
    }
    
    class BehaviorOneImplClassTwo extends BehaviorOneImpl {
    ...
    }
    
    //interfaces
    interface BehaviorOneImplClassOneInterface {
    ...
    }
    
    interface BehaviorOneImplClassTwoInterface {
    ...
    }
    
    
    //Implementations concrete class "Second Level Behavior"
    class SecondLevelBehaviorClassOne implements BehaviorOneImplClassOneInterface {
    ...
    }
    
    class SecondLevelBehaviorClassTwo implements BehaviorOneImplClassTwoInterface {
    ...
    }
    
    
    //Since SecondLevelBehaviorClassOne has many subclasses, I created an interface and in this way it's possible to access the interface 
    //instead using extends (concrete classes).
    interface ISecondLevelBehaviorClassOneInterface  {
       public SecondLevelBehaviorClassOne getInformation(SecondLevelBehaviorClassOne secondLevelBehaviorClassOne);
    }
    
    
    //And finally, the implementation of the different classes. As you can see,  the goal here is the WhateverItIsOne access the interface
    //ISecondLevelBehaviorClassOneInterface instead access the class SecondLevelBehaviorClassOne directly.
    class WhateverItIsOne implements ISecondLevelBehaviorClassOneInterface{
    	public SecondLevelBehaviorClassOne getInformation(SecondLevelBehaviorClassOne secondLevelBehaviorClassOne) {
                  ....
    	}
    }
    
    class WhateverItIsTwo implements ISecondLevelBehaviorClassOneInterface{
    	public SecondLevelBehaviorClassOne getInformation(SecondLevelBehaviorClassOne secondLevelBehaviorClassOne) {
                  ....
    	}
    }
    
    
    class WhateverItIsThree implements ISecondLevelBehaviorClassOneInterface{
    	public SecondLevelBehaviorClassOne getInformation(SecondLevelBehaviorClassOne secondLevelBehaviorClassOne) {
                  ....
    	}
    }
    
    //And the same for the BehaviorOneImplClassTwoInterface.
    Note that my class BehaviorOneImpl represents the very same "position" as the class A in the above example.

    I don't want you to do my job, but if you have a better idea or agree that it's ok, would be great.

    Regards.
    Last edited by DistillingSpring; Feb 22nd, 2007 at 04:36 AM.

  6. #6

    Default

    Hello,

    I am still having some doubts regarding the last model that I sent. Would anyone mind to take a look on that? If you want, I can send a more detailled and functional source code.

    Thanks for any help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •