views:

49

answers:

1

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  A: 

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.

AdamRalph
Thanks, Sadly, PHP does not support such features as far as I know.
Itay Moav