The main difference between process-oriented design and object-oriented design is whether to use lengthy if else judgment at the business logic layer. If you are still using the if else in large quantities, except for the interface presentation layer, even if you use a fully object-oriented language like Java/C, it only means that your thinking stays on the traditional process-oriented language.
Traditional thinking habits Analysis
Why does the business logic layer use if
Else, in fact, the user's purpose is also to reuse, but this is the reuse of process-oriented programming. The programmer only sees code reuse, because he sees if
In several cases, most of the else code is repeated and only a few are different. Therefore, if else can be used to avoid code duplication, and it is considered as a template pattern.
He Fan's error is: programmers only look at the code in the direction of the code running sequence. This kind of thinking is similar to water pipes or serial circuits, where water flows along the water pipes (code running order ), when there are several sub-managers, the sub-pipes are flowing, which is equivalent to the IF else of the Code.
When Oo is used, the code is first broken from top to bottom, which is equivalent to the sequential order of the runtime.
The code structure cannot be executed in sequence. What determines the code structure? Designed by Oo; Design Patterns replace these if
Else, but at last, the OO modules are always assembled by a service and other general classes in the running order. There is only one such module, which can contain transactions. Generally, this module is a service.
Session Bean.
Once the requirements change, we may be more likely to be the various oo modules in the service, or even only change the execution sequence of the OO modules in the Service to meet the requirements.
Here we also see the idea of OO separation, which completely breaks down a main function in the previous process language and separates the running sequence from other logic of the Code, rather than being as messy as the process-oriented logic. Some people feel that OO is also in order. This is for sure. The key is that the running sequence should be separated separately.
If else does not exist, you can see whether you have separated the running sequence to the home.
Design mode switch entry
It is often reported that the design mode is good, but it is difficult for me to use. In fact, if you use if else to write code (except for display control), you are writing business logic, however, a simple judgment statement is used as a replacement for the actual situation.
Take the forum post that you are familiar with as an example. For example, forummessage is a model, but in practice the post is divided into two types: topic post (first root post) and reply (back to the previous post), there is a simple solution:
Create a forummessage and add istopic to the forummessage to determine the statement. Note that the introduction of a simple attribute here may lead to If else judgment everywhere in your program.
If we use another method of analysis and implementation to look at it as an object, there are actually theme posts and replies.
There are two types of objects, but most of these two objects are the same. Therefore, I set forummessage as a topic post, and then create a subclass that inherits forummessage.
Forummessagereply is used as the reply. In this way, I am in the program, such as in the service, I have confirmed that this model is a reply, and I will directly go down
Forummessagereply is enough, which is similar to the forced type conversion when the object is put into the collection and retrieved. By using this method, I have eliminated if
The else judgment statement may appear.
This shows that incorrect analysis may also lead to misuse of the model.
We will discuss the example of a design pattern. We cannot use cases without business context. Otherwise, we cannot decide whether to use the pattern. The following are two examples of comparison:
First, there is no context for the first code case in this post. This article only describes a piece of code:
Main (){ If (Case ){ // Do with strategy } Else (Case B ){ // Do with STRATEGY B } Else (Case C ){ // Do with strategy C } } |
|
This code is pure code and has no business functions. Therefore, in this case, it is difficult for us to determine which mode to use, that is, the policy mode must be used, it cannot escape the fate of using if else. The design pattern is not magic. It cannot make meaningless code simple, and it can only expand the business functions it embodies more easily.
Second. in this post, the author gave a packetparser business case. This code shows the business functions and analyzes data packets. The author also compares the usage of different modes, therefore, we still use the dynamic proxy mode or command mode to eliminate the possible if else
The above two cases show that the business logic is the starting point for us to use the design model, and when we break down the business logic, we may use if else for implementation, if you have such an attempt or already implemented the code, you should consider whether refactoring needs to be reconstructed.
If else replacement
In practice, which design modes can replace if else? In fact, the gof design pattern can be used to replace if else. We describe it as follows:
Status Mode
When the data object has various possible states, and this state will affect
When it comes to different business results, we should consider whether to use the status mode. Of course, before using the status mode, you must first have the concept of memory status, rather than the concept of database, because traditional
In a database-oriented system, it is difficult for you to find the status, read a value from the database, and then run the code based on the value for shunting. This is a common task for beginners. Reference: status object: Database replacement
In the case of traditional language thinking, a class integer variable is used to identify the status:
Public class order { Private int status; // Description: // Status = 1 indicates order but view; // Status = 2 indicates that the file has not been processed; // Status = 3 indicates that the unpaid payment has been processed // Status = 4 indicates payment has not been delivered // Status = 5 indicates that the product has been delivered. } |
The above class design undoubtedly uses the class as a function of the traditional language, which leads to a large number of if else in the program code.
Rule Mode
When you are faced with several algorithms or formulas, you can consider the policy mode. The traditional process language is: Read the algorithm value from the database. The value 1 indicates policy 1, for example, save it to the database; A value of 2 indicates policy 2, for example, saved to an XML file. Here, if else is used as the switch for policy selection.
Command mode
The traditional process is: if the client sends the Code 1 or "A", then I call. java. If the code is 2 or "B", I will call B. if else is used to determine the code sent from the client, and then the corresponding table is specified in advance, and the corresponding class is called for processing.
MVC Mode
The misuse of the traditional MVC mode is similar to that of the command mode,
In an action class, if
Else performs frontend and backend scheduling. If the client sends any command, I call the background result. If the backend processes any structure, I decide what pages to push. However, now we use
The framework implementers in MVC mode such as Struts and JSF do not need to make such low-level errors.
Responsibility Chain Model
The responsibility chain mode and command mode are optional. If you do not know what code will be issued by the client, there is no pre-defined table, then you can only write classes to open the package just as you try your luck. Unlike command, it is analyzed in the document of AOP vs decorator.
Proxy or dynamic proxy Mode
The proxy object can be a representative that meets certain conditions, for example,
The traditional process-oriented approach is to use if
Else can only allow access when certain conditions are met. In this way, permission judgment and business data logic are in disorder and can be clearly separated using the proxy mode, use Dynamic proxy,
Or the following methods, such as AOP.
AOP or decorator Mode
In fact, the use of the filter can also replace the if else in our business. The filter can be used to filter and filter objects that comply with the filter conditions to intercept and do something, this is the function of a filter. The combination of multiple filters is actually the combination of if else.
Therefore, if you really cannot find any way, you can use a filter to regard the filter as a firewall. When the client has a request, it goes through a firewall of different nature, this firewall is the interception end.
The firewall is used for security check and interception. Filters are similar to various filters of red, blue, and white. The red filter can only intercept the red color in the light. The blue filter intercepts the blue color in the light, which is actually
Is to use if else for decomposition of light.
We use conditional filters to implement three-dimensional Signal Separation. If you use if Else, it means that you combine conditions 1/2/3/4 in the image, implement condition judgment in the same place.
To learn more about the implementation details and minor differences of filters, see: AOP vs decorator
Summary of OO design
There is also a pseudo-mode. Although the State mode is used, the IF else or switch mode is actually used inside the mode for status switching or important condition judgment, which undoubtedly requires further efforts. More importantly, you cannot live in a model and publish a book to people.
It is difficult to grasp the object-oriented ideas. At present, there are various ideas that hold your hair up.
I think thinking in
Java (Java programming ideology) is useless. It tries to talk about OO programming ideology at the language level, which is very unsuccessful. It can be used as a language reference, but as a learning material for the OO ideology embodied in Java,
It is wrong.
OO programming is a methodology. If there is no Application Comparison of methodology, it is impossible to understand the characteristics of this methodology. Zen is a methodology in ancient times, and the application of wuzen can be realized only by picking water and cutting firewood.
So what application can oo idea be realized? Is the gof design mode, while the gof design mode is
Because a programmer cannot even have a basic life, why does he live as a programmer? Engaged in OO professional design and programming, if you do not master the basic skills of the design mode,
Just like a monk who is unwilling to pick water and cut firewood, why does he base himself in this industry? The master has been driving down the hill for a long time.
Conclusion: If else can be used in a small place, such as a simple numerical value. However, if you follow your traditional habits, if else is also used to implement business functions, it means that your thinking may need to be reshaped. The richer your programming experience is, the more deep-rooted the traditional process thinking model is. It is difficult to change your thinking by yourself. We recommend that you receive professional brainstorm training.
To sum up one sentence: if you have made a lot of systems and haven't used if else for a long time, it means that you may actually enter the OO design stage. (This is my own practical evaluation standard ).
From: http://www.jdon.com 2006/1/11