Strategy Pattern : strategy with the caller as attribute? [duplicate]












-1
















This question already has an answer here:




  • In the strategy pattern can the strategy take the Context as parameter

    3 answers




I use a Strategy pattern, so I have my Strategy abstract class, my ConcreteStrategy class and my Player class. In my ConcreteStrategy I must access my Player object.



Knowing that Player has a Strategy and that, as my teacher told me, I must not include Player in Strategy class, how can I access Player without including it in Strategy?










share|improve this question















marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

    – f1sh
    Nov 14 '18 at 15:45













  • Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

    – janardhan sharma
    Nov 14 '18 at 16:07











  • @f1sh, please don't answer questions in comments. Add an answer or answers below.

    – jaco0646
    Nov 14 '18 at 18:09
















-1
















This question already has an answer here:




  • In the strategy pattern can the strategy take the Context as parameter

    3 answers




I use a Strategy pattern, so I have my Strategy abstract class, my ConcreteStrategy class and my Player class. In my ConcreteStrategy I must access my Player object.



Knowing that Player has a Strategy and that, as my teacher told me, I must not include Player in Strategy class, how can I access Player without including it in Strategy?










share|improve this question















marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

    – f1sh
    Nov 14 '18 at 15:45













  • Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

    – janardhan sharma
    Nov 14 '18 at 16:07











  • @f1sh, please don't answer questions in comments. Add an answer or answers below.

    – jaco0646
    Nov 14 '18 at 18:09














-1












-1








-1


2







This question already has an answer here:




  • In the strategy pattern can the strategy take the Context as parameter

    3 answers




I use a Strategy pattern, so I have my Strategy abstract class, my ConcreteStrategy class and my Player class. In my ConcreteStrategy I must access my Player object.



Knowing that Player has a Strategy and that, as my teacher told me, I must not include Player in Strategy class, how can I access Player without including it in Strategy?










share|improve this question

















This question already has an answer here:




  • In the strategy pattern can the strategy take the Context as parameter

    3 answers




I use a Strategy pattern, so I have my Strategy abstract class, my ConcreteStrategy class and my Player class. In my ConcreteStrategy I must access my Player object.



Knowing that Player has a Strategy and that, as my teacher told me, I must not include Player in Strategy class, how can I access Player without including it in Strategy?





This question already has an answer here:




  • In the strategy pattern can the strategy take the Context as parameter

    3 answers








java design-patterns circular-dependency strategy-pattern






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:02









jaco0646

5,08952846




5,08952846










asked Nov 14 '18 at 15:40









Kevin VKevin V

347




347




marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

    – f1sh
    Nov 14 '18 at 15:45













  • Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

    – janardhan sharma
    Nov 14 '18 at 16:07











  • @f1sh, please don't answer questions in comments. Add an answer or answers below.

    – jaco0646
    Nov 14 '18 at 18:09



















  • What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

    – f1sh
    Nov 14 '18 at 15:45













  • Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

    – janardhan sharma
    Nov 14 '18 at 16:07











  • @f1sh, please don't answer questions in comments. Add an answer or answers below.

    – jaco0646
    Nov 14 '18 at 18:09

















What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

– f1sh
Nov 14 '18 at 15:45







What about an abstract runStrategy(Player p) method in the Strategy class/interface? Either this or use a generic type to not have the word Player in the Strategy class.

– f1sh
Nov 14 '18 at 15:45















Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

– janardhan sharma
Nov 14 '18 at 16:07





Please add some class UML class diagram if not coded something, if coded please put some snippets. That will help.

– janardhan sharma
Nov 14 '18 at 16:07













@f1sh, please don't answer questions in comments. Add an answer or answers below.

– jaco0646
Nov 14 '18 at 18:09





@f1sh, please don't answer questions in comments. Add an answer or answers below.

– jaco0646
Nov 14 '18 at 18:09












4 Answers
4






active

oldest

votes


















2














You can have the Player as an instance variable of your ConcreteStrategy, eg



class PlayerStratey implements Strategy {
Player player;
public PlayerStrategy(Player pl) {
player = pl;
}
@Override
public void executeStrategy() {
System.out.printf("Player %s is executing his strategy!%n", player);
}
}


EDIT:



As for "a player has a strategy, not the other way around", that's pretty much an implementation detail to me. Here's a PlayerStrategy with an inner class:



