2012-05-28 21 views
0

尝试创建一个简单的noddy示例,以便我可以使用流畅接口构建我的下一个项目。 我需要为我的dll的用户提供一个直观的逐步构建类的方式。流畅的流畅界面如何在开始时隐藏所有方法,但只有一个?

问题。 我想用户只能看到初始化时,第一次启动然后step1然后step2 然后结束?我该怎么做?

这是我的尝试,只要你把“。”你看到了一切,然后他们隐藏起来。

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

}

建设逐步流利接口的任何建议或好的链接?

回答

3

您可以将Initialize()转换为静态工厂方法,并使构造函数为私有。然后你创建代码如下所示:

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

第二个想法,将是使该接口实现明确的。然后,您只能看到当时正在处理的界面的方法。

更新

下面是这两种方法的示例代码。接口和你的问题完全一样。

例与工厂方法:

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

感谢您的回复,听起来good.How将是改变接口的其余喜?这是否意味着我的IStep1初始化会消失? – user9969

+0

刚刚尝试过,但之后,我看到everything.I认为我错过了明显的。任何时间使用我提供的接口的小例子。很多谢谢 – user9969

+0

感谢现在很多都清楚。 – user9969