douling6469 2009-12-20 21:23
浏览 13
已采纳

是否有DP来解决一种类型只能由另一种类型使用的问题?

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?