哪种设计选项更适合用于编码框架?

时间:2022-10-06 13:54:44

I'm coding up a framework (in Java, but question is generic) in which I will provide a set of interfaces for clients to implement. The functions in the framework are going to rely on how the implementation classes will be constructued, that is, thay depend on those implementations to provide other instances of interfaces.

我正在编写一个框架(在Java中,但问题是通用的),我将在其中提供一组客户端实现的接口。框架中的函数将依赖于如何构造实现类,即,依赖于那些实现来提供其他接口实例。

For example I might have:

例如,我可能有:

Interface IContribution {
   public IMyStuff getMyStuff();
   public IHelper getHelper();
}

Interface IMyStuff {
     public void doSomeMethod(IHelper helper);
}

How can I make sure that those instances of IMyStuff and IHelper are available?

我怎样才能确保IMyStuff和IHelper的那些实例可用?

One method would be to create the 'getter' methods in the interface and in my framework painstakingly check for null objects returned.

一种方法是在接口中创建'getter'方法,并在我的框架中仔细检查返回的null对象。

Another option would be to create abstract classes that implement a factory that calls (using a strategy patterns) the interface methods to be implemented. But this defies the fact that I have the interface in the first place. Clients should then use the abstract class. But they could circumvent this by using the interface instead of the abstract class. Therefore I should not provide the interface but only the abstract class...

另一种选择是创建实现工厂的抽象类,该工厂调用(使用策略模式)要实现的接口方法。但是,这就是我首先拥有界面的事实。然后客户端应该使用抽象类。但他们可以通过使用接口而不是抽象类来绕过这一点。因此我不应该提供接口而只提供抽象类...

So, what are your ideas on this, what is a pragmatic approach to this?

那么,您对此有何看法,这是一种务实的方法呢?

3 个解决方案

#1


How can I make sure that those instances of IMyStuff and IHelper are available?

我怎样才能确保IMyStuff和IHelper的那些实例可用?

If the clients are responsible for implementing the interfaces and classes themselves, I would say it's their responsibility to make sure that those instances are available - I wouldn't concern myself with putting it in my own code.

如果客户端负责自己实现接口和类,我会说他们有责任确保这些实例可用 - 我不会把自己放在我自己的代码中。

#2


In order to build a good framework, you need to simultaneously be building an application around it. That way you will know and understand the pain your clients will endure BEFORE it is foisted upon them.

为了构建一个好的框架,您需要同时围绕它构建一个应用程序。通过这种方式,您将了解并了解客户在被强加给他们之前所忍受的痛苦。

In other words, start with: How would my clients work with this application? How do they need to work with it?

换句话说,从以下开始:我的客户将如何使用此应用程序?他们如何使用它?

You will immediately realize that the simplest way, from their perspective, is going to be best.

您将立即意识到,从他们的角度来看,最简单的方法是最好的。

#3


You can't ensure it with Interfaces alone, no behaviour there.

您无法单独使用Interfaces来确保它,也没有行为。

I agree with the philosphy of defensive programming in the Framework, help the developers avoid making mistakes.

我同意框架中防御性编程的哲学,帮助开发人员避免犯错误。

You can supply a factory object:

您可以提供工厂对象:

public class MyPoliceman {
    public IContribution makeContributor( IMyStuff stuffer, IHelper helper)
                    throws BadAssociatesException {

     // check validity of stuffer and helper here, throw exceptions if null
    }
}

Then at least we can check for nulls etc.

那么至少我们可以检查空值等。

With some thought it's usually possible to give the developers help. In some cases the best you can do is to trap errors and report them craefully. For example here, a perfectly fine IHelper could be passed to your factory, but later actions on the class could render it incapable. (eg. Image it was a File, and a side effect later closed the file.) Then all you can do is trap the resulting error condition, record an error somewhere and (likely) throw an exception. Then at least the developer has a clue about what to fix.

有些人认为通常可以给开发人员提供帮助。在某些情况下,您可以做的最好的事情就是捕获错误并报告它们。例如,在这里,一个非常精细的IHelper可以传递给你的工厂,但是后来对这个类的操作可能会使它无法实现。 (例如,Image它是一个文件,副作用后来关闭了文件。)然后你所能做的就是捕获产生的错误条件,在某处记录错误并(可能)抛出异常。然后至少开发人员有一个关于修复什么的线索。

#1


How can I make sure that those instances of IMyStuff and IHelper are available?

我怎样才能确保IMyStuff和IHelper的那些实例可用?

If the clients are responsible for implementing the interfaces and classes themselves, I would say it's their responsibility to make sure that those instances are available - I wouldn't concern myself with putting it in my own code.

如果客户端负责自己实现接口和类,我会说他们有责任确保这些实例可用 - 我不会把自己放在我自己的代码中。

#2


In order to build a good framework, you need to simultaneously be building an application around it. That way you will know and understand the pain your clients will endure BEFORE it is foisted upon them.

为了构建一个好的框架,您需要同时围绕它构建一个应用程序。通过这种方式,您将了解并了解客户在被强加给他们之前所忍受的痛苦。

In other words, start with: How would my clients work with this application? How do they need to work with it?

换句话说,从以下开始:我的客户将如何使用此应用程序?他们如何使用它?

You will immediately realize that the simplest way, from their perspective, is going to be best.

您将立即意识到,从他们的角度来看,最简单的方法是最好的。

#3


You can't ensure it with Interfaces alone, no behaviour there.

您无法单独使用Interfaces来确保它,也没有行为。

I agree with the philosphy of defensive programming in the Framework, help the developers avoid making mistakes.

我同意框架中防御性编程的哲学,帮助开发人员避免犯错误。

You can supply a factory object:

您可以提供工厂对象:

public class MyPoliceman {
    public IContribution makeContributor( IMyStuff stuffer, IHelper helper)
                    throws BadAssociatesException {

     // check validity of stuffer and helper here, throw exceptions if null
    }
}

Then at least we can check for nulls etc.

那么至少我们可以检查空值等。

With some thought it's usually possible to give the developers help. In some cases the best you can do is to trap errors and report them craefully. For example here, a perfectly fine IHelper could be passed to your factory, but later actions on the class could render it incapable. (eg. Image it was a File, and a side effect later closed the file.) Then all you can do is trap the resulting error condition, record an error somewhere and (likely) throw an exception. Then at least the developer has a clue about what to fix.

有些人认为通常可以给开发人员提供帮助。在某些情况下,您可以做的最好的事情就是捕获错误并报告它们。例如,在这里,一个非常精细的IHelper可以传递给你的工厂,但是后来对这个类的操作可能会使它无法实现。 (例如,Image它是一个文件,副作用后来关闭了文件。)然后你所能做的就是捕获产生的错误条件,在某处记录错误并(可能)抛出异常。然后至少开发人员有一个关于修复什么的线索。