views:

53

answers:

2

I'm writing an iPhone application and I find that there are three controllers in the application that have very similar functionality. They are similar enough that it doesn't make sense to separate them into three separate classes, so I have a "mode" property that clients of the class use to specify how the controller should behave in certain situations. But again, maybe 95% of the functionality is identical. There are three separate modes with only minor differences in behavior.

This feels messy to me. Is there a better pattern for this?

+1  A: 

You could try inheritance ... the three controllers can all inherit from a common base that implements the shared functionality.

Aside from that, you could look at the Strategy Pattern.

Which one you use depends on what your code is doing and what the bits that change look like :-)

Joel Martinez
Thanks - Strategy is what I'm looking for.
Bill
A: 

A similar approach would involve not to use inheritance (i.e.: use the same controller for the three screens) and use the state pattern to define specific behavior for each of the screens.

TehJabbit