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