In a discussion about the usefulness of inheritance in Java I stumbled over this pattern for a class:
public final class Demo<T>
{
private final T m_Value;
private final Function<T,String> m_Processor;
public Demo( final T value, final Function<T,String> processor )
{
m_Value = value;
m_Processor = processor;
}
public final String process() { return m_Processor.apply( m_Value ); }
}
Confessed, this particular class is of limited use! But it should only demonstrate the underlying pattern: that I can have instances of the same class, but each have a different behaviour because I assigned a different processor on creation time.
Or even worse, when m_Processor
is not final, and I provide a setter for it, it can change its behaviour during its lifetime.
As this resembles the behaviour of JavaScript, a participant of that discussion coined the name "JavaScript Inheritance" for it.
And of course we can start a discussion about the usefulness of this pattern, or if it is in fact an anti-pattern … but please not here! I am just interested in if there is already a name for it!
That the behaviour of Demo
is determined by assigning a processor looks like the "Strategy" pattern, and in this aspect, the sample may be chosen badly.
But "Strategy" is not meant to be an alternative for inheritance, it makes use of polymorphism.
A better candidate would be "Facade" or "Adapter" (from the purpose), but these would be implemented differently.
Aucun commentaire:
Enregistrer un commentaire