class Player {
Strategy strategy = new PlayerStrategy();

// now the player "has a" strategy.
public Strategy getStrategy() {
return strategy();
}

class PlayerStrategy implements Strategy {
@Override
public void executeStrategy() {
System.out.printf("Player %s is executing his strategy!%n", Player.this);
}
}
}


As for this being valid, it does depend on what exactly the Strategy is used for. You'd need to give us the actual interface the strategy represents.






share|improve this answer





















  • 3





    it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

    – elbraulio
    Nov 14 '18 at 15:51











  • @BraulioLopez That is my question... is it really legit to do this ?

    – Kevin V
    Nov 14 '18 at 16:35











  • @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

    – UmshiniWami
    Nov 14 '18 at 16:55











  • @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

    – daniu
    Nov 14 '18 at 17:22



















0














Since the player does something that it needs the strategy for, you can pass the Player instance to the strategy method(s).



interface PlayerStratey {
void stumble(Player p);
}

class DullPlayerStratey implements PlayerStratey {
@Override
public void stumble(Player p) {
p.move(ThreadLocalRandom.current().nextBoolean() ? -1 : 1);
}
}





share|improve this answer































    0














    You can create an abstraction for the Player class (read about Dependency Inversion for more info) and pass it as an argument to the Strategy abstract class. This way, the details remain decoupled.



    If you really need to avoid the circular dependency, you can try to extract from the Player class whatever you need to use in your strategies to a new class, and then pass it as an argument through a method (or constructor) on the Strategyclass.






    share|improve this answer































      0














      You should inject the concrete strategy in your player Class via the Strategy interface. The Strategy then takes a player as argument:



      1- the Interface:



      public interface Strategy {
      public void apply(Player player);
      }


      2- Concrete strategies:



      public class StrategyOne implements Strategy{

      public void apply(Player player) {
      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
      }

      public class StrategyTwo implements Strategy {

      public void apply(Player player) {
      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
      }
      }


      3- Context (here your player class):



      public class Player {
      private Strategy strategy;
      private String name;

      public String getName(){
      return name;
      }

      public Player(Strategy strategy, String name){// construct using your chosen strategy
      this.strategy = strategy;
      this.name = name;
      }

      public void executeStrategy(Player player){
      System.out.print("Player "+ this.getName()+ " applies ");
      strategy.apply(player);
      }
      }

      public static void main(String args) {
      Player playerOne = new Player(new StrategyOne(), "playerOne");
      Player playerTwo = new Player(new StrategyTwo(), "playerTwo");
      playerOne.executeStrategy(playerOne);
      playerOne.executeStrategy(playerTwo);
      playerTwo.executeStrategy(playerTwo);
      playerTwo.executeStrategy(playerOne);
      }


      5- Output:



      Player playerOne applies StrategyOne on playerOne



      Player playerOne applies StrategyOne on playerTwo



      Player playerTwo applies StrategyTwo on playerTwo



      Player playerTwo applies StrategyTwo on playerOne



      Then you have your player applying the strategy that's been assigned to him on the player targeted by the strategy.






      share|improve this answer


























      • that don't really answer my problem...

        – Kevin V
        Nov 14 '18 at 16:25











      • From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

        – UmshiniWami
        Nov 14 '18 at 16:42











      • In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

        – UmshiniWami
        Nov 14 '18 at 16:50











      • @KevinV Ok I think I understand what you mean now =) have a look at the updates

        – UmshiniWami
        Nov 14 '18 at 19:59


















      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can have the Player as an instance variable of your ConcreteStrategy, eg



      class PlayerStratey implements Strategy {
      Player player;
      public PlayerStrategy(Player pl) {
      player = pl;
      }
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", player);
      }
      }


      EDIT:



      As for "a player has a strategy, not the other way around", that's pretty much an implementation detail to me. Here's a PlayerStrategy with an inner class:



      class Player {
      Strategy strategy = new PlayerStrategy();

      // now the player "has a" strategy.
      public Strategy getStrategy() {
      return strategy();
      }

      class PlayerStrategy implements Strategy {
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", Player.this);
      }
      }
      }


      As for this being valid, it does depend on what exactly the Strategy is used for. You'd need to give us the actual interface the strategy represents.






      share|improve this answer





















      • 3





        it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

        – elbraulio
        Nov 14 '18 at 15:51











      • @BraulioLopez That is my question... is it really legit to do this ?

        – Kevin V
        Nov 14 '18 at 16:35











      • @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

        – UmshiniWami
        Nov 14 '18 at 16:55











      • @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

        – daniu
        Nov 14 '18 at 17:22
















      2














      You can have the Player as an instance variable of your ConcreteStrategy, eg



      class PlayerStratey implements Strategy {
      Player player;
      public PlayerStrategy(Player pl) {
      player = pl;
      }
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", player);
      }
      }


      EDIT:



      As for "a player has a strategy, not the other way around", that's pretty much an implementation detail to me. Here's a PlayerStrategy with an inner class:



      class Player {
      Strategy strategy = new PlayerStrategy();

      // now the player "has a" strategy.
      public Strategy getStrategy() {
      return strategy();
      }

      class PlayerStrategy implements Strategy {
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", Player.this);
      }
      }
      }


      As for this being valid, it does depend on what exactly the Strategy is used for. You'd need to give us the actual interface the strategy represents.






      share|improve this answer





















      • 3





        it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

        – elbraulio
        Nov 14 '18 at 15:51











      • @BraulioLopez That is my question... is it really legit to do this ?

        – Kevin V
        Nov 14 '18 at 16:35











      • @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

        – UmshiniWami
        Nov 14 '18 at 16:55











      • @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

        – daniu
        Nov 14 '18 at 17:22














      2












      2








      2







      You can have the Player as an instance variable of your ConcreteStrategy, eg



      class PlayerStratey implements Strategy {
      Player player;
      public PlayerStrategy(Player pl) {
      player = pl;
      }
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", player);
      }
      }


      EDIT:



      As for "a player has a strategy, not the other way around", that's pretty much an implementation detail to me. Here's a PlayerStrategy with an inner class:



      class Player {
      Strategy strategy = new PlayerStrategy();

      // now the player "has a" strategy.
      public Strategy getStrategy() {
      return strategy();
      }

      class PlayerStrategy implements Strategy {
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", Player.this);
      }
      }
      }


      As for this being valid, it does depend on what exactly the Strategy is used for. You'd need to give us the actual interface the strategy represents.






      share|improve this answer















      You can have the Player as an instance variable of your ConcreteStrategy, eg



      class PlayerStratey implements Strategy {
      Player player;
      public PlayerStrategy(Player pl) {
      player = pl;
      }
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", player);
      }
      }


      EDIT:



      As for "a player has a strategy, not the other way around", that's pretty much an implementation detail to me. Here's a PlayerStrategy with an inner class:



      class Player {
      Strategy strategy = new PlayerStrategy();

      // now the player "has a" strategy.
      public Strategy getStrategy() {
      return strategy();
      }

      class PlayerStrategy implements Strategy {
      @Override
      public void executeStrategy() {
      System.out.printf("Player %s is executing his strategy!%n", Player.this);
      }
      }
      }


      As for this being valid, it does depend on what exactly the Strategy is used for. You'd need to give us the actual interface the strategy represents.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 7:52

























      answered Nov 14 '18 at 15:44









      daniudaniu

      7,64521635




      7,64521635








      • 3





        it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

        – elbraulio
        Nov 14 '18 at 15:51











      • @BraulioLopez That is my question... is it really legit to do this ?

        – Kevin V
        Nov 14 '18 at 16:35











      • @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

        – UmshiniWami
        Nov 14 '18 at 16:55











      • @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

        – daniu
        Nov 14 '18 at 17:22














      • 3





        it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

        – elbraulio
        Nov 14 '18 at 15:51











      • @BraulioLopez That is my question... is it really legit to do this ?

        – Kevin V
        Nov 14 '18 at 16:35











      • @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

        – UmshiniWami
        Nov 14 '18 at 16:55











      • @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

        – daniu
        Nov 14 '18 at 17:22








      3




      3





      it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

      – elbraulio
      Nov 14 '18 at 15:51





      it should not be the other way around? I mean a player has an Strategy and not a Strategy has a Player.

      – elbraulio
      Nov 14 '18 at 15:51













      @BraulioLopez That is my question... is it really legit to do this ?

      – Kevin V
      Nov 14 '18 at 16:35





      @BraulioLopez That is my question... is it really legit to do this ?

      – Kevin V
      Nov 14 '18 at 16:35













      @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

      – UmshiniWami
      Nov 14 '18 at 16:55





      @KevinV according to your teacher it is not: "as my teacher told me, I must not include Player in Strategy class"

      – UmshiniWami
      Nov 14 '18 at 16:55













      @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

      – daniu
      Nov 14 '18 at 17:22





      @KevinV will you're not including the player in the strategy class, so the loose coupling is achieved, which is the point of the pattern. Ymmv with your teacher's interpretation, he might get it wrong ;) As for who has what (player /strategy), that's pretty much an implementation detail. I can provide an alternative one later.

      – daniu
      Nov 14 '18 at 17:22













      0














      Since the player does something that it needs the strategy for, you can pass the Player instance to the strategy method(s).



      interface PlayerStratey {
      void stumble(Player p);
      }

      class DullPlayerStratey implements PlayerStratey {
      @Override
      public void stumble(Player p) {
      p.move(ThreadLocalRandom.current().nextBoolean() ? -1 : 1);
      }
      }





      share|improve this answer




























        0














        Since the player does something that it needs the strategy for, you can pass the Player instance to the strategy method(s).



        interface PlayerStratey {
        void stumble(Player p);
        }

        class DullPlayerStratey implements PlayerStratey {
        @Override
        public void stumble(Player p) {
        p.move(ThreadLocalRandom.current().nextBoolean() ? -1 : 1);
        }
        }





        share|improve this answer


























          0












          0








          0







          Since the player does something that it needs the strategy for, you can pass the Player instance to the strategy method(s).



          interface PlayerStratey {
          void stumble(Player p);
          }

          class DullPlayerStratey implements PlayerStratey {
          @Override
          public void stumble(Player p) {
          p.move(ThreadLocalRandom.current().nextBoolean() ? -1 : 1);
          }
          }





          share|improve this answer













          Since the player does something that it needs the strategy for, you can pass the Player instance to the strategy method(s).



          interface PlayerStratey {
          void stumble(Player p);
          }

          class DullPlayerStratey implements PlayerStratey {
          @Override
          public void stumble(Player p) {
          p.move(ThreadLocalRandom.current().nextBoolean() ? -1 : 1);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 15:47









          Mark JeronimusMark Jeronimus

          5,17122136




          5,17122136























              0














              You can create an abstraction for the Player class (read about Dependency Inversion for more info) and pass it as an argument to the Strategy abstract class. This way, the details remain decoupled.



              If you really need to avoid the circular dependency, you can try to extract from the Player class whatever you need to use in your strategies to a new class, and then pass it as an argument through a method (or constructor) on the Strategyclass.






              share|improve this answer




























                0














                You can create an abstraction for the Player class (read about Dependency Inversion for more info) and pass it as an argument to the Strategy abstract class. This way, the details remain decoupled.



                If you really need to avoid the circular dependency, you can try to extract from the Player class whatever you need to use in your strategies to a new class, and then pass it as an argument through a method (or constructor) on the Strategyclass.






                share|improve this answer


























                  0












                  0








                  0







                  You can create an abstraction for the Player class (read about Dependency Inversion for more info) and pass it as an argument to the Strategy abstract class. This way, the details remain decoupled.



                  If you really need to avoid the circular dependency, you can try to extract from the Player class whatever you need to use in your strategies to a new class, and then pass it as an argument through a method (or constructor) on the Strategyclass.






                  share|improve this answer













                  You can create an abstraction for the Player class (read about Dependency Inversion for more info) and pass it as an argument to the Strategy abstract class. This way, the details remain decoupled.



                  If you really need to avoid the circular dependency, you can try to extract from the Player class whatever you need to use in your strategies to a new class, and then pass it as an argument through a method (or constructor) on the Strategyclass.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 18:16









                  Ricardo CosteiraRicardo Costeira

                  6091613




                  6091613























                      0














                      You should inject the concrete strategy in your player Class via the Strategy interface. The Strategy then takes a player as argument:



                      1- the Interface:



                      public interface Strategy {
                      public void apply(Player player);
                      }


                      2- Concrete strategies:



                      public class StrategyOne implements Strategy{

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }

                      public class StrategyTwo implements Strategy {

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }
                      }


                      3- Context (here your player class):



                      public class Player {
                      private Strategy strategy;
                      private String name;

                      public String getName(){
                      return name;
                      }

                      public Player(Strategy strategy, String name){// construct using your chosen strategy
                      this.strategy = strategy;
                      this.name = name;
                      }

                      public void executeStrategy(Player player){
                      System.out.print("Player "+ this.getName()+ " applies ");
                      strategy.apply(player);
                      }
                      }

                      public static void main(String args) {
                      Player playerOne = new Player(new StrategyOne(), "playerOne");
                      Player playerTwo = new Player(new StrategyTwo(), "playerTwo");
                      playerOne.executeStrategy(playerOne);
                      playerOne.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerOne);
                      }


                      5- Output:



                      Player playerOne applies StrategyOne on playerOne



                      Player playerOne applies StrategyOne on playerTwo



                      Player playerTwo applies StrategyTwo on playerTwo



                      Player playerTwo applies StrategyTwo on playerOne



                      Then you have your player applying the strategy that's been assigned to him on the player targeted by the strategy.






                      share|improve this answer


























                      • that don't really answer my problem...

                        – Kevin V
                        Nov 14 '18 at 16:25











                      • From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                        – UmshiniWami
                        Nov 14 '18 at 16:42











                      • In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                        – UmshiniWami
                        Nov 14 '18 at 16:50











                      • @KevinV Ok I think I understand what you mean now =) have a look at the updates

                        – UmshiniWami
                        Nov 14 '18 at 19:59
















                      0














                      You should inject the concrete strategy in your player Class via the Strategy interface. The Strategy then takes a player as argument:



                      1- the Interface:



                      public interface Strategy {
                      public void apply(Player player);
                      }


                      2- Concrete strategies:



                      public class StrategyOne implements Strategy{

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }

                      public class StrategyTwo implements Strategy {

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }
                      }


                      3- Context (here your player class):



                      public class Player {
                      private Strategy strategy;
                      private String name;

                      public String getName(){
                      return name;
                      }

                      public Player(Strategy strategy, String name){// construct using your chosen strategy
                      this.strategy = strategy;
                      this.name = name;
                      }

                      public void executeStrategy(Player player){
                      System.out.print("Player "+ this.getName()+ " applies ");
                      strategy.apply(player);
                      }
                      }

                      public static void main(String args) {
                      Player playerOne = new Player(new StrategyOne(), "playerOne");
                      Player playerTwo = new Player(new StrategyTwo(), "playerTwo");
                      playerOne.executeStrategy(playerOne);
                      playerOne.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerOne);
                      }


                      5- Output:



                      Player playerOne applies StrategyOne on playerOne



                      Player playerOne applies StrategyOne on playerTwo



                      Player playerTwo applies StrategyTwo on playerTwo



                      Player playerTwo applies StrategyTwo on playerOne



                      Then you have your player applying the strategy that's been assigned to him on the player targeted by the strategy.






                      share|improve this answer


























                      • that don't really answer my problem...

                        – Kevin V
                        Nov 14 '18 at 16:25











                      • From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                        – UmshiniWami
                        Nov 14 '18 at 16:42











                      • In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                        – UmshiniWami
                        Nov 14 '18 at 16:50











                      • @KevinV Ok I think I understand what you mean now =) have a look at the updates

                        – UmshiniWami
                        Nov 14 '18 at 19:59














                      0












                      0








                      0







                      You should inject the concrete strategy in your player Class via the Strategy interface. The Strategy then takes a player as argument:



                      1- the Interface:



                      public interface Strategy {
                      public void apply(Player player);
                      }


                      2- Concrete strategies:



                      public class StrategyOne implements Strategy{

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }

                      public class StrategyTwo implements Strategy {

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }
                      }


                      3- Context (here your player class):



                      public class Player {
                      private Strategy strategy;
                      private String name;

                      public String getName(){
                      return name;
                      }

                      public Player(Strategy strategy, String name){// construct using your chosen strategy
                      this.strategy = strategy;
                      this.name = name;
                      }

                      public void executeStrategy(Player player){
                      System.out.print("Player "+ this.getName()+ " applies ");
                      strategy.apply(player);
                      }
                      }

                      public static void main(String args) {
                      Player playerOne = new Player(new StrategyOne(), "playerOne");
                      Player playerTwo = new Player(new StrategyTwo(), "playerTwo");
                      playerOne.executeStrategy(playerOne);
                      playerOne.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerOne);
                      }


                      5- Output:



                      Player playerOne applies StrategyOne on playerOne



                      Player playerOne applies StrategyOne on playerTwo



                      Player playerTwo applies StrategyTwo on playerTwo



                      Player playerTwo applies StrategyTwo on playerOne



                      Then you have your player applying the strategy that's been assigned to him on the player targeted by the strategy.






                      share|improve this answer















                      You should inject the concrete strategy in your player Class via the Strategy interface. The Strategy then takes a player as argument:



                      1- the Interface:



                      public interface Strategy {
                      public void apply(Player player);
                      }


                      2- Concrete strategies:



                      public class StrategyOne implements Strategy{

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }

                      public class StrategyTwo implements Strategy {

                      public void apply(Player player) {
                      System.out.println(this.getClass().getSimpleName() +" on " + player.getName());
                      }
                      }


                      3- Context (here your player class):



                      public class Player {
                      private Strategy strategy;
                      private String name;

                      public String getName(){
                      return name;
                      }

                      public Player(Strategy strategy, String name){// construct using your chosen strategy
                      this.strategy = strategy;
                      this.name = name;
                      }

                      public void executeStrategy(Player player){
                      System.out.print("Player "+ this.getName()+ " applies ");
                      strategy.apply(player);
                      }
                      }

                      public static void main(String args) {
                      Player playerOne = new Player(new StrategyOne(), "playerOne");
                      Player playerTwo = new Player(new StrategyTwo(), "playerTwo");
                      playerOne.executeStrategy(playerOne);
                      playerOne.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerTwo);
                      playerTwo.executeStrategy(playerOne);
                      }


                      5- Output:



                      Player playerOne applies StrategyOne on playerOne



                      Player playerOne applies StrategyOne on playerTwo



                      Player playerTwo applies StrategyTwo on playerTwo



                      Player playerTwo applies StrategyTwo on playerOne



                      Then you have your player applying the strategy that's been assigned to him on the player targeted by the strategy.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 14 '18 at 19:57

























                      answered Nov 14 '18 at 16:00









                      UmshiniWamiUmshiniWami

                      6118




                      6118













                      • that don't really answer my problem...

                        – Kevin V
                        Nov 14 '18 at 16:25











                      • From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                        – UmshiniWami
                        Nov 14 '18 at 16:42











                      • In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                        – UmshiniWami
                        Nov 14 '18 at 16:50











                      • @KevinV Ok I think I understand what you mean now =) have a look at the updates

                        – UmshiniWami
                        Nov 14 '18 at 19:59



















                      • that don't really answer my problem...

                        – Kevin V
                        Nov 14 '18 at 16:25











                      • From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                        – UmshiniWami
                        Nov 14 '18 at 16:42











                      • In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                        – UmshiniWami
                        Nov 14 '18 at 16:50











                      • @KevinV Ok I think I understand what you mean now =) have a look at the updates

                        – UmshiniWami
                        Nov 14 '18 at 19:59

















                      that don't really answer my problem...

                      – Kevin V
                      Nov 14 '18 at 16:25





                      that don't really answer my problem...

                      – Kevin V
                      Nov 14 '18 at 16:25













                      From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                      – UmshiniWami
                      Nov 14 '18 at 16:42





                      From what I understood of your problem you want to use a strategy pattern in which the Player uses a given strategy. In this case, the Player is dependent on the chosen strategy. Do you mean you are trying to have the Strategy depend on the Player?

                      – UmshiniWami
                      Nov 14 '18 at 16:42













                      In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                      – UmshiniWami
                      Nov 14 '18 at 16:50





                      In short my answers to "how can I access to Player without including it in my Strategy ?" is you don't. The Player accesses the strategy as you hinted: "knowing that Player have a Strategy"

                      – UmshiniWami
                      Nov 14 '18 at 16:50













                      @KevinV Ok I think I understand what you mean now =) have a look at the updates

                      – UmshiniWami
                      Nov 14 '18 at 19:59





                      @KevinV Ok I think I understand what you mean now =) have a look at the updates

                      – UmshiniWami
                      Nov 14 '18 at 19:59



                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine