How to write code with OOP mindset
When we talk about the OOP Design, it is all about how to write your code with OOP mindset.
This is very important if you are using OOP language as Java or C#.
Then how to build your OOP mindset when you started to write code.
1. Java Programming all started with Class and Object
No matter how small or big your program/app you will started with.
You have to think about Class and Object first.
Initialize the first object and next, started to cultivate the thinking that everything is an object
So try to avoid to write all your sample/prototype code into your main function. Think about more which elements are Object/Class, Method, Attribute. When you start to build the thought as this, you can see your coding reusability will be start to getting better and better. Make your componentalization concept maximized.
In the Java Programming real world, theoretically you will not use the Primitive Data Types compare to the Object Type.
For example,
If you have read through the clean code book, you know when your function have more than 3 parameters pass through there, it means something wrong there based on OOP Design.
If you have too much if-else code written there, it is wrong as well. It needs to use Object or redesign to refactor the code.
Why you are using utility class instead of design your object inheritance better
When to use Domain Driven Design better?
领英推荐
3. Writing function code
I will suggest that start to write what steps we need to complete it
Using comments instead to have your ideas ready
Started with OOP Objects.
Treat almost everything can be the objects.
4. OOP started with Drawing OO relationships
When you started to writing code, better to draw on paper on Class, Object,Relationship, Method and Attribute etc.
Later you can start to use some online class diagram tool, it helps to get into OOP Design as well.
5. Start to be always treat each line of your code with good Code Quality
Started with Naming Convention , then next Code readability.
You can refer to Google Coding Standard和 Oracle Java Coding Standard.
Also read through the clean code book as well.