OOP设计问题 [英] OOP design question

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

问题描述

我有一个类(B)派生自另一个类(A) - 它提供了该基类的

特化。


它转基类(A)需要可替换。也就是说,我需要能够插入新的实现(保留相同的

界面)。


我不能更改A或B的源代码,它需要在一个库中运送
。但是客户应该能够插入新的基类。


除了将代理类作为A委托给插件接口之外,

我看不到任何其他方式。而且我真的不喜欢写代理

课程。


任何人都有更好的想法吗?


谢谢。

I have a class (B) that derives from another class (A) - it provides a
specialization of that base class.

It turns out that the base class (A) needs to be replaceable. That is, I
need to be able to plug in new implementations (retaining the same
interface).

I cannot change the source code of A or B, it needs to be in a library that
is shipped out. But customers should be able to ''plug-in'' new base classes.

Other than having a proxy class as A that delegates to a plug-in interface,
I can''t see any other way of doing it. And I really don''t like writing proxy
classes.

Anyone have any better ideas?

Thanks.

推荐答案

John,


在这种情况下,而不是通过提供专业化派生,

您可能想要提供一个已实现的接口,然后根据需要调用

。然后,所有交互都是针对您运送的类来完成的,其中专门化是插件。这样,如果你添加更多

的功能,你就可以换掉那个类。


