English 中文(简体)
进步流利的界面 我怎么能隐藏所有的方法 除了一个在开始时?
原标题:Progressing fluent interfaces how can I hide all methods but one at the start?

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?

这是我的尝试 一旦你把"。" 你看到一切,然后他们隐藏。

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();
时 时

时 时

关于逐步建立流利界面的建议或良好联系?

最佳回答

您可以将 Intialize () 转换为静态工厂方法, 并让构建器成为私有。 然后您的创建代码将看起来像 :

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

第二个想法是让接口执行清晰化。然后您只看到您当时正在处理的接口的方法 。

<强 > 更新

s 两种方法的示例代码。接口与您的问题完全相同。

工厂方法实例:

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;
    }
}

具有明确接口执行的示例:

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;
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签