.NET Framework中的构造函数与工厂 [英] Constructor vs. Factory in .NET Framework

查看:120
本文介绍了.NET Framework中的构造函数与工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是有关.net框架使用模式的文章。我不知道我明白下面摘录中的粗体部分。这是否意味着如果您更改创建对象的细节,您(可能)会更改构造函数参数?

Below is an article about .net framework's use of patterns. I'm not sure I understand the bolded part in the excerpt below. Is it implying if you change the details of creating the object, you (might) change the constructor arguments?


框架,你
可以获得一个新的实例的结构或类
,而不需要自己调用它的构造函数。
System.Convert类包含一系列静态的
方法,这样一来工作。例如,要将整数
转换为布尔值,可以调用
Convert.ToBoolean并传入整数。如果整数为非零,则该方法调用的
返回值是一个新的布尔值
设置为true,否则为
false。 Convert类为您创建具有正确值的
布尔值。其他类型
转换方法的工作方式类似。 Int32和Double上的Parse
方法返回新的实例
将这些对象设置为适当的值
,只给出一个字符串。

There are many cases in the Framework where you can obtain a new instance of a struct or class without calling its constructor yourself. The System.Convert class contains a host of static methods that work like this. To convert an integer to a Boolean, for example, you can call Convert.ToBoolean and pass in the integer. The return value of this method call is a new Boolean set to "true" if the integer was non-zero and "false" otherwise. The Convert class creates the Boolean for you with the correct value. Other type conversion methods work similarly. The Parse methods on Int32 and Double return new instances of those objects set to the appropriate value given only a string.

创建新对象实例的策略是
,称为Factory模式。而不是调用
对象的构造函数,您可以要求对象
工厂为您创建实例。这样,
工厂类可以隐藏
对象创建的复杂性(像如何从
解析一个字符串)。如果要更改
的详细信息创建对象,则只需要更改
工厂本身; 您不需要更改代码中每个
的单个位置,其中构造函数为

This strategy for creating new object instances is known as a Factory pattern. Rather than invoking the object's constructor, you can ask the object factory to create the instance for you. That way, the factory class can hide the complexity of object creation (like how to parse a Double out of a string). If you wanted to change the details of creating the object, you'd only have to change the factory itself; you would not have to change every single place in the code where the constructor is called.

来自: http://msdn.microsoft.com/ en-us / magazine / cc188707.aspx

推荐答案

我实际上认为他们提供的例子是不一定很好的例子。

I actually think that the examples they've provided are not necessarily great examples.

当您构建类时,Factory模式在.NET中变得更加有用。例如,查看 WebRequest class

The Factory pattern becomes more useful in .NET when you're constructing classes. For example, look at the WebRequest class.

此类通常通过调用实例化:

This class is normally instantiated by calling:

WebRequest request = WebRequest.Create(url);

WebRequest.Create 方法使用Factory模式。根据URL的类型,它将创建一个不同类型(子类)的WebRequest。例如,如果你传递一个 http:// url,你将会创建一个 HttpWebRequest 实例 - 一个 ftp:// URL将创建一个 FtpWebRequest

The WebRequest.Create method uses the Factory pattern. Depending on the type of URL, it will create a different type (subclass) of WebRequest. If you pass it an http:// url, for example, you will actually create an HttpWebRequest instance - an ftp:// URL will create an FtpWebRequest.

通过在这里使用Factory模式,稍后可以添加更多的URL类型,而不需要更改客户端的任何代码 - 只需传递不同的URL(作为字符串),并获取一个新对象。

By using the Factory pattern here, more URL types can be added later without changing any code on the client side - you just pass in the different URL (as a string), and get a new object.

这篇关于.NET Framework中的构造函数与工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