Announcement Announcement Module
Collapse
No announcement yet.
How to apply Strategy Pattern using Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    This was discussed in the forums before.
    For example, http://forum.springframework.org/sho...rategy+pattern

    Comment


    • #3
      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

      Comment


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

        Comment


        • #5
          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, 05:36 AM.

          Comment


          • #6
            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.

            Comment

            Working...
            X