· 6 min read

Chuck Norris's Guide to the Strategy Pattern in Java

~~Oh, the legend of Chuck Norris continues to astound us with his unrivaled coding prowess! Now, let's witness the breathtaking power of...

~~Oh, the legend of Chuck Norris continues to astound us with his unrivaled coding prowess! Now, let's witness the breathtaking power of...

Chuck Norris’s Guide to the Strategy Pattern in Java

Favour composition over inheritance

~~Oh, the legend of Chuck Norris continues to astound us with his unrivalled coding prowess! Let’s witness the breathtaking power of composition as Chuck implements RoundhouseKick and LawAndOrder. Brace yourself for the awe-inspiring code!

First, we’ll tackle inheritance. In the world of inheritance, classes are derived from other classes. When Chuck Norris gets involved, those classes are derived from legends.

1. Foot Meets Face: An Introduction to Diverse Roundhouse Kick Implementations

interface RoundhouseKick {
  void performRoundhouseKick();
}

class NormalRoundhouseKick implements RoundhouseKick {
  // This is where the magic happens. A mere implementation of RoundhouseKick.
  @Override
  public void performRoundhouseKick() {
    System.out.println("Just a normal roundhouse kick, because sometimes, being average is a superpower too!");
  }
}

class ChuckRoundhouseKick implements RoundhouseKick {
  // The most deadly implementation of RoundhouseKick!
  @Override
  public void performRoundhouseKick() {
    System.out.println("Chuck Norris just delivered a roundhouse kick! The class hierarchy shakes in fear!");
  }
}

2. Justice Served… With a Side of Chuck Norris: Implementations of Law Enforcement in Java

interface LawAndOrder {
  void enforceLaw();
}

class NormalLawAndOrder implements LawAndOrder {
  // Law and order version v1.0
  @Override
  public void enforceLaw() {
    System.out.println("Without Chuck who will deliver justice?");
  }
}

class ChuckLawAndOrder implements LawAndOrder {
  // Chuck Norris takes composition to a whole new level with a separate implementation of LawAndOrder.
  @Override
  public void enforceLaw() {
    System.out.println("With Chuck, laws are just cute suggestions.");
  }
}

3. Chuck Norris wasn’t created; he simply roundhouse-kicked his way into existence!

// Chuck Norris can only perform normal roundhouse kicks, quick, let's rectify this before something terrible happens
class ChuckNorris extends NormalRoundhouseKick {
}

class ChuckNorris extends NormalRoundhouseKick {
  // Phew, Chuck Norris extends NormalRoundhouseKick to override the normal power level of Roundhouse!
  @Override
  public void performRoundhouseKick() {
    System.out.println("Chuck Norris just delivered a roundhouse kick! The JVM is in awe!");
  }
}

4. Where Roundhouse Kicks Meet Street Justice – The Ultimate Law and Order Edition

// Through sheer will, ChuckNorris can extend multiple classes. 
// No, seriously, this isn't possible in Java!
class ChuckNorris extends ChuckRoundhouseKick, ChuckLawAndOrder {
  // Chuck Norris extends NormalRoundhouseKick to override Roundhouse's normal power level!
  @Override
  public void performRoundhouseKick() {
    System.out.println("Chuck Norris just roundhouse kicked the diamond problem!");
  }

  @Override
  public void enforceLaw() {
    System.out.println("The JVM now only respects Chuck Norris's law of inheritance");
  }
}

// This solution is not a joke, we've implemented enforcing the law here.
// What if we need backup? What if we create VinDiesel? 
// They need to enforce the same law and order, or else it'll fast and furiously become anarchy.
class ChuckNorris extends ChuckRoundhouseKick implements LawAndOrder {
  @Override
  public void enforceLaw() {
    System.out.println("With Chuck, justice isn't blind. It's just too scared to look.");
  }
  
    // As we can see above, inheritance is very limiting. 
    // We can't do "ChuckNorris extends ChuckRoundhouseKick, ChuckLawAndOrder" in Java.
    // There is one class you can extend. You have to choose between ChuckRoundhouseKick or ChuckLawAndOrder.
    // You can have many interfaces. 
    // But this won't help VinDiesel sort out, frankly, what seems to be spiralling family issues without 
    //   Chuck's implementation of law and order.
    // And to boot, this can't change at runtime.
}

5. Grand Finale - Composition - The Art of Chuck Norris Fusion!

class ChuckNorris implements RoundhouseKick, LawAndOrder {
  private RoundhouseKick roundhouseKick;
  private LawAndOrder lawAndOrder;

