Learning to think in the Object Oriented Way
I am a programmer trying to learn to code in the object oriented paradigm... I mainly work with PHP and i thought of learning the zend framework... So, felt I need to learn to code in OO PHP....
The problem is, having done code using fu开发者_开发知识库nctions for quite a long time, i just can't get my head to think in the OO way....
Also felt that probably I am not the only one facing this problem since the beginning of time...
So, how did you people learn object oriented programming... especially how did you succeed in "unlearning" to code using functions... and learn to see you code as objects...?
Is there any good resource books or sites where one could find help...??
Thanks for sharing your knowledge and experiences...
I've been doing software development with OO for over 20 years now, and I can tell you that looking at other peoples code is more often than not going to teach you how to do procedural programming in an object oriented language.
What I would recommend is to use the following techniques, which if applied liberally, will force you to use OO techniques, even though you may not yet be aware of them.
- Do not copy and paste code - ever.
- Create classes that represent the things you talk about when talking about the functionality - for example, an order entry system will have Orders, Customers, Accounts, OrderItems, InventoryItems, etc.
- When creating these classes, do NOT code any public set and get methods to access the class data members.
- Add methods to these domain model classes that perform the work on the object in question. Order.invoice(), account.close(), InventoryItem.decrement(). The lack of public get methods will show you the correct location for the code (where the data is - in the appropriate domain object). Remember, an object is data and the code that operates on it - anything short of both is not an object.
- You will eventually find that you have to add some public get methods for some class members, and that is ok, just hold off until you are forced to do so. Reluctantly add public get methods.
- At the level of the application, almost every line of code should be moving mountains. In other words, most of the lines of code at the application level should be calling on domain model methods.
- Put all of the functionality in the domain model objects, then expose that functionality in an application by hooking it up to a user interface. I repeat, put the functionality in the domain model, not the application.
If you follow these guidelines, you will definitely be producing object oriented code, and probably at a much higher level of proficiency than many experienced developers.
Finally, avoid injection - i.e. Spring, Unity, etc!! There are probably a handful of valid cases for using injection - most uses arise out of a lack of object oriented design experience. As a guideline for whether to inject or not, consider how often what you are thinking of injecting is likely to change. In many, many cases, I find that what is being injected will never change - in these cases, the only thing being injected is pure overhead.
Good luck!
- Read other people's code - people who you know are good developers
- Books/articles that teach "idomatic usage" of the language
- (Avoid anything with the words "in 21 days")
It takes time.
Moving from procedural programming to object oriented is difficult. These days, many people start with object oriented, so they don't struggle with this paradigm change.
- Learn about the fundamentals of OOP and keep referring to them to start with.
- Read OO code - there are lots of open source projects out there that you could sample.
read one of the following book
- 2015_Book_Object-OrientedAnalysisDesignA
- Head First Design Patterns By Eric Freeman and Elisabeth Freeman Small
- Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to OOA&D
enjoy
PDF LINKS
1) Head First Object-Oriented Analysis and Design
Head First Object-Oriented Analysis and Design.pdf
2) Head First Design Patterns
Head First Design Patterns.pdf
精彩评论