当然,这可以在不破坏现有代码的情况下完成(new

实现只使用新类,而不是旧类。另外,如果您需要更多专业课程,请定义一个新界面,并且要求实施这一点。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" John Wood" < j@ro.com>在留言中写道

新闻:OC ************** @ TK2MSFTNGP12.phx.gbl ...
John,

In this case, instead of providing specialization through derivation,
you might want to supply an interface that is implemented, and then call
those as needed. Then, all interaction is done against the class that you
ship, where the specializations are the plug-ins. This way, if you add more
functionality, you just swap out that class.

Of course, this could be done without breaking existing code (new
implementations just use the new class, not the old one). Also, if you
require more from your specialized classes, define a new interface and
require that to be implemented.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Wood" <j@ro.com> wrote in message
news:OC**************@TK2MSFTNGP12.phx.gbl...
我有一节课( B)派生自另一个类(A) - 它提供了该基类的专门化。

事实证明基类(A)需要是可替换的。也就是说,我需要能够插入新的实现(保留相同的
接口)。

我无法更改A或B的源代码,它需要在库中
运出。但是客户应该能够插入新的基础
类。
除了代理一个代理插入
接口的代理类之外,我可以''看到任何其他方式。而且我真的不喜欢写
代理课程。

任何人都有更好的想法吗?

谢谢。
I have a class (B) that derives from another class (A) - it provides a
specialization of that base class.

It turns out that the base class (A) needs to be replaceable. That is, I
need to be able to plug in new implementations (retaining the same
interface).

I cannot change the source code of A or B, it needs to be in a library that is shipped out. But customers should be able to ''plug-in'' new base classes.
Other than having a proxy class as A that delegates to a plug-in interface, I can''t see any other way of doing it. And I really don''t like writing proxy classes.

Anyone have any better ideas?

Thanks.



是的,但问题是该''插入''接口如何暴露给应用程序的

使用者。你看,专门的类必须直接暴露界面的每个方法,以及它自己提供的新方法。


如果没有创建实现接口的代理类,但是将每个方法委托给你插件的实现,我看不出你怎么能

插件实现而不更改源代码。


理解我的意思?


Nicholas Paldino [。 NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:eK ************** @ TK2MSFTNGP12.phx.gbl ...
Yes, but the problem is how that ''plugged-in'' interface is exposed to the
consumer of the application. You see, the specialized class has to expose
every method of the interface directly, as well as the new methods it
provides itself.

Without creating a proxy class that implements the interface but delegates
each method to the implementation you ''plug-in'', I can''t see how you can
plug-in an implementation without changing the source code.

Understand what I mean?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eK**************@TK2MSFTNGP12.phx.gbl...
John,
在这种情况下,您可能希望提供已实现的接口,然后根据需要调用
,而不是通过派生提供专业化。然后,所有交互都是针对您发布的类进行的,其中特化是插件。这样,如果你添加
更多的功能,你只需换掉那个类。

当然,这可以在不破坏现有代码的情况下完成(新的
实现只需使用新的上课,而不是旧的)。此外,如果您需要更多专业课程,请定义新界面并要求实施。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

John Wood < j@ro.com>在消息中写道
新闻:OC ************** @ TK2MSFTNGP12.phx.gbl ...
John,

In this case, instead of providing specialization through derivation,
you might want to supply an interface that is implemented, and then call
those as needed. Then, all interaction is done against the class that you
ship, where the specializations are the plug-ins. This way, if you add more functionality, you just swap out that class.

Of course, this could be done without breaking existing code (new
implementations just use the new class, not the old one). Also, if you
require more from your specialized classes, define a new interface and
require that to be implemented.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Wood" <j@ro.com> wrote in message
news:OC**************@TK2MSFTNGP12.phx.gbl...
我有一个派生类(B)来自另一个类(A) - 它提供了该基类的专门化。

事实证明基类(A)需要是可替换的。也就是说,我需要能够插入新的实现(保留相同的
接口)。

我无法更改A或B的源代码,它需要在一个库中
I have a class (B) that derives from another class (A) - it provides a
specialization of that base class.

It turns out that the base class (A) needs to be replaceable. That is, I
need to be able to plug in new implementations (retaining the same
interface).

I cannot change the source code of A or B, it needs to be in a library


已发货。但客户应该能够插入新的基础
is shipped out. But customers should be able to ''plug-in'' new base


类。


除了将代理类作为A委托给插件-in

Other than having a proxy class as A that delegates to a plug-in


interface,


interface,

我看不到其他任何方式。而且我真的不喜欢写
I can''t see any other way of doing it. And I really don''t like writing


代理

课程。

任何人都有更好的想法吗?

谢谢。
classes.

Anyone have any better ideas?

Thanks.




>是的,但问题是插入接口如何暴露给应用程序的
> Yes, but the problem is how that ''plugged-in'' interface is exposed to the
使用者。你看,专门的类必须直接暴露界面的每个方法,以及它自己提供的新方法。


你需要一些返回接口的对象工厂,并且插件组件有一些方法可以用它来注册。我的

当前应用程序框架有一个''PlugInManager''类,它加载

程序集并将每个程序集传递给感兴趣的各方,因为它们是

已加载。


例如,如果我有一个IFileExporter接口和一个FileExporterManager

,它维护一个可用文件导出器列表。当每个插件程序集加载时,它将被传递给FileExporterManager对象。

FileExporterManager比枚举程序集中的公共对象

(使用反射)并提取实现

IFileExporter接口的所有类型的列表将它们放在一个列表中。


稍后我可以在内部询问FileExporterManager''哪个出口商可以使用'b $ b''并将列表提供给用户选择一个。在内部我

他们可以对FileExporterManager说'好吧,我想要这个。你可以为我创建它吗?'''。然后管理器创建一个对象的实例并返回

一个IFileExporter引用供我使用。


当然,你不仅限于接口。你可以使用抽象类,

基类,属性或组合。

理解我的意思?
consumer of the application. You see, the specialized class has to expose
every method of the interface directly, as well as the new methods it
provides itself.
You''ll need some kind of object factory that returns the interface and has
some way of for plug-in assemblies to register themselves with it. My
current application framework has a ''PlugInManager'' class that loads the
assemblies and passes each assembly to interested parties as they are
loaded.

For example, if I have an IFileExporter interface and a FileExporterManager
which maintains a list of available file exporters. As each plugin assembly
is loaded, it is passed to the FileExporterManager object. The
FileExporterManager than enumerates the public objects within the assembly
(using reflection) and extracts a list of all types implementing the
IFileExporter interface and places them in a list.

Later on I can internally ask the FileExporterManager ''Which exporters are
available'' and present the list to the user who chooses one. Internally I
can they say to the FileExporterManager ''Ok, I want this one. Can you create
it for me?''. The manager then creates an instance of the object and returns
an IFileExporter reference for me to use.

Of course, you''re not limited to interfaces. You could use abstract classes,
base classes, attributes or a combination.
Understand what I mean?




我希望以上内容相关且有意义:)


n!



I hope the above is relevant and makes sense :)

n!


这篇关于OOP设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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