工厂方法 [英] Factory method

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

问题描述

我有一个抽象类,以及一组继承自我的抽象

类的类。它是抽象的事实可能是无关紧要的。我的抽象类中有一个静态的

工厂方法,用于创建子类。我的子类中的

构造函数必须能够在我的基础

类中调用构造函数。对于我的抽象类中的工厂方法来调用我的子类上的构造函数

,子类中的构造函数必须是公共的(或

internal)。他们不能私有或受到保护。但是,难道不是吗?b $ b基本上打败了工厂的目的?我希望能够将所有这些构造函数设置为私有或类似的,以便有人在不使用Factory的情况下无法创建子类

实例。如果这都是在一个单独的DLL中,我/ b $ b假设我将被内部保存,但我们有一小组开发人员

从事项目工作这种分离真的不应该是必要的。


建议?下面的代码示例...


提前致谢。


Mark

公共抽象类MyBase

{

首先保护字符串;


公共MyBase(字符串优先)

{

this.first = first;

}


public static MyBase Factory(int stateId)

{

开关(stateId)

{

案例700:

返回新的MySubClass1(第一个);

默认:

抛出新的例外(Bogus代码!);

}

}

}

I have an abstract class, and a set of classes that inherit from my abstract
class. The fact that it is abstract is likely irrelevant. I have a static
factory method in my abstract class that creates subclasses. The
constructor in my subclasses must be able to call the constructor in my base
class. For the factory method in my abstract class to call the constructor
on my subclasses, the constructor in the subclass MUST be public (or
internal). They cannot be private or protected. However, doen''t that
basically defeat the purpose of factory? I''d love to be able to make all
these constructors private or similar so that someone can''t create subclass
instances without using the Factory. If this was all in a separate DLL, I
suppose I''d be saved by "internal", but we have small team of developers
working on projects where that type of seperation really shouldn''t be
necessary.

Suggestions? Code sample below ...

Thanks in advance.

Mark
public abstract class MyBase
{
protected string first;

public MyBase(string first )
{
this.first = first;
}

public static MyBase Factory(int stateId)
{
switch (stateId)
{
case 700:
return new MySubClass1(first);
default:
throw new Exception("Bogus code!");
}
}
}

推荐答案

嗯,是的,但是,基础课有任何知识

它''派生类失败了OO的目的。


-

-

真相,

James Curran

[以前的VC ++ MVP]


主页: www.noveltheory.com 工作: www.njtheater.com

博客: www.honestillusion.com 日工作: www.partsearch.com


" Mark" <马** @ nowhere.com>在消息中写道

新闻:uQ ************** @ TK2MSFTNGP09.phx.gbl ...
Well, yes, but then, having the base class have any knowledge at all
it''s derived classes defeats the purpose of OO.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Mark" <Ma**@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
我有一个抽象类,以及一组继承自
抽象类的类。它是抽象的事实可能是无关紧要的。我的抽象类中有一个静态的工厂方法来创建子类。我的子类中的
构造函数必须能够在
基类中调用构造函数。对于我的抽象类中的工厂方法来调用子类上的
构造函数,子类中的构造函数必须是公共的(或内部的
)。他们不能私有或受到保护。但是,那不是基本上打败了工厂的目的吗?我希望能够将所有这些构造函数设置为私有或类似的,以便有人在不使用Factory的情况下无法创建
子类实例。如果这一切都在一个单独的DLL中,我想我会被内部保存,但我们有一小组开发人员
从事那些类型的分离真的不应该做的项目是必要的。

建议?下面的代码示例...

提前致谢。

$

