2009-12-20 21:23
浏览 11


I have a class which I want it's instances to be used only by a certain other classes.
Is there a known design pattern for such a problem?
An example for such a necessity would be a case of a big application (a team of 10) with BL objects and DL objects under a MVC hood, where I want to make sure only the BL classes can call/use the DL classes. Not in the controllers/views/helpers etc.
I am using PHP (which means I have no sub classes).

图片转代码服务由CSDN问答提供 功能建议

我有一个类,我希望它的实例仅由某个其他 使用 这个问题有一个已知的设计模式吗?
这样一个必要性的例子就是一个大型应用程序(一个10人团队)的案例,其中包含BL对象和MVC下的DL对象 引擎盖,我想确保只有BL类可以调用/使用DL类。 不在控制器/视图/帮助器等中。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwen2162 2009-12-20 21:30

    I'm not a PHP person, but in other platforms such as .NET you could make the DL classes and BL classes live in the same assembly (library) with the DL classes marked as 'internal' to the assembly (cannot be accessed by other assemblies). Is this something that is achievable in PHP?

    Generally, however, I would not do this. I would have one BL library and one DL library and simply make it known that the DL library ought not to be used by any code apart from that in the BL library. In my experience this approach works, even in a team of 10 or more. If your BL libraries genuinely encapsulate reusable logic which makes them better classes to use than the DL classes directly then you should see your peer developers using them in favour of the DL classes. Improper direct use of DL classes should be exposed either by peer code reviews or failed unit tests and/or integration tests. In fact, the latter should always expose any failures in business logic in BL/DL consuming code due to bypassing mandatory business logic rules which are contained in the BL layer. I guess what I'm really saying is, as the DL/BL developer, you should just code those classes appropriately without concern for their visibility, and safely rely on other mechanisms to expose flaws in any potential consuming code, which should be the case if your project has a good testing and code review policy.

    Note: just for completness, in .NET it is also possible to have separate BL and DL libraries, with the DL classes marked as internal and the BL library marked as a 'friend' assembly of the DL library, allowing the BL library to access the DL internal members. Other libraries would not be marked as friends and would therefore not be able to acccess the internal DL members. Again, I'm not sure if anything like this is possible in PHP.

    解决 无用
    打赏 举报