构建器之间的继承 - 如何处理类型?

时间:2012-12-19 10:10:04

标签: c# oop design-patterns builder

我在C#中有这个构建器(当然,这个例子很简单):

class BusBuilder
{
    Wheels mWheels = DefaultWheels;
    int mRoute = 0;

    public BusBuilder WithWheels(Wheels aWheels)
    {
        mWheels = aWheels;
        return this;
    }

    public BusBuilder WithRoute(int aRoute)
    {
        mRoute = aRoute;
        return this;
    }

    public Bus Build()
    {
        return new Bus { Wheels = mWheels, Route = mRoute };
    }
}

它的使用方式如下:

Bus bus = 
    new BusBuilder()
    .WithWheels(someWheels)
    .WithRoute(50)
    .Build()

现在我想提取一个只包含部分方法的超类:

class VehicleBuilder
{
    Wheels mWheels = DefaultWheels;

    public VehicleBuilder WithWheels(Wheels aWheels)
    {
        mWheels = aWheels;
        return this;
    }
}

class BusBuilder : VehicleBuilder
{
    ...
}

问题是现在我不能写

Bus bus = 
    new BusBuilder()
    .WithWheels(someWheels)
    .WithRoute(50)
    .Build()

因为WithWheels返回VehicleBuilder而非BusBuilder,因此未定义WithRoute方法。

你会如何设计?

1 个答案:

答案 0 :(得分:13)

构建器模式在继承方面有点痛苦。你可以这样做:

class VehicleBuilder<T> where T : VehicleBuilder<T>
{
    private T @this;

    protected VehicleBuilder()
    {
        // Or pass it in as a constructor parameter
        @this = (T) this;
    }

    public T WithWheels(...)
    {
        return @this;
    }
}

然后:

class BusBuilder : VehicleBuilder<BusBuilder>
{
    ...
}

此时,您的WithWheels方法仍会返回BusBuilder,因此您仍然可以拨打WithRoute

您还需要在每个派生类构建器中使用新的Build方法,请注意......