Strategy Pattern : strategy with the caller as attribute? [duplicate]
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
?
java design-patterns circular-dependency strategy-pattern
marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat
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.
add a comment |
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
?
java design-patterns circular-dependency strategy-pattern
marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat
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 abstractrunStrategy(Player p)
method in theStrategy
class/interface? Either this or use a generic type to not have the wordPlayer
in theStrategy
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
add a comment |
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
?
java design-patterns circular-dependency strategy-pattern
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
java design-patterns circular-dependency strategy-pattern
edited Nov 14 '18 at 18:02
jaco0646
5,21952846
5,21952846
asked Nov 14 '18 at 15:40
Kevin VKevin V
347
347
marked as duplicate by jaco0646, Matthieu Brucher, Rob, Billal Begueradj, GhostCat
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
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 abstractrunStrategy(Player p)
method in theStrategy
class/interface? Either this or use a generic type to not have the wordPlayer
in theStrategy
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
add a comment |
What about an abstractrunStrategy(Player p)
method in theStrategy
class/interface? Either this or use a generic type to not have the wordPlayer
in theStrategy
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
add a comment |
4 Answers
4
active
oldest
votes
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.
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
add a comment |
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);
}
}
add a comment |
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 Strategy
class.
add a comment |
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.
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
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
edited Nov 15 '18 at 7:52
answered Nov 14 '18 at 15:44
daniudaniu
7,67521635
7,67521635
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
add a comment |
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
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
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);
}
}
answered Nov 14 '18 at 15:47
Mark JeronimusMark Jeronimus
5,18622136
5,18622136
add a comment |
add a comment |
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 Strategy
class.
add a comment |
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 Strategy
class.
add a comment |
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 Strategy
class.
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 Strategy
class.
answered Nov 14 '18 at 18:16
Ricardo CosteiraRicardo Costeira
6241613
6241613
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
What about an abstract
runStrategy(Player p)
method in theStrategy
class/interface? Either this or use a generic type to not have the wordPlayer
in theStrategy
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