  public ChuckNorris(RoundhouseKick roundhouseKick, LawAndOrder lawAndOrder) {
    this.roundhouseKick = roundhouseKick;
    this.lawAndOrder = lawAndOrder;
  }

  // ChuckNorris now wields the fusion of RoundhouseKick and LawAndOrder!
  @Override
  public void performRoundhouseKick() {
    roundhouseKick.performRoundhouseKick();
  }

  @Override
  public void enforceLaw() {
    lawAndOrder.enforceLaw();
  }
}

Now we’re talking! Chuck Norris’s code is a composition masterpiece. He’s not just a class, he’s an orchestra of interfaces and implementations, each tuned to perfection. He can perform roundhouse kicks with the precision of a master martial artist and enforce the law with the iron fist of justice!

With composition, Chuck Norris achieves ultimate flexibility and re-usability. Need another implementation? Easy! Chuck can create it with a mere snap of his fingers. His codebase is so efficient that it defies the laws of software engineering!

Another example

6. Enhancing Interfaces with Default Methods, these bad boys were unleashed in Java 1.8

interface RoundhouseKick {
  void performRoundhouseKick();

  default void practiceRoundhouseKick() {
    System.out.println("Practicing a roundhouse kick. But let's be real, you'll never be Chuck.");
  }
}

interface LawAndOrder {
  void enforceLaw();

  default void studyLaw() {
    System.out.println("Studying the law. Pfft, as if it matters when Chuck is around.");
  }
}

7. Chuck Norris’ DNA: Fear and Awe through Inheritance

class ChuckNorrisInheritance extends ChuckRoundhouseKick implements LawAndOrder {
  @Override
  public void enforceLaw() {
    System.out.println("With Chuck, laws are just cute suggestions.");
  }

  // Chuck can override default methods if he wishes.
  @Override
  public void practiceRoundhouseKick() {
    System.out.println("Chuck doesn't practice. Practice needs Chuck.");
  }
}

9. Encore - Using Composition - Assembling Chuck’s Arsenal (As If He Needs It)

class ChuckNorrisComposition {
  private RoundhouseKick kickStyle;
  private LawAndOrder lawStyle;

  public ChuckNorrisComposition(RoundhouseKick kickStyle, LawAndOrder lawStyle) {
    this.kickStyle = kickStyle;
    this.lawStyle = lawStyle;
  }

  public void performKick() {
    kickStyle.performRoundhouseKick();
  }

  public void practiceKick() {
    kickStyle.practiceRoundhouseKick();
  }

  public void enforceLaw() {
    lawStyle.enforceLaw();
  }

  public void studyLaw() {
    lawStyle.studyLaw();
  }

  public void setKickStyle(RoundhouseKick newKick) {
    this.kickStyle = newKick;
    System.out.println("Why would Chuck change his kicking style? Oh right, to terrify the laws of physics.");
  }

  public void setLawStyle(LawAndOrder newLaw) {
    this.lawStyle = newLaw;
    System.out.println("Law and Order: Chuck Norris Edition - where the laws bend to Chuck's will.");
  }
}

10. Chuck Norris doesn’t call the main method, it calls him

public class Main {
  public static void main(String[] args) {
    // Using inheritance, we're limiting our action hero.
    ChuckNorrisInheritance chuckInheritance = new ChuckNorrisInheritance();
    chuckInheritance.performRoundhouseKick();
    chuckInheritance.practiceRoundhouseKick();
    chuckInheritance.enforceLaw();

    // Using composition, our action hero has a fighting chance
    ChuckNorrisComposition chuckComposition = new ChuckNorrisComposition(new NormalRoundhouseKick(), new NormalLawAndOrder());
    chuckComposition.performKick();
    chuckComposition.practiceKick();
    chuckComposition.enforceLaw();
    chuckComposition.studyLaw();

    System.out.println("Wait... Chuck's not feeling merciful. He's changing his style.");
    chuckComposition.setKickStyle(new ChuckRoundhouseKick());
    chuckComposition.setLawStyle(new ChuckLawAndOrder());

    chuckComposition.performKick();
    chuckComposition.enforceLaw();
  }
}

And thus ends another Chuck-tacular composition extravaganza! We’ve sneakily sprinkled in some ‘studying’ and ’ practising’ for a hint of realism… but who are we kidding? From the day Chuck roundhouse kicked himself out of the womb, he was ready.

So, dear programmers, take inspiration from Chuck Norris and embrace the power of composition in your Java adventures. Just remember, when Chuck codes, even the bugs themselves bow down to his supreme programming skills! 🎸🎶🥋~~

Thanks to Unsplash for the blog cover image - https://unsplash.com/photos/green-tree-on-green-grass-field-during-daytime-vLAmX7UXv6M

Back to Blog