4 ms·
"Only, the real world doesn’t work that way. It really doesn’t work that way. In morphology, for example, we have penguins, birds that swim. And the bat, a mamm
by V-2 13y ago
"Only, the real world doesn’t work that way. It really doesn’t work that way. In morphology, for example, we have penguins, birds that swim. And the bat, a mammal that flies."
Yes but we have Single Responsibility Principle and while an animal is one object in physical world, it doesn't mean it should be a single object in OOP. Start breaking it down...
public interface BodyType {}
public class TwoArmsTwoLegs implements BodyType {}
public class FourLegs implements BodyType {}
public interface Locomotion<B extends BodyType>
{
void walk(B body);
}
public class BipedWalk implements Locomotion<TwoArmsTwoLegs> {
public void walk(TwoArmsTwoLegs body) {}
}
public class Slither implements Locomotion<NoLimbs>
{
public void walk(NoLimbs body) {}
}
public class Animal
{
BodyType body;
Locomotion locomotion;
}
Animal human = new Animal(new TwoArmsTwoLegs(), new BipedWalk());
(Code sample from an article in Software Development Journal by Łukasz Baran)