NotionCommotion Posted May 15, 2018 Share Posted May 15, 2018 I've been reading about OOP and am starting to get a grasp of some of the principles such as encapsulation, inheritance, polymorphism, prefer composition over inheritance, SOLID, etc, but still am not "thinking" OOP but only somewhat applying these principles to my current procedural oriented brain. I expect I am overly thinking this, but I recently wrote an application using primarily inheritance and it become a convoluted mess, and I really need to change my way of thinking (I will write up some specifics to follow). I have listed a couple of my newfound inklings below: I had an epiphany yesterday when I questioned who is responsible to specify the interface. Previously, I would write some class which did something, and then write an interface which enforces it. While this might provide a little value, I now think I had it wrong, and this viewpoint prevented me from thinking OOP. Instead, I should have started from the parent class's prospective and asked "what do I want?" or "what you must provide" and not "what I can deliver". Is this a better way of thinking? If that need is overly specific, then I should make it more abstract and think "VERB ANYTHING" or "VERB YouTellMe". I still question how abstract is enough (i.e. drawCircle() is too specific, draw() is more abstract, makeArt() is very abstract) but expect I will come to grasp with this over time. In addition, I suspect one might try to decouple adjectives and/or adverbs from the action, but haven't yet generated any ideas how or really even why. I think domain classes represent the thing and is the authority of what the thing can do (methods) and what data it has (properties), and implementation classes control what we want to do with the thing. I've read that domain classes shouldn't extend implementation classes and neither the reverse and each should be separate classes, but I was never certain the correct definition of domain and implementation classes. UML are to OOP as ERD is to relational databases. Question: How important is it to create UMLs, and do you do so? Polymorphism, prefer composition over inheritance, and open-closed principle are all related to the same concept. Yes, I suppose polymorphism can be archived by inheritance but most seem to imply that it is archived by combining objects differently (i.e. composition). OCP requires that the functionality be changed without modifying the code, and if I extend the class and modify the code, well then it doesn't meet this principle. The use of the world "extension" is a little confusing in OCP's definition "Objects or entities should be open for extension, but closed for modification" as it implies that one class is "extended" by inheritance, but I suppose I am just misinterpreting the word "extend". "Prefer composition over inheritance" does not mean only use composition, but only when all things are equal, use composition. Inheritance is the better choice when taking a "thing" and making it more specific. For instance, circle extends (is inherited from) shape is more appropriate than inserting circle characteristics into a generic shape. Inheritance is preferred when dealing with a single logical domain where a sub-class's abilities are primarily additive to that of the superclass. Classes should be small, and if they get big, they are likely doing to much and have multiple "reasons to change" and should be sub-divided into multiple smaller classes. Inheriting some super grab bag utility class (i.e. more specialized array operations, cURL, rendering a prepared statement for troubleshooting, etc) is not a good idea (but is something I have always done). Instead I should either inject this class or make them static, or maybe if I reduce class sizes enough, not use them at all (not sure if I like this idea), and probably should break them into sub-classes. Sorry for kicking a dead horse, but I really need to make this sink in and would appreciate any comments whether I am "thinking" correct, or whether there are addition important ways I should alter my thinking. Thanks Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.