Progressing fluent interfaces how can I hide all methods but one at the start?

百般思念 提交于 2019-12-25 08:13:38

问题


Trying to create a simple noddy example so that I can build my next project using fluent interfaces. I need to provide the user of my dll with an intuitive step by step way of building a class.

Question. I would like the user only to see Initialise when the first start then step1 then step2 and then end?How can I do it?

This is my attempt which As soon as you put the "." you see everything,then they hide.

class Program
{
    static void Main(string[] args)
    {
        IEnd builder = new StepBuilder()
            .Initialize()
            .LoadStuff()
            .Validate()
            .End();

    }
}
 public class StepBuilder : IInitialize,
                              IStep1,
                              IStep2,
                              IStep3,
                              IEnd
 {
     public IStep1 Initialize()
     {
         return this;
     }

     public IStep2 LoadStuff()
     {
         return this; ;
     }

     public IStep3 Validate()
     {
         return this;
     }

     public IEnd End()
     {
         return this;
     }

     public bool Save()
     {
         return true;
     }
 }

public class Step
{
    public string Name { get; set; }
    public string Category { get; set; }
    public decimal Price { get; set; }
}

public interface IInitialize
{
    IStep1 Initialize();
}

public interface IStep1 
{
    IStep2 LoadStuff();
}

public interface IStep2 
{
    IStep3 Validate ();
}

public interface IStep3
{
    IEnd End();
}
public interface IEnd 
{
    bool Save();
}

}

any suggestions or good link on building progressive fluent interfaces?


回答1:


You could turn Initialize() into a static factory method, and make the constructor private. Then your creation code would look like:

    IEnd builder = StepBuilder.Initialize()
         .LoadStuff()
         .Validate()
         .End(); 

A second idea, would be to make the interface implementations explicit. Then you'd only see the methods for the interface you're dealing with at the time.

Update

Here's example code for both methods. The interfaces are exactly the same as in your question.

Example with factory method:

class Program
{
    static void Main(string[] args)
    {
        IEnd builder = StepBuilder
            .Initialize()
            .LoadStuff()
            .Validate()
            .End();

    }
}

public class StepBuilder : IInitialize,
                             IStep1,
                             IStep2,
                             IStep3,
                             IEnd
{
    private StepBuilder()
    {
    }

    public static IStep1 Initialize()
    {
        var builder = new StepBuilder();
        //do initialisation stuff
        return builder;
    }

    public IStep2 LoadStuff()
    {
        return this; 
    }

    public IStep3 Validate()
    {
        return this;
    }

    public IEnd End()
    {
        return this;
    }

    public bool Save()
    {
        return true;
    }
}

Example with explicit interface implementations:

class Program
{
    static void Main(string[] args)
    {
        IEnd builder = new StepBuilder()
            .Initialize()
            .LoadStuff()
            .Validate()
            .End();

    }
}

public class StepBuilder : IInitialize,
                             IStep1,
                             IStep2,
                             IStep3,
                             IEnd
{
    public IStep1 Initialize()
    {
        return this;
    }

    public IStep2 IStep1.LoadStuff()
    {
        return this; 
    }

    public IStep3 IStep2.Validate()
    {
        return this;
    }

    public IEnd IStep3.End()
    {
        return this;
    }

    public bool IEnd.Save()
    {
        return true;
    }
}


来源:https://stackoverflow.com/questions/10779736/progressing-fluent-interfaces-how-can-i-hide-all-methods-but-one-at-the-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!