Skip to main content
added 519 characters in body
Source Link
Rzassar
  • 131
  • 4

Set the final Build() method to a different interface, so that the client code is forced to call that method right before building the actual concrete class.

public class House
{
}

public interface IHouseBuilderWithMandatoryMethod
{
    House Build();
}

public interface IHouseBuilder
{
    IHouseBuilder AddBalcony();

    IHouseBuilder AddPool();

    IHouseBuilderWithMandatoryMethod AddBarbecueStation();
}

public class HouseBuilder: IHouseBuilder, IHouseBuilderWithMandatoryMethod
{
    public IHouseBuilder AddBalcony() { return this; }

    public IHouseBuilder AddPool() { return this; }

    public IHouseBuilderWithMandatoryMethod AddBarbecueStation() { return this; }

    public House Build() { return new House(); }
}

internal class Program
{
    static void Main(string[] args)
    {
        IHouseBuilder builder = new HouseBuilder();
        var house = builder
                        .AddBalcony()
                        .AddPool()
                        .AddBarbecueStation()
                        .Build();
    }
}

Another solution would be to check if the desired method has been called throughout the calling chain (inside the Build() method) and throw an exception if not:

public class HouseBuilder
{
    private mandatoryMethodHasBeenCalled = false;

    AddBarbecueStation()
    {
       mandatoryMethodHasBeenCalled = true;
    }
    
    public House Build()
    {
       if(!mandatoryMethodHasBeenCalled)
         throw new Exception("...");
    }
}

Set the final Build() method to a different interface, so that the client code is forced to call that method right before building the actual concrete class.

public class House
{
}

public interface IHouseBuilderWithMandatoryMethod
{
    House Build();
}

public interface IHouseBuilder
{
    IHouseBuilder AddBalcony();

    IHouseBuilder AddPool();

    IHouseBuilderWithMandatoryMethod AddBarbecueStation();
}

public class HouseBuilder: IHouseBuilder, IHouseBuilderWithMandatoryMethod
{
    public IHouseBuilder AddBalcony() { return this; }

    public IHouseBuilder AddPool() { return this; }

    public IHouseBuilderWithMandatoryMethod AddBarbecueStation() { return this; }

    public House Build() { return new House(); }
}

internal class Program
{
    static void Main(string[] args)
    {
        IHouseBuilder builder = new HouseBuilder();
        var house = builder
                        .AddBalcony()
                        .AddPool()
                        .AddBarbecueStation()
                        .Build();
    }
}

Set the final Build() method to a different interface, so that the client code is forced to call that method right before building the actual concrete class.

public class House
{
}

public interface IHouseBuilderWithMandatoryMethod
{
    House Build();
}

public interface IHouseBuilder
{
    IHouseBuilder AddBalcony();

    IHouseBuilder AddPool();

    IHouseBuilderWithMandatoryMethod AddBarbecueStation();
}

public class HouseBuilder: IHouseBuilder, IHouseBuilderWithMandatoryMethod
{
    public IHouseBuilder AddBalcony() { return this; }

    public IHouseBuilder AddPool() { return this; }

    public IHouseBuilderWithMandatoryMethod AddBarbecueStation() { return this; }

    public House Build() { return new House(); }
}

internal class Program
{
    static void Main(string[] args)
    {
        IHouseBuilder builder = new HouseBuilder();
        var house = builder
                        .AddBalcony()
                        .AddPool()
                        .AddBarbecueStation()
                        .Build();
    }
}

Another solution would be to check if the desired method has been called throughout the calling chain (inside the Build() method) and throw an exception if not:

public class HouseBuilder
{
    private mandatoryMethodHasBeenCalled = false;

    AddBarbecueStation()
    {
       mandatoryMethodHasBeenCalled = true;
    }
    
    public House Build()
    {
       if(!mandatoryMethodHasBeenCalled)
         throw new Exception("...");
    }
}
Source Link
Rzassar
  • 131
  • 4

Set the final Build() method to a different interface, so that the client code is forced to call that method right before building the actual concrete class.

public class House
{
}

public interface IHouseBuilderWithMandatoryMethod
{
    House Build();
}

public interface IHouseBuilder
{
    IHouseBuilder AddBalcony();

    IHouseBuilder AddPool();

    IHouseBuilderWithMandatoryMethod AddBarbecueStation();
}

public class HouseBuilder: IHouseBuilder, IHouseBuilderWithMandatoryMethod
{
    public IHouseBuilder AddBalcony() { return this; }

    public IHouseBuilder AddPool() { return this; }

    public IHouseBuilderWithMandatoryMethod AddBarbecueStation() { return this; }

    public House Build() { return new House(); }
}

internal class Program
{
    static void Main(string[] args)
    {
        IHouseBuilder builder = new HouseBuilder();
        var house = builder
                        .AddBalcony()
                        .AddPool()
                        .AddBarbecueStation()
                        .Build();
    }
}