公共抽象类MyBase
{
受保护的字符串首先;

公共MyBase(字符串优先)
{
this.first = first;
}
公共静态MyBase Factory(int stateId)
{
开关(stateId)
{案例700:
返回新的MySubClass1(第一个);
默认:
抛出新的例外(Bogus代码!);
}
}
}
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static
factory method in my abstract class that creates subclasses. The
constructor in my subclasses must be able to call the constructor in my base class. For the factory method in my abstract class to call the constructor on my subclasses, the constructor in the subclass MUST be public (or
internal). They cannot be private or protected. However, doen''t that
basically defeat the purpose of factory? I''d love to be able to make all
these constructors private or similar so that someone can''t create subclass instances without using the Factory. If this was all in a separate DLL, I
suppose I''d be saved by "internal", but we have small team of developers
working on projects where that type of seperation really shouldn''t be
necessary.

Suggestions? Code sample below ...

Thanks in advance.

Mark
public abstract class MyBase
{
protected string first;

public MyBase(string first )
{
this.first = first;
}

public static MyBase Factory(int stateId)
{
switch (stateId)
{
case 700:
return new MySubClass1(first);
default:
throw new Exception("Bogus code!");
}
}
}



check这里的定义

http:// www。 dofactory.com/Patterns/PatternAbstract.aspx

-

HTH


Ollie Riches
http://www.phoneanalyser.net


免责声明:意见表达d在这个论坛中是我自己的,而不是我雇主的b $ b代表。

我不代表我的雇主回答问题。我只是一名程序员

帮助程序员。


" Mark" <马** @ nowhere.com>在消息中写道

新闻:uQ ************** @ TK2MSFTNGP09.phx.gbl ...
check out the definition here

http://www.dofactory.com/Patterns/PatternAbstract.aspx
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a programmer
helping programmers.

"Mark" <Ma**@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
我有一个抽象类,以及一组继承自
抽象类的类。它是抽象的事实可能是无关紧要的。我的抽象类中有一个静态的工厂方法来创建子类。我的子类中的
构造函数必须能够在
基类中调用构造函数。对于我的抽象类中的工厂方法来调用子类上的
构造函数,子类中的构造函数必须是公共的(或内部的
)。他们不能私有或受到保护。但是,那不是基本上打败了工厂的目的吗?我希望能够将所有这些构造函数设置为私有或类似的,以便有人在不使用Factory的情况下无法创建
子类实例。如果这一切都在一个单独的DLL中,我想我会被内部保存,但我们有一小组开发人员
从事那些类型的分离真的不应该做的项目是必要的。

建议?下面的代码示例...

提前致谢。

$

公共抽象类MyBase
{
受保护的字符串首先;

公共MyBase(字符串优先)
{
this.first = first;
}
公共静态MyBase Factory(int stateId)
{
开关(stateId)
{案例700:
返回新的MySubClass1(第一个);
默认:
抛出新的例外(Bogus代码!);
}
}
}
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static
factory method in my abstract class that creates subclasses. The
constructor in my subclasses must be able to call the constructor in my base class. For the factory method in my abstract class to call the constructor on my subclasses, the constructor in the subclass MUST be public (or
internal). They cannot be private or protected. However, doen''t that
basically defeat the purpose of factory? I''d love to be able to make all
these constructors private or similar so that someone can''t create subclass instances without using the Factory. If this was all in a separate DLL, I
suppose I''d be saved by "internal", but we have small team of developers
working on projects where that type of seperation really shouldn''t be
necessary.

Suggestions? Code sample below ...

Thanks in advance.

Mark
public abstract class MyBase
{
protected string first;

public MyBase(string first )
{
this.first = first;
}

public static MyBase Factory(int stateId)
{
switch (stateId)
{
case 700:
return new MySubClass1(first);
default:
throw new Exception("Bogus code!");
}
}
}



I今天也一直在寻找工厂(作为缺乏

虚拟构造函数的解决方案)。当班级

没有使用参数化构造函数时,似乎工厂效果最好,但是有一个虚拟方法来初始化新实例。这创建了一个两步过程:


MyClass x = new MyClass();

x.Initialize(...);


第一行意味着每个

级别你还需要一个专门的工厂,好吧。


Ergo,我已经设计了以下方案,有其自身的缺点...


我定义了一个界面:


公共接口IConstructable

{

void构造函数(params object [] Params);

}


(如果是接口肯定会很好可以指定构造函数。)


以下静态工厂方法:


公共静态IConstructable

新< br $>


System.Type什么



params object [] Params



{

IConstructable result = null;


if(What.GetConstructors()。Length> 1)

{

throw(new System.ArgumentException(string.Format)



"类型{0}包含的内容超过默认构造函数。"



什么.Name

)));

}


System.Reflection.ConstructorInfo con = What.GetConstructor(new

System.Type [] {});


if(con == null)

{

throw(new System.ArgumentException(string.Format



"没有为类型{0}找到无参数构造函数。)



What.Name

)));

}


if(!con .IsPublic)

{

throw(new System.ArgumentException(string.Format



" Did找不到类型为

{0}的公共无参数构造函数。"



What.Name

)));

}


result =(IConstructable)con.Invoke(new object [] {});


result.Constructor(Params);


return(result);
}


然后用它上课:


A类:IConstructable

{

private string str;


public virtual void

构造函数



params object [] Params



{

this.str =(string)Params [0]; < br $>
}


公共覆盖字符串

ToString





{

返回(this.str);

}

}


并制作一个实例(这是一个不好的部分):


A a =(A)新的(typeof(A),A和);


这个工厂方法适用于任何实现IConstructable的类,

就好像是一个虚拟构造函数。但是类的构造函数方法将需要对参数进行类型检查,这很糟糕。


也许我可以让它使用一个结构来实现参数,嗯...

注意_ideally_该类不会定义构造函数,最多可能会定义一个公共无参数构造函数。


再一次,这是为了解决缺乏虚拟构造函数的问题,所以是的,一个不知情的用户可以在课堂上调用new并搞砸了,但是我'我仍然在工作'不是

I''ve been looking at factories today too (as a work-around for the lack of
virtual constructors). It seems that factories work best when the class
doesn''t use a parameterized constructor, but has a virtual method to
initialize the new instance. This creates a two-step process:

MyClass x = new MyClass() ;
x.Initialize ( ... ) ;

That first line means that you still need a specialized factory for each
class, yuck.

Ergo, I have devised the following scheme, which has its own shortcomings...

I defined an interface:

public interface IConstructable
{
void Constructor ( params object[] Params ) ;
}

(It sure would be nice if an interface could specify a constructor.)

And the following static factory method:

public static IConstructable
New
(
System.Type What
,
params object[] Params
)
{
IConstructable result = null ;

if ( What.GetConstructors().Length > 1 )
{
throw ( new System.ArgumentException ( string.Format
(
"Type {0} contains more than the default constructor."
,
What.Name
) ) ) ;
}

System.Reflection.ConstructorInfo con = What.GetConstructor ( new
System.Type[] {} ) ;

if ( con == null )
{
throw ( new System.ArgumentException ( string.Format
(
"Did not find a parameterless constructor for type {0}."
,
What.Name
) ) ) ;
}

if ( !con.IsPublic )
{
throw ( new System.ArgumentException ( string.Format
(
"Did not find a public parameterless constructor for type
{0}."
,
What.Name
) ) ) ;
}

result = (IConstructable) con.Invoke ( new object[] {} ) ;

result.Constructor ( Params ) ;

return ( result ) ;
}

Then to use it for a class:

class A : IConstructable
{
private string str ;

public virtual void
Constructor
(
params object[] Params
)
{
this.str = (string) Params [ 0 ] ;
}

public override string
ToString
(
)
{
return ( this.str ) ;
}
}

And to make an instance thereof (this is the fugly part):

A a = (A) New ( typeof(A) , "A" ) ;

This one factory method works with any class that implements IConstructable,
as if ''twere a virtual constructor. But the class''s Constructor method will
need to do type checking on the parameters, yuck.

Perhaps I can have it use a struct for the parameters, hmm...

Note that _ideally_ the class would not define a constructor, at most it may
define a public parameterless constructor.

Again, this was to work-around the lack of virtual constructors, so yes, an
uninformed user may call new on the class and mess things up, but I''m still
working on''t.


这篇关于工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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