Reply to comment

RE: Template Method Pattern (GoF) without abstract methods in Ru

By Andy Jones at 01/11/2019 - 05:28

My 10p:

Many of these patterns were designed for older languages with more basic features. I looked up Template Method -- and, please correct me if I've got the wrong end of the stick -- this is just polymorphism, a basic feature of OOP?

I know Ruby coders often shy away from inheritance (with good reason) but personally this is probably one of those times to consider using it.

Obviously Ruby doesn't have abstract classes or abstract methods. But AFAIK the idiomatic way to simulate an abstract method in Ruby is:

def foo
fail NotImplementedError, "You need to override this method"

...and of course an "abstract class" is any class that has an abstract method in it.

I'm sure that there are dozens of other ways to achieve this goal, other than using inheritance. And, as I say, perhaps I've misunderstood the problem. But for my money if it looks like polymorphism then you want inheritance.

Click here to view Company Information and Confidentiality Notice.<>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <>