views:

30

answers:

2

I am looking for a design pattern where it fits these specs:

1- Say you have a package animal including 'Animal', 'Cat' and 'Dog'.

2- You have to modify a method behaviour in 'Animal' (also overridden in 'Cat' and 'Dog' as in the package) without doing any modification in the package animal source code.

I did find Visitor Pattern for this kind of purpose, but the problem with that you have to modify package animal source code with 'implement'ing a Visitable interface including accept method. However, this is not allowed.

What do you suggest?

+1  A: 

I am not exactly sure about the spec, but you could use Decorator, to create classes outside the animal package, which will use composition to extend the normal behaviour.

Gabriel Ščerbák
A: 

I agree with @Gabriel, the Decorator pattern might be a good place to start. An easy to understand example can be found on Holger Kreissl's Devblog at Decorator Pattern in C#.

Edward Leno