构建器模式中构建器的创建
我想澄清我对构建器模式的使用,特别是如何创建构建器的类型。在示例中,它只是假设构建器的类型并创建它。但是,我在“ChartBuilderFactory”类中创建了一个 CreateBuilder 方法,该方法根据传递给它的某个对象返回构建器的类型。这是最好的方法吗?我已经发布了我的示例代码,并且希望有更好的设计的替代方案和建议。
class Program
{
static void Main(string[] args)
{
DataObject data = new DataObject("basic", 2);
ChartDirector director = new ChartDirector();
ChartBuilderFactory builderFactory = new ChartBuilderFactory();
ChartBuilder builder = builderFactory.CreateChartBuilder(data);
director.Construct(builder);
Console.WriteLine(builder.GetChart());
Console.ReadKey();
}
}
class ChartBuilderFactory
{
public ChartBuilder CreateChartBuilder(DataObject data)
{
ChartBuilder builder;
if (data.DataType == "basic")
builder = new ChartBuilderBasic(data);
else if (data.DataType == "vector")
builder = new ChartBuilderVector(data);
else
builder = null;
return builder;
}
}
class ChartDirector
{
public void Construct(ChartBuilder builder)
{
builder.CreateChart();
builder.LoadData();
}
}
abstract class ChartBuilder
{
protected string myChartObject = "";
protected DataObject data;
public ChartBuilder(DataObject data)
{
this.data = data;
}
abstract public void CreateChart();
abstract public void LoadData();
public string GetChart() { return myChartObject; }
}
class ChartBuilderBasic : ChartBuilder
{
public ChartBuilderBasic(DataObject data)
: base(data) { }
public override void CreateChart()
{
myChartObject = "MyBasicChart";
}
public override void LoadData()
{
myChartObject += Convert.ToString(data.Data);
}
}
class ChartBuilderVector : ChartBuilder
{ /** Alternative Implementation here */ }
class DataObject
{
/** Constructor and private member variables here */
public string DataType { get { return this.dataType; } }
public int Data { get { return this.data; } }
}
I want to clarify my use of the builder pattern, in particular how the type of builder is created. In examples, it just assumes the type of builder and creates it. However, I created a CreateBuilder
method in a 'ChartBuilderFactory' class which returns the type of builder based on some object that was passed to it. Is this the best way of doing this? I've posted my sample code and would appreciate alternatives and suggestions for better design.
class Program
{
static void Main(string[] args)
{
DataObject data = new DataObject("basic", 2);
ChartDirector director = new ChartDirector();
ChartBuilderFactory builderFactory = new ChartBuilderFactory();
ChartBuilder builder = builderFactory.CreateChartBuilder(data);
director.Construct(builder);
Console.WriteLine(builder.GetChart());
Console.ReadKey();
}
}
class ChartBuilderFactory
{
public ChartBuilder CreateChartBuilder(DataObject data)
{
ChartBuilder builder;
if (data.DataType == "basic")
builder = new ChartBuilderBasic(data);
else if (data.DataType == "vector")
builder = new ChartBuilderVector(data);
else
builder = null;
return builder;
}
}
class ChartDirector
{
public void Construct(ChartBuilder builder)
{
builder.CreateChart();
builder.LoadData();
}
}
abstract class ChartBuilder
{
protected string myChartObject = "";
protected DataObject data;
public ChartBuilder(DataObject data)
{
this.data = data;
}
abstract public void CreateChart();
abstract public void LoadData();
public string GetChart() { return myChartObject; }
}
class ChartBuilderBasic : ChartBuilder
{
public ChartBuilderBasic(DataObject data)
: base(data) { }
public override void CreateChart()
{
myChartObject = "MyBasicChart";
}
public override void LoadData()
{
myChartObject += Convert.ToString(data.Data);
}
}
class ChartBuilderVector : ChartBuilder
{ /** Alternative Implementation here */ }
class DataObject
{
/** Constructor and private member variables here */
public string DataType { get { return this.dataType; } }
public int Data { get { return this.data; } }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您实际上想要的是使用工厂模式来创建生成器类。 Director 并不创建 Builder,但对它们具有依赖关系。
看看这个网站,它可以很好地洞察模式并使用一些真实的例子。
[编辑:基于修订后的问题]
现在您已经在工厂内创建了构建器,您就可以开始了。如果您想删除正确构建器的条件/发现,有许多选项,具体取决于系统的大小和复杂性。
最后两个选项可能涉及反射(使用 Activator 类)以创建 Builder 类(或一些依赖注入容器,例如 unity),但确切的机制至少封装在
ChartBuilderFactory
中,理想情况下,如果您使用依赖注入(控制反转),您将分离构建从对象的使用方式来看。如果你将其集中化,它就会变得更容易控制,甚至在以后需要时进行更改。更不用说让它更容易测试了。
I think you actually want is to use the Factory pattern to create the Builder classes. The Director does not create the Builder but has the dependency on them.
Have a look at this site which gives a good insight into patterns and uses some real example.
[EDIT : based on revised question]
Now that you have the creating of the Builders inside a factory you are set to go. If you want to remove the condition / finding of the correct builder there are a number of options depending on the size and complexity of the system.
The last 2 options might involve reflection (using the Activator class) in order to create the Builder class (or some Dependency Injection containers such as unity), but the exact mechanics are at least encapsulated inside the
ChartBuilderFactory
Ideally if you use Dependency Injection (Inversion of Control) you are separating the construction of the objects from the way they are used. And if you centralize this it becomes easier to control and even change later if you need to. Not to mention making it easier to test.