This period of time has been gnawing algorithm, for a long time have not written an article, mainly algorithm this thing is written repeatedly, the level of the article has, I also do not want to gather together this lively, of course, encountered interesting problems, or want to remember.
None of this is relevant to what I’m going to write, so I’ll cut to the chase.
The strategy pattern
In current development, there are about 23 design patterns, of which the most widely used are singletons, policies, adapters, and delegates, and I’m going to talk about policy patterns, because they might be used.
In simple terms, the policy mode automatically selects different algorithms and implementations according to different environments and conditions, that is, the policy mode is the encapsulation of the algorithm, which separates the responsibility of the algorithm from the algorithm itself and delegates it to different objects to manage.
Here’s a snippet of code (maybe two or three lines before you scroll down) :
public static void setAction(JMenuItem menuItem) {
if (menuItem.getText().equals("New")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Open")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Exit")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Goto")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Help")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Windows")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }else if (menuItem.getText().equals("Setting")) {
menuItem.addActionListener(
event -> {
//someting to do.}); }}Copy the code
In a nutshell, this code uses the policy pattern, but unlike the standard definitions we’ve seen, it uses if and else.
NOTE: So-called design patterns, in fact, there is no standard definition, and I said “standard definition” is actually, is generally accepted, accept the definition of, because of the definition is only convenient for us to communicate, otherwise we communication is – “I wrote a method (function), to each different algorithms encapsulation, and then the parameter passed to this method, It is up to the method to choose the algorithm.” Smelly and long, who wants to see, remember that design patterns are an idea, now formalized, but not necessarily related. The idea of a design pattern leads to a formal definition, not to a formal definition. Don’t reverse causality.
You can see that this code is supposed to be written as a menu event, with Lambda expressions, hey, kind of interesting.
However, it can also be seen that there are a lot of hard damage, not to mention the number of menu items that may be added in the later period, but in order to improve the button event, the amount of code is absolutely not less, a function encapsulates more than 100 lines of code, it is really rare.
It’s against the clean code we’re looking for.
Simplify the code
The simplification of this code is simple, and everyone has a different approach.
For example:
- The implementation of the button event is distributed to other methods, which do not implement the button event in this method, just save if… The else… Structure, in this way, we ensure that a function \ method to complete a function of the principle, convenient later maintenance.
- Of course, we can also use multiple classes, each with a single responsibility. As in the code above, the NewItem class implements the New button, and since it is a class, we can do even more with this button. It’s not hard to imagine that this would expand the class library and make it more bloated (probably only for large projects), but it’s a good way to do it.
- Routing… The structure and functionality of the above code may not be practical.
Simple implementation (really crude) :
public static void setAction(JMenuItem menuItem) {
// Reflection is used here, which is the second method above.
if (menuItem.getText().equals("New") setAction("New", menuItem);
/ / else if...
}
Copy the code
There are a variety of methods, but remember a word: the method itself is not right, there is only suitable for the problem.
Pick the best, guys.
I note: The title is not grandiose, not too pragmatic, which is why I hate headlines.