Getting Started with C#: How to Reasonably Formulate Method Parameters – Part 1
1. Use object-oriented principles to split the functions we face into related objects
2. The same function should only appear in one class.
3. Try to extract the common parts of similar functions
The overall goal is simply to reduce the amount of code.
Let’s use a dynamic condition query interface to illustrate the above points
This is a medical record query function. The list on the left is all the fields that can be selected, the field in the middle is the field that the user requires to query, and the conditions for a certain query field on the right.
What classes are used to organize this interface function?
The result is not unique, everyone can have their own ideas. But I think organizing it in a way that more people can easily understand should be our goal.
The first thing that comes to our mind should be two objects: All fields and Field query range
Study the rules of all fields. More often, you should chat face to face with customers. We found that some fields use the same query range entry interface.
So there seems to be a field type hidden here. That is, the behavior of a certain type of fields is consistent, and the behaviors of interface entry, condition generation, etc. are consistent.
Field query range and field type are easy to think of as attributes of the field, rather than functional objects that construct the main elements.
Yes, the name is confusing. But if I change the names to “query scope” and “behavior controller”, you may understand my intention.
Three objects: field, query scope, behavior controller
The left display shows all the “fields”, and the noon display shows all the “query ranges”. The behavior controller must be an interface that allows the main program to handle all types of fields gracefully.
The picture above is the code for the intermediate query range selection event. This main program can handle the display of N types of fields.
This article is only published in the blog garden. Please do not reprint without permission!