如何创建“真正可扩展的”应用程序班级
我读过很多关于开发类的文章(我使用的是 php),其标签行是: “可扩展、稳健、可维护和可扩展”。
但作为一个初学者,我一直在创建用我的话来说“只是抽象的”类。这意味着我只是将一堆或重复的代码分开,并将它们放入一个类中,并提供用于访问常见任务的方法。
问题是,我找不到一种方法来使我的类可扩展(我知道抽象类等概念,我什至正在使用它们,但只是为了定义我的其他类将遵循的方法)。问题是,我总是发现自己编辑核心类只是为了添加功能。
关于使我的课程可扩展有什么建议吗? (我用谷歌搜索过这个,弹出的所有内容都是对抽象类、接口和 OOP 的解释,没有关于指针的讨论或一些创建可扩展类的技巧)。
哦,顺便说一句,对我放轻松,我九个月前就开始了“实际的”oop 编程(我所在的大学让我继续进行 OOP 理论,但他们让我们按照程序进行工作,因为它更快,而且可以满足该死的项目截止日期,这种情况持续了四年,直到我毕业)。
I have read a lot of articles on developing classes (I am using php), with the tag lines :
'scalable, robust, maintainable and extensible'.
But as a beginner, I have been creating classes that are, in my words, "just abstracted". Meaning I just separated a bunch or repetitive codes and put them in a class and provide methods for accessing common tasks.
The thing is, I can't find a way to make my class extensible (I know the concept of abstract classes and such, I am even using them, but just to define methods that my other classes will follow). The thing is, I always find myself editing the core class just to add functionlities.
Any tips on making my class extensible? (I have googled on this and everything that pops out are explanations of abstract classes, interfaces and OOP, no discussions on pointers or some tips for making extensible classses).
Oh, btw, go easy on me, I have started "actual" oop programming 9 mos ago (the university I'm from had me going on theories on OOP, but they had us working PROCEDURALLY, because it's faster and it meets damn project deadlines, and that went on for 4 years until I graduated).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该看看这本书设计模式:可重用面向对象软件的元素
正如您所发现的,可扩展类正在将系统分解为有用且可重用的对象。
您是否正在尝试对某些现实世界场景进行建模,或者您是否专注于应用程序内部的通信/协作和依赖关系?
这是一个我认为可以说明您正在寻找的内容的示例。当然还有更多、更好的例子:
我想开发一个缓存系统,为我的开发人员提供一个简单、规范化的 API,无论他们在哪里缓存什么内容。我在缓存系统中想要什么(在基础层面)?
我想出了这个:
这是我的可扩展基类。然后我得到了三个缓存类
MyNs_Cache_Fs
、MyNs_Cache_Apc
和MyNs_Cache_Memcache
这相当简单。它以文件系统的形式实现缓存。它不提供任何超出我原来的课程的内容。
我的 APC 缓存可以在缓存系统中执行我想要的所有操作(设置/获取/删除),但它还提供清除整个缓存的功能(这对我的文件系统缓存没有用处,并且对于 memcached 来说是不可能的)
现在我知道我总是可以获取我的缓存对象之一,然后使用 $obj->Get('some_key') ,我总会得到结果。
同样,我还可以访问特定于我当前正在尝试使用的功能。
You should check out the book Design Patterns: Elements of Reusable Object-Oriented Software
The problem with making extensible classes, as you've discovered, is decomposing the system into useful and reusable objects.
Are you trying to model some real world scenario, or are you focusing on communication / collaboration and dependencies inside your application?
Here's an example that I think kinda demonstrates what you are looking for. There are certainly a far more, far better examples:
I wanted to develop a caching system that offered my developers a simple, normalized API no matter what / where they were caching something. What do I want in a caching system (at the basic level)?
I came up with this:
There's my extensible base class. I've then got three caching classes
MyNs_Cache_Fs
,MyNs_Cache_Apc
andMyNs_Cache_Memcache
That's fairly straight forward. It implements the cache in terms of a FileSystem. It doesn't offer anything past my original class.
My APC cache does everything I want in a caching system (set/get/delete) but it also offers the ability to clear the entire cache (something that's not useful for my FileSystem cache and impossible with memcached)
Now I know that I can always get one of my cache objects and just in with $obj->Get('some_key') and I'll always get a result.
Likewise, I also have access to functionality specific to what I'm currently trying to work with.
您不需要编辑核心类来添加功能,您可以覆盖子类中的方法,例如:
You don't need to edit your core class to add functionality you can overwrite a method in a child class, eg: