我们还可以将其称为构建器模式吗?
这是我的工厂方法类。
public class AFactory : FactoryBase {
public override ProductBase ProduceProdut() {
return new SuperCoolProduct();
}
}
这是我的建造者课程。
public class ABuilder : BuilderBase {
ProductBase product = new SuperCoolProduct()
public override void BuildStep1() {
//Do Something (e.g. product.Add() )
}
public override void BuildStep2() {
//Do some more...(e.g. product.AddMore() )
}
public override ProductBase GetFinalProduct() {
return product;
}
}
我的问题如下〜1
)构建器是否必须公开Director可以调用的一些方法来组合产品?
这个类还可以被视为构建器类吗?或者,我们必须将其称为工厂类吗?我认为我们不能再将下面的此类称为构建器,它现在是一个工厂类。
public class ABuilder : BuilderBase {
ProductBase product = new SuperCoolProduct();
public override ProductBase GetFinalProduct() {
BuildStep1();
BuildStep2();
return product;
}
private void BuildStep1() {
//Do Something (e.g. product.Add() )
}
private void BuildStep2() {
//Do some more...(e.g. product.AddMore() )
}
}
2)下面的代码是否是结合“构建器”模式和“工厂方法”模式的一个很好的示例?
public class AFactory : FactoryBase {
BuilderBase builder;
public AFactory(BuilderBase builder){
this.builder = builder;
}
public override ProductBase ProduceProdut() {
this.builder.BuildStep1();
this.builder.BuildStep1();
return this.builder.GetFinalProduct();
}
}
Here is my factory-method class.
public class AFactory : FactoryBase {
public override ProductBase ProduceProdut() {
return new SuperCoolProduct();
}
}
Here is my builder class.
public class ABuilder : BuilderBase {
ProductBase product = new SuperCoolProduct()
public override void BuildStep1() {
//Do Something (e.g. product.Add() )
}
public override void BuildStep2() {
//Do some more...(e.g. product.AddMore() )
}
public override ProductBase GetFinalProduct() {
return product;
}
}
My questions are the following ~
1) Does the builder has to expose some methods that the Director can call for compositing the product?
Can this class still be considered as a builder class? Or, Do we have to call it as a factory class? I think that we can't call this class below as a builder anymore and it's a factory class now..
public class ABuilder : BuilderBase {
ProductBase product = new SuperCoolProduct();
public override ProductBase GetFinalProduct() {
BuildStep1();
BuildStep2();
return product;
}
private void BuildStep1() {
//Do Something (e.g. product.Add() )
}
private void BuildStep2() {
//Do some more...(e.g. product.AddMore() )
}
}
2) Would the code below be a good example of combining "Builder" pattern and "Factory Method" pattern?
public class AFactory : FactoryBase {
BuilderBase builder;
public AFactory(BuilderBase builder){
this.builder = builder;
}
public override ProductBase ProduceProdut() {
this.builder.BuildStep1();
this.builder.BuildStep1();
return this.builder.GetFinalProduct();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于工厂:更常见的是调用方法
CreateXXX
As for factories: It's more common to call the methods
CreateXXX