Question

I am a bit confused about the best way to use interface extraction when I have multiple layers of classes and interfaces. Suppose I have the following hierarchy:

Foo : WidgetBase, IWidget 
WidgetBase: BusinessObject, IThingy
IThingy : IGenericObject

Now I know I have to extract an IFoo interface into a separate project; but does that interface structure have to replicate the real structure - meaning that I would have to extract interfaces for BusinessObject and WidgetBase too? Or does it mean that I should flatten the structure when creating extracted interface, creating 1 interface, IFoo, containing properties corresponding to the properties of Foo AND all of its parent classes and interfaces, all the way up the hierarchy?

There are also some enum type properties in my interfaces - how can I best tackle those?

public interface IWidget
{
    public WidgetType Type { get; set; }
}

public enum WidgetType
{
    Big, Small
}

Please shed some light.

Was it helpful?

Solution

Minimum requirements for IFoo

IFoo should inherit IWidget and IThingy. It also needs to include all public members of Foo and WidgetBase.

public interface IFoo : IWidget, IThingy
{
    // public member of Foo and WidgetBase...
}

Potential refactoring 1:

It would probably be better to define an interface for WidgetBase like this:

public interface IWidgetBase : IThingy
{
    // public members of WidgetBase...
}

then IFoo could just inherit that:

public interface IFoo : IWidget, IWidgetBase
{
    // public member of Foo.
}

Potential refactoring 2:

The same could apply to BusonessObject: define an interface for it, and have IWidgetBase inherit that.

General principal:

Your interfaces should specify the behaviours of your objects, and should use inheritance where behaviours are naturally extended.

OTHER TIPS

I will recommend that depending on your purpose of the new interface. What behaviors you want the IFoo to have? In other word, what is it used for in the new project. Extract just enough for the new project. Do not mess up your IFoo with the inheritance from other interfaces or from the current state of Foo class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top