将应用程序与应用程序库、共享库、组件等分离
我正在创建一个 MVC 应用程序框架,仅使用我自己创建的库和组件(主要是一种学习经验),但我不太确定如何将每种库彼此分开。
我们将我的应用程序命名为 Cat。
假设我正在创建一个名为 Dog 的库,它有点像 Zend,并且充满了执行不同任务的不同组件(数据库类、DAL、用于确定从给定 URL 中选择哪些控制器的路由器等),并将位于 root/library/ 目录中。
我还将创建一个特定于应用程序的应用程序库(可能包含 FrontController 或 Application 之类的类,以帮助启动和配置应用程序)。这将位于 root/app/library/ 中,
我想在此应用程序和其他应用程序上使用 Dog 库,并希望它独立于 Cat,因此它可以用于许多其他应用程序。
在我的 Cat 应用程序中,假设我创建了一个新的数据库对象。我应该写:
$database = new Dog_Database();
还是拥有一个纯粹扩展 Dog_Database 类的 Cat_Database 类会更好?这意味着我稍后可以告诉 Cat_Database 如果需要的话扩展 Ferret_Database ...
我想主要问题是,我的应用程序应该直接从共享库调用东西,还是应该调用特定于应用程序的库类,这如果需要的话,反过来从共享库扩展?
I am creating an MVC application framework, using only libararies and components that I create myself (mainly a learning experience), but I'm not quite sure how to separate each kind of library from one another.
We'll call my application Cat.
Lets say I'm creating a library called Dog, which would sort of be like Zend and is full of different components that do different tasks (Database classes, DALs, Routers for figuring out what Controllers to choose from a given URL, etc), and will be located in the root/library/ dir.
I'll also be creating an app library that would be application specific, (Might contain a class like FrontController or Application to help with initiating and configuring the app). This will be located in root/app/library/
I want to use the Dog library on both this application and other applications, and for it to be hopefully independant of the Cat, so it can be used on a number of other applications.
In my Cat application, lets say I create a new database object. Should I be writing:
$database = new Dog_Database();
or would it be better to have a Cat_Database class that purely extends the Dog_Database class? That would mean I can later tell the Cat_Database to extend a Ferret_Database instead if needed...
I guess the main question is, should my application be calling things straight from a shared library, or should it be calling app-specific library classes, which in turn extend from the shared library if needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你在某种程度上回答了你自己的问题。你说你希望狗独立于猫。对我来说,这意味着在两者之间使用大量依赖注入,而不是大量继承或直接调用。如果您想在没有框架的其他地方重用应用程序库,则无法使您的应用程序库从框架扩展类。
对于这样一个广泛的问题很难给出详细的建议。也许其中一些概念会对您有所帮助。
依赖注入
对接口进行编程
I think you answered your own question somewhat. You say that you want dog to be independent of cat. That means, to me, using a lot of dependency injection between the two and not a lot of inheritance or direct calling. You can't make your app library extend classes from the framework if you want to reuse the app library elsewhere sans the framework.
Its tough to give detailed advice to a broad question like this. Perhaps a few of these concepts will help you though.
Dependency Injection
Programming to an interface