Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One approach would be to utilize the <a href="http://en.wikipedia.org/wiki/Specification_pattern" rel="nofollow">Specification pattern</a>.</p> <p>So, basically, your item would have a <code>WearerSpecification</code> property which returns a specification that returns true if the current character satisfies it.</p> <p>Something like this:</p> <pre><code>public abstract class Item { private readonly ISpecification&lt;Character&gt; _wearerSpecification; protected Item(ISpecification&lt;Character&gt; wearerSpecification) { _wearerSpecification = wearerSpecification; } public ISpecification&lt;Character&gt; WearerSpecification { get { return _wearerSpecification; } } } </code></pre> <p>In the method that is called when the character tries to pick up an item, the code would look something like this:</p> <pre><code>public class Character { public void PickUpItem(Item item) { if(item.WearerSpecification.SatisfiedBy(this)) { // item can be picked up } else { // item can't be picked up } } } </code></pre> <hr> <p>To simplify the actual creation of your items and to not repeat your code, you can create concrete specification classes that represent requirements that often occur, e.g. you could create a <code>AtLeastLevel10Specification</code> or a <code>MinimumStrengthSpecification</code>:</p> <pre><code>public class AtLeastLevel10Specification : ISpecification&lt;Character&gt; { public bool IsSatisfiedBy(Character character) { return character.Level &gt;= 10; } } public class MinimumStrengthSpecification : ISpecification&lt;Character&gt; { private readonly int _minimumRequiredStrength; public MinimumStrengthSpecification(int minimumRequiredStrength) { _minimumRequiredStrength = minimumRequiredStrength; } public bool IsSatisfiedBy(Character character) { return character.Strength &gt;= _minimumRequiredStrength; } } </code></pre> <p>You would then possible use these classes directly inside your item declaration:</p> <pre><code>public class VeryHeavyShield : Item { public VeryHeavyShield() : base(CreateSpecification()) { } private static ISpecification&lt;Character&gt; CreateSpecification() { return new AtLeastLevel10Specification().And( new MinimumStrengthSpecification(50)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload