title: Using the Strategy Pattern to avoid downcasting
body: <p>I was reading on <a href="https://dzone.com/articles/the-liskov-substitution-principle-with-examples" rel="nofollow noreferrer">this site</a> about the Liskov substitution principle. It states:</p>
  
  <blockquote>
    <p>As per the LSP, functions that use references to base classes must be
    able to use objects of the derived class without knowing it. In simple
    words, derived classes must be substitutable for the base class.</p>
  </blockquote>
  
  <p>According to <a href="https://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/" rel="nofollow noreferrer">this page</a>,  if you override a method in the base class and it does nothing or throws an exception, you're in violation of the principle. </p>
  
  <p>Suppose I had an <code>abstract class</code> called <code>Weapon</code>, and the <code>subclasses</code> <code>ReloadableWeapon</code> and <code>Sword</code>. <code>ReloadableWeapon</code> contains a method that's unique to that class, called <code>Reload()</code>. When declaring objects, standard practice is you do it from the abstract class and then subclass, like so:</p>
  
  <pre><code>Weapon rifle = new ReloadableWeapon();
  Weapon sword = new Sword();
  </code></pre>
  
  <p>If I wanted to use the reload <code>method</code> for a <code>rifle</code>, I could <code>cast</code> it. Based on numerous articles and textbooks, this could lead to problems later on.</p>
  
  <p>Also, if I have the reload method in the base class Weapon, then Sword would ignore or throw, which is wrong.   </p>
  
  <p>If I wanted to avoid all that, would using the <code>Strategy Pattern</code> be a viable option? Like this:</p>
  
  <pre><code>public final Weapon{
  
      private final String name;
      private final int damage;
      private final List&lt;AttackStrategy&gt; validactions;
      private final List&lt;Actions&gt; standardActions;
  
      private Weapon(String name, int damage, List&lt;AttackStrategy&gt; standardActions, List&lt;Actions&gt; attacks)
      {
          this.name = name;
          this.damage = damage;
          standardActions = new ArrayList&lt;Actions&gt;(standardActions);
          validAttacks = new ArrayList&lt;AttackStrategy&gt;(validActions);
      }
  
      public void standardAction(String action){} // -- Can call reload or aim here.  
  
      public int attack(String action){} // - Call any actions that are attacks. 
  
      public static Weapon ReloadableWeapon(String name, int damage){
          return new Weapon(name, damage, this.constructActions(), this.constructStandardActions);
      }
  
      public static Weapon Sword(String name, damage){
          return new Weapon(name, damage, this.standardSwordActions, this.swordActions);
      }
  
      //returns a List collection that contains the actions for a reloadable Weaopon. - Shoot 
      private List&lt;AttackStrategy&gt; reloadableActions(){}
  
      //returns a List collection of standard non attack actions. - Reload 
      private List&lt;Actions&gt; standardReloadableActions(){}
  
       //returns a List collection that contains the actions for a Sword - Swing/Strike  
      private List&lt;AttackStrategy&gt; swordActions(){}
  
      //returns a List collection of standard non attack actions. - Sharpen 
      private List&lt;Actions&gt; standardSwordActions(){}
  
  }
  </code></pre>
  
  <p><strong>Attack Interface and Implementation:</strong></p>
  
  <pre><code>public interface AttackStrategy{
      void attack(Enemy enemy);
  }
  
  public class Shoot implements AttackStrategy {
      public void attack(Enemy enemy){
          //code to shoot
      }
  }
  
  public class Strike implements AttackStrategy {
      public void attack(Enemy enemy){
          //code to strike
      }
  }
  </code></pre>
  
  <p>By having the <code>List&lt;AttackStrategy&gt;</code> being constructed inside the Weapon class, client code can't pass in <code>List&lt;AttackStrategy&gt;</code> not meant for a certain types of <code>Weapons</code>, for example, a Sword cannot shoot bullets, and doesn't reload, if I added a grenade, it shouldn't be able to strike like a Sword(you get the idea).</p>
  
  <p>I'm not asking if I've implemented the <code>Strategy Pattern</code> correctly, but rather can I use the <code>pattern</code> when faced with a <code>subclass</code> that has a <code>method</code> unique to that <code>subclass</code> and I don't want to <code>cast</code> it? or in other words, rather than violate the LSP, can I prohibit the use of inheritance and use the <code>Strategy Pattern</code> to implement the require methods?</p>
  
  <p><strong>Notes:</strong><br>
  The pattern solves my problem in 2 ways:</p>
  
  <ol>
  <li>I don't have to downcast, I can store my Weapons in a <code>List&lt;Weapon&gt;</code> collection without worrying about checking the type, and then <code>casting</code></li>
  <li>Any <code>weapon</code> that isn't <code>Reloadable</code>, won't have the <code>concrete class</code> <code>Reload</code>. This mean no <code>throwing</code> or leaving the <code>method</code> blank</li>
  </ol>
  
  <p>My problem is, given the <a href="https://dzone.com/articles/design-patterns-strategy" rel="nofollow noreferrer">definition</a> of the Strategy pattern, I don't believe I'm using it in that context.</p>
  
  <p>From the site:</p>
  
  <blockquote>
    <p>The Strategy pattern is to be used where you want to choose the
    algorithm to use at runtime.
    ...</p>
    
    <p>The Strategy pattern provides a way to define a family of algorithms,
    encapsulate each one as an object, and make them interchangeable.</p>
  </blockquote>
  
  <p>I like this approach, could I just look at it as allowing the client to choose how to use a <code>Weapon</code> at <code>runtime</code>, with the added benefit of <code>immutability</code>, and avoiding to <code>cast</code>? or Am I just stretching the definition a little too far?</p>