构造函数与工厂在.NET框架 [英] Constructor vs. Factory in .NET Framework

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

问题描述

下面是关于.NET框架的使用模式的文章。我不知道我的理解下面的摘录粗体部分。难道这意味着如果你改变创建对象的细节,你(可能)改变构造函数的参数?

  

有许多情况下框架,你   能获得一个结构或类的新实例   没有自己调用构造函数。该   System.Convert类包含了一系列静态   那这样的工作方法。要转换为整数   一个布尔值,例如,你可以调用   Convert.ToBoolean并传入整数。该   这种方法调用的返回值是一个新的布尔   设置为真,如果是整数非零和   假,否则。转换类创建   你用正确的值Boolean。其他类型   转换方法与此类似。解析   上的Int32和双方法返回新实例   设置为适当的值的那些对象的   仅给出一个字符串。

     

本策略创建新的对象实例是   被称为工厂模式。而不是调用   该对象的构造函数,你可以问的对象   工厂创建为您的实例。那样,   工厂类可以隐藏的复杂性   创建对象(比如如何解析双出   字符串)。如果你想改变的细节   创建对象,你只需要改变   工厂本身; 您就不必改变每   在code,其中的构造是一个地方   调用。

来源: http://msdn.microsoft.com/en-us/杂志/ cc188707.aspx

解决方案

其实,我觉得,他们已经提供的例子不一定是很好的例子。

当你构建类工厂模式成为.NET更加有用。例如,看一下 WebRequest类

本类通常是通过调用实例:

 的WebRequest请求= WebRequest.Create(URL);
 

的<一个href="http://msdn.microsoft.com/en-us/library/system.net.webrequest.create.aspx">WebRequest.Create方法使用工厂模式。根据URL的类型,将创建的WebRequest的不同类型(子类)。如果你把它传递一个的http:// URL,例如,你会真正创建的HttpWebRequest 实例 - 一个的ftp:// URL将创建一个的FtpWebRequest

利用这里的工厂模式,多类型的URL可以在以后添加不改变客户端上的任何code - 你只是传递了不同的URL(作为一个字符串),并得到一个新的对象

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?

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.

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.

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

解决方案

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

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

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.

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框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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