DEV Community

Discussion on: Design Patterns: Strategy Pattern

Collapse
 
jvanbruegge profile image
Jan van Brügge

Always when I see Java code and "best practices", I always wonder how people can still use such a verbose and limited language (and paradigm). Let's express your example in Haskell:

{-# LANGUAGE ConstraintKind #-}

class CanSwim a where
    doSwim :: a -> IO()

class CanGreet a where
    doGreet :: a -> IO()

instance CanSwim DogRobot where
    doSwim = dogSwim

instance CanSwim SharkRobot where
    doSwim = sharkSwim

-- ...

type Robot = (CanSwim a, CanGreet a, CanFly a, CanWalk a)

No need for complex class hierarchies, you simply have a few functions following an interface (type class). A robot then is just the composition of the type classes.

Collapse
 
marlysson profile image
Marlysson Silva

Object oriented programming is a limited paradigm to express certain behaviors?

Collapse
 
jvanbruegge profile image
Jan van Brügge

No, Java is limited as a language. OO is verbose and leads to unnecessary abstractions (read: convoluted class hierachies).
E.g. your example:
In haskell: A bunch of functions that follow an interface (1 level)
OO: Robot class, subclass, Behavior interface, behavior implementation (4 levels)