Practice again today , Let everyone as familiar as possible and skilled object-oriented code writing ideas and methods .
Today's clothes are in Chinese “ Shape ” As an example , Practice how to write object-oriented code for Hanfu . The Chinese encoding in this article is for your convenience , You should not write your business in Chinese .
One 、 conceived
Your idea , It can come from UML chart , It can also be drawn from paper at will , It can also come from your mind map , You like it , You need to make sure the hierarchy is clear .
The picture below is about “ The shape of Hanfu ” Mind maps .
1. The inheritance of form is shown in the figure above , The tree structure is clear , No more nonsense .
2. Every shape , It's all about dynasties .
3. Every shape , You can go with some coats .
Science Popularization , Some people don't know about Hanfu , The appearance design of Hanfu is called shape . By the way , The rest of my life , Strive for the revival of Han costume of Shenzu .
Two 、 Before coding
Because I have studied the form of Han costume in advance , So it's been conceived of as an inheritance .
About the need for UML, And how detailed it needs to be , It's up to you , Usually , I only design the general direction , It helps me grasp the whole picture . For the method definition inside 、 Attribute definitions 、 Definition of relationship, etc , I don't think so carefully .
When to think about these things ？ I thought about it when I was coding .
This is my object-oriented thinking habit , You can refer to , You don't have to copy .
Once you have an overall framework idea , When you're going to code , It's already very fast . Now please continue to look at .
3、 ... and 、 Start coding ： First write the basic framework code
Let's first write down the inheritance of form ：
Then write down the inheritance relationship between the Dynasty and the coat ：
well , such , We've decided on the overall structure .
For such code writing ideas , I'm sure you have no problem .
Four 、 Start coding ： Write logic management class
Create Hanfu class , Hanfu is made of shapes 、 Dynasty 、 The coat is made up of , So for convenience , We write it directly in the constructor ：
Create Hanfu management class , Management usually looks for features ：
well , Let's write about a founder of Hanfu , That is, the class of the generator ：
I don't know if any readers with a good sense of smell have found , Actually write code , It's a routine , Nothing more than the first subject , Then manage the class , Then the driver class , Finally, add the relation operation control class .
5、 ... and 、 restructure
You'll see the code above , It's all concrete implementation . I've said that before , All the classes , Whether you're abstract or concrete , It's all concrete implementation , We need to abstract to the top level , Interface .
Pictured above , You edit -> restructure -> Extract interface , Or press the shortcut key . Take shape, for example ：
after , You need to modify the specific classes used everywhere before , Change to interface dependency or association , that will do .
Have you learned ？
Have a good meal .