• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Composition vs Strategy Pattern

 
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Weapon is interface, it has two implementations - SwordWeapon, ShieldWeapon.
Character is a class, which has dependency for Weapon.
In runtime, with the help of constructor, we can pass any weapon impl to the Weapon instance variable of the Character class.

I know there is a composition involved between Character and Weapon.
So, here my question is, is this strategy pattern, as I am changing different behaviors at runtime?
If yes, so composition is same as Strategy pattern?
If no, then how strategy pattern differ from composition?

Thanks in advance.
 
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:. . . Character is a class . . .

It already exists in the java.lang package, so you should think of a different name for it. Maybe Warrior.
Call your implementations Sword and Shield rather than XXXWeapon.

In runtime, with the help of constructor, we can pass any weapon impl to the Weapon instance variable of the Character class.

Are you deciding the weapon at runtime, or at compile time or before? If you can make a Warrior change between a Sword and a Shield at runtime, that would be something different. This Wikipedia page says the Strategy Pattern should permit changes of the algorithm at runtime. You have a plain simple field there, and I don't see any code to change it.
 
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Campbell said.

This goes into Domain Knowledge rather than Design Patterns, but having a Shield modeled as a Weapon is questionable at best.

Even the simplest Rougelikes from the 80's, Classic Rogue, Mike's Adventure Game, SuperRogue/AdvancedRogue/XRogue/UltraRogue allowed one to equip at least one Shield and at least one Weapon.

There were some interactions between the two, a Buckler was a Shield that didn't require a hand to hold, so you could actually have two weapons equipped rather than just one if you were using that as a Shield.

The Two-Handed Sword could do a LOT of damage, but required two hands and therefore meant that you couldn't also simultaneously have a Shield equipped, except in the games that allowed a Buckler.

The question is about Design Patterns, and is, in fact, something I am studying this week, so I may have more to say about it later.

I have been playing RPG's on-and-off since I was a kid in the 1980's, so I definitely am equipped with both hands to criticize a design for this type of game.  
 
vishnu priyag
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I actually didn’t get the answer.
Can someone please provide the example differences between composition and strategy pattern.

Thanks in advance.
 
Sheriff
Posts: 26943
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Wikipedia article which Campbell linked to says this at the very beginning:

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.



Note that the phrase "run-time" is used twice. Composition doesn't work that way; the objects contained by another object are specified at compile time.
 
vishnu priyag
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
Note that the phrase "run-time" is used twice. Composition doesn't work that way; the objects contained by another object are specified at compile time.



Can you please give me an example, how to specify behavior at ‘run-time’
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your line 7:
       weapon.fight();

Good old Basic Polymorphism means you are choosing behavior at Runtime, rather than compile-time, as the appropriate @Override gets called automatically.

"composition" (lower-case) just means that class A "has a" instance of class B, or in this case, interface Weapon.

I agree with Campbell that if you do not allow the instance of the interface to change during the object's lifetime, that is not the Strategy Pattern.
It should be settable after the object has been created, as many times as may be needed.

=======================================================================

I thought you had got that part, and added that in an actual game (which the Design Pattern doesn't know or care about), you would never be stuck with one Weapon for the whole game (unless it was cursed, but that is a detail), and that a Shield is a different category than a Weapon, as well.

 
vishnu priyag
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:In your line 7:
       weapon.fight();

Good old Basic Polymorphism means you are choosing behavior at Runtime, rather than compile-time, as the appropriate @Override gets called automatically.

"composition" (lower-case) just means that class A "has a" instance of class B, or in this case, interface Weapon.

I agree with Campbell that if you do not allow the instance of the interface to change during the object's lifetime, that is not the Strategy Pattern.
It should be settable after the object has been created, as many times as may be needed.

=======================================================================

I thought you had got that part, and added that in an actual game (which the Design Pattern doesn't know or care about), you would never be stuck with one Weapon for the whole game (unless it was cursed, but that is a detail), and that a Shield is a different category than a Weapon, as well.


So if I use set method in my above Character class, then it is a strategy pattern ?
Eg: setWeapon(Weapon weapon);
 
Paul Clapham
Sheriff
Posts: 26943
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might say "My strategy is to use a Sword"... which is why it's called the Strategy pattern.
 
Paul Clapham
Sheriff
Posts: 26943
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But more seriously, it seems like you are expecting Composition and Strategy to be totally different things -- like Sword and Sausage. But that's not how it is. Composition and Strategy have a lot in common, as you can see from the examples already given.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just don't confuse it with the Composite Design Pattern, which is something else entirely (a common super-class to represent both container nodes and leaf nodes).

Composition is quite close to Composite lexically, but totally different in meanings.
 
vishnu priyag
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all replies.
But one doubt, how come adding weapon through constructor is not a runtime change, but adding weapon through setter is a runtime change?

using constructor:
new Character(new SwordWeapon())
--------------------------------------------------------------
using setter:
Character c = new Character();
c.setWeapon(new SwordWeapon());

even for both I have to pass the object to setter in code same as I do for constructor.
And both provides the ways to choose weapon among many other instead of just hardcoding the weapon behavior at Character class.

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:Thanks . . .

That's a pleasure

how come adding weapon through constructor is not a runtime change, but adding weapon through setter is a runtime change? . . . .

I would suggest that predefining a weapon in code is a change that happens at runtime but is specified [before] compile time. If you have a list of weapon types and the user specifies a weapon to use whilst playing the game, that is more like the strategy pattern.
And please take note of what I said about class names.
 
vishnu priyag
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
And please take note of what I said about class names.


Sure, I will.

Campbell Ritchie wrote:I would suggest that predefining a weapon in code is a change that happens at runtime but is specified [before] compile time. If you have a list of weapon types and the user specifies a weapon to use whilst playing the game, that is more like the strategy pattern.


While starting the game, if the user can choose what weapon to start with, with the help of constructor, then even constructor is also a runtime change right.

eg:


I am not hardcoding in Warrior class like every warrior will start with SwordWeapon, like


So, even Warrior class don't know what weapon it will get, it depends on the user who uses it. So, this is also a runtime change right like setter.

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 74606
335
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:. . . if the user can choose what weapon to start with . . . even constructor is also a runtime change right. . . .

Now you have explained how it works, yes, I agree.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

vishnu priyag wrote:. . . Character is a class . . .

It already exists in the java.lang package, so you should think of a different name for it. Maybe Warrior.
Call your implementations Sword and Shield rather than XXXWeapon.

In runtime, with the help of constructor, we can pass any weapon impl to the Weapon instance variable of the Character class.

Are you deciding the weapon at runtime, or at compile time or before? If you can make a Warrior change between a Sword and a Shield at runtime, that would be something different. This Wikipedia page says the Strategy Pattern should permit changes of the algorithm at runtime. You have a plain simple field there, and I don't see any code to change it.



Hello, Sorry to bump up an old thread but this was featured on the code ranch journal and was an interesting read. So when you say deciding the weapon what do you mean by "before" ? I have only heard of Run vs Compile time. Thanks !
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question that was unclear for a moment was that did we need to see something like:

setWeapon(Weapon weapon);

for it to be a use of the Strategy Pattern, or would it also be said to be a use of the Strategy Pattern if it was only set at the time of object construction:


It is common to see the Strategy vary during the lifetime of the object.
However, if it does not, the above would still be considered the Strategy Pattern.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic