In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to improve the reusability of Java code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Measure 1: rewrite the instance method of the class
Code reuse through class inheritance is not an accurate code reuse technology, so it is not the most ideal code reuse mechanism. In other words, we cannot reuse a single method in the class without inheriting all the methods and data members of the entire class. Inheritance always brings extra methods and data members, which always complicate the code to reuse a method in a class. In addition, the dependency of the derived class on the parent class further complicates the code: changes to the parent class may affect the subclass; when modifying any class of the parent class or any subclass, it is difficult to remember which method is overridden by the subclass and which method is not overridden by the subclass; finally, it is sometimes not obvious whether the overriding method in the subclass calls the corresponding method in the parent class.
Any method, as long as it performs a single concept task, should be the preferred reusable code in itself. In order to reuse this code, we must return to the process-oriented programming model and move the instance methods of the class out of the global process. In order to improve the reusability of this process, procedure code should be written like a static tool method: it can only use its own input parameters, can only call other global procedures, and cannot use any non-local variables. This restriction on external dependencies simplifies the application of the process, so that the process can be easily used anywhere. Of course, because this organization always makes the code clearer, even code that does not consider reusability can also benefit.
In Java, a method cannot exist separately from a class. To do this, we can organize related processes into separate classes and define these processes as common static methods.
For example, for the following class:
Class Polygon {
.
.
Public int getPerimeter () {...}
Public boolean isConvex () {...}
Public boolean containsPoint (Point p) {...}
.
.
}
We can rewrite it as:
Class Polygon {
.
.
Public int getPerimeter () {return pPolygon.computePerimeter (this);}
Public boolean isConvex () {return pPolygon.isConvex (this);}
Public boolean containsPoint (Point p) {return pPolygon.containsPoint (this)
P
);}
.
}
Among them, pPolygon is:
Class pPolygon {
Static public int computePerimeter (Polygon polygon) {...}
Static public boolean isConvex (Polygon polygon) {...}
Static public boolean
ContainsPoint (Polygon polygon, Point p) {...}
}
From the name of the class pPolygon, we can see that the process encapsulated by this class is mainly related to objects of type Polygon. The p before the name indicates that the sole purpose of the class is to organize common static processes. In Java, it is non-standard for a class to start with a lowercase letter, but a class like pPloygon does not actually provide the functionality of a normal Java class. In other words, it does not represent a class of objects, it is just a mechanism by which the Java language organizes code.
In the above example, the end effect of changing the code is that the client code that applies the Polygon functionality no longer has to inherit from Polygon. The functionality of the Polygon class is now provided by the pPolygon class on a process-by-process basis. The client code only uses the code it needs, and it doesn't care about the functionality in the Polygon class that it doesn't need. But it does not mean that the role of classes has been weakened in this new type of procedural programming. On the contrary, classes play an indispensable role in organizing and encapsulating object data members, and as described next in this article, the ability of classes to achieve polymorphism through multiple interfaces itself brings excellent code reuse support. However, because encapsulating code functions with an example method is not the preferred means of code reuse, it is not ideal to achieve code reuse and polymorphism support through class inheritance.
Measure 2: change the parameter type to the interface
As Allen Holub pointed out in "Build User Interfaces for Object-Oriented Systems", the real point of code reuse in object-oriented programming is to take advantage of polymorphism through interface parameter types, rather than through class inheritance:
". We achieve code reuse by programming interfaces rather than classes. if all the parameters of a method are references to known interfaces, then the method can manipulate objects whose classes don't even exist when we write code for the method. technically, it is the method that is reusable, not the object passed to the method."
According to Holub's view on the results of measure 1, when a piece of code can be written as an independent global process, we can further improve its reusability as long as the parameters of all its class forms are changed to interface forms. After this change, the parameters of the procedure can be the objects of all classes that implement the interface, not just the objects created by the original class. As a result, the process will be able to manipulate a large number of object types that may exist.
Thank you for reading this article carefully. I hope the article "how to improve the reusability of Java code" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.