了解C#中的Builder模式
作者:互联网
Builder模式的定义:
Builder模式将复杂对象的规范与其实际分开
施工.相同的构造过程可以创建不同的表示.
好吧,我有像这样的Builder Pattern的代码示例
class Director
{
// Build a Product from several parts
public void Construct(IBuilder builder)
{
builder.BuildPartA();
builder.BuildPartB();
builder.BuildPartB();
}
}
interface IBuilder
{
void BuildPartA();
void BuildPartB();
Product GetResult();
}
class Builder1 : IBuilder
{
private Product product = new Product();
public void BuildPartA()
{
product.Add("PartA ");
}
public void BuildPartB()
{
product.Add("PartB ");
}
public Product GetResult()
{
return product;
}
}
class Builder2 : IBuilder
{
private Product product = new Product();
public void BuildPartA()
{
product.Add("PartX ");
}
public void BuildPartB()
{
product.Add("PartY ");
}
public Product GetResult()
{
return product;
}
}
class Product
{
List<string> parts = new List<string>();
public void Add(string part)
{
parts.Add(part);
}
public void Display()
{
Console.WriteLine("\nProduct Parts -------");
foreach (string part in parts)
Console.Write(part);
Console.WriteLine();
}
}
public class Client
{
public static void Main()
{
// Create one director and two builders
Director director = new Director();
IBuilder b1 = new Builder1();
IBuilder b2 = new Builder2();
// Construct two products
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Display();
director.Construct(b2);
Product p2 = b2.GetResult();
p2.Display();
Console.Read();
}
}
现在,我在上面的代码中做了一些更改.以下代码是否仍然代表构建器模式?
class Director
{
public void Construct(IBuilder builder)
{
builder.BuildPartA();
builder.BuildPartB();
builder.GetResult();
}
}
interface IBuilder
{
void BuildPartA();
void BuildPartB();
void GetResult();
}
class Builder1 : IBuilder
{
List<string> product = new List<string>();
public void BuildPartA()
{
product.Add("PartA ");
}
public void BuildPartB()
{
product.Add("PartB ");
}
public void GetResult()
{
foreach (var p in product)
{
Console.WriteLine("the product created is :" + p);
}
}
}
class Builder2 : IBuilder
{
List<string> product = new List<string>();
public void BuildPartA()
{
product.Add("PartX ");
}
public void BuildPartB()
{
product.Add("PartY ");
}
public void GetResult()
{
foreach (var p in product)
{
Console.WriteLine("the product created is :" + p);
}
}
}
public class Client
{
public static void Main()
{
Director director = new Director();
IBuilder b1 = new Builder1();
IBuilder b2 = new Builder2();
director.Construct(b1);
director.Construct(b2);
Console.Read();
}
}
注意:
I have removed the product class from the second sample code.
我的问题是,创建一个处理所有对象并以有序方式调用方法的类使它成为Builder模式?只是想确保我理解Builder Pattern的概念.
提前致谢
更新1
我真的不明白为什么第二个样本不是Builder模式.让我创建第二个例子,这对我来说很容易理解,并且对其他人来说很容易帮助我理解.以下是代码.
interface IRoutine {
void WakeUp();
void GoToSchool();
void ListenToMusic();
void Sleep();
}
class person1 : IRoutine
{
public void GoToSchool()
{
Console.WriteLine("going to school");
}
public void ListenToMusic()
{
Console.WriteLine("litening to music");
}
public void Sleep()
{
Console.WriteLine("tring to sleep");
}
public void WakeUp()
{
Console.WriteLine("good morning !!!");
}
}
class person2 : IRoutine
{
public void GoToSchool()
{
Console.WriteLine("going to school");
}
public void ListenToMusic()
{
Console.WriteLine("litening to music");
}
public void Sleep()
{
Console.WriteLine("tring to sleep");
}
public void WakeUp()
{
Console.WriteLine("good morning !!!");
}
}
class Builder {
public void Construct(IRoutine routine) {
routine.WakeUp();
routine.GoToSchool();
routine.ListenToMusic();
routine.Sleep();
}
}
class Client {
static void Main() {
Builder builder = new Builder();
IRoutine r1 = new person1();
IRoutine r2 = new person2();
builder.Construct(r1);
builder.Construct(r2);
Console.Read();
}
}
最近的例子是我提供了Builder模式吗?如果不是那么为什么它不是Builder Pattern,请帮助我理解.
解决方法:
您的后一个代码段不是构建器模式的示例.创建最终对象的步骤是模式的关键部分,通过删除该步骤,构建器类有效地成为实际的构造(可变)对象.换句话说,如果string是可变的,那么就不存在StringBuilder,你只需要使用一个假设的string.Append方法来改变结束对象.或者,如果StringBuilder返回除构造字符串以外的任何内容,则不会将其称为StringBuilder.
为了证明在实践中存在建造者,我希望它能够:
>提供一个用于构建不可变对象的接口,然后“冻结”它,即返回一个不可变的构建实例(StringBuilder就是一个很好的例子).
>提供一个稍微更清晰(例如流畅)的接口来实例化一个对象,比较一下,比如拥有大量的构造函数参数.但是,如果最终对象是可变的,则更难以证明这一点,因为无论如何都可以设置单个属性.
一些示例使用构建器为构建对象提供额外的抽象层(即允许以相同的方式构建不同的具体类型),然后将抽象构建器传递给使用它构建对象的“director”,但我发现这一步与模式无关.我甚至更进一步说,有几种方法的抽象构建器感觉像反模式,因为它们将每个具体对象的构造与同一构建过程联系起来.为了更好地理解我的观点,请考虑如何在给定here的示例中实现BoatBuilder.
标签:c,design-patterns,builder-pattern 来源: https://codeday.me/bug/20190608/1198723.html