0

Hi I was watching a video on the "strategy pattern" and I understand his explanation on paper but I have no idea how it would look in code because I have never worked with interfaces before and his example relies heavily on the use of interfaces.

Here is a screen shot of the UML diagram as a reference: enter image description here

The goal was to have different implementations of the quack() method. SimpleQuack and NoQuack

How would this look in Java code? Lets say that the City Duck class implements SimpleQuack where as Wild Duck implements NoQuack.

And why is it beneficial to use an interface to achieve this instead of defining the behavior in the inherited class like one would normally do.

jakegergen
  • 135
  • 1
  • 1
  • 10
  • The base interface has a method called speak(). CityDuck implements this by calling a method called simpleQuack and WildDuck with a method called noQuack. – NomadMaker Aug 17 '20 at 03:53
  • Why is this different then overriding the speak method like in normal inheritance – jakegergen Aug 17 '20 at 03:54
  • Nothing, but since you didn't take the time to put your point into text and I could barely read the image, it was the best I could do. When you ask for help here, you need to provide the information to help you. – NomadMaker Aug 17 '20 at 03:58
  • @NomadMaker "And why is it beneficial to use an interface to achieve this instead of defining the behavior in the inherited class like one would normally do." – jakegergen Aug 17 '20 at 04:01
  • @KevinAnderson Yes that was super helpful thank you! – jakegergen Aug 17 '20 at 04:03
  • 1
    The _interface_ doesn't implement two different methods; you have different classes that all implement the interface in different ways. – chrylis -cautiouslyoptimistic- Aug 17 '20 at 04:08
  • The strategy pattern is about changing the behavior of an object at run time. Inheritance is about changing it at compile time. Please google "java strategy pattern" for tutorials that you can read. – NomadMaker Aug 17 '20 at 04:15
  • @NomadMaker Researching the strategy pattern is how I came up with this question in the first place. That's why my screenshot is of a video titled "Strategy Pattern - Design Patterns (ep 1)" – jakegergen Aug 18 '20 at 13:20
  • You'll probably get more out of one of the tutorials that are written, rather then a video. Also, a clear shot of the white-board behind him would have been better, rather than one with his head in the way. – NomadMaker Aug 18 '20 at 13:27
  • @NomadMaker brother relax....My question has already been answered. – jakegergen Aug 19 '20 at 14:02

0 Answers0