Home >Java >javaTutorial >Improve the reusability of java code

Improve the reusability of java code

angryTom
angryTomOriginal
2019-07-16 17:35:182283browse

What are the methods and measures to improve the reusability of java code? The following explains three methods to improve the reusability of java code. Let’s learn about it together~

1. Overriding the instance methods of a class

Achieving code reuse through class inheritance is not an exact code reuse technique, so it is not the most ideal code reuse mechanism. Inheritance always brings some redundant methods and data members, which always complicates code that reuses a method in a class.

In addition, the dependence of the derived class on the parent class also further complicates the code: changes to the parent class may affect the subclass; it is difficult for us to remember when modifying either the parent class or the subclass. Which method is overridden by the subclass and which method is not overridden by the subclass? Finally, it is sometimes not obvious whether the overridden method in the subclass should call the corresponding method in the parent class.

Any method, as long as it performs the task of a single concept, by itself, should be the preferred reusable code. In order to reuse this code, we must return to the procedure-oriented programming model and move the class instance methods out into global procedures.

To improve the reusability of such procedures, procedural code should be written like a static utility method: it can only use its own input parameters, can only call other global procedures, and cannot use any non-local methods. variable. This restriction on external dependencies simplifies the application of procedures, allowing them to be easily used anywhere.

Of course, since this kind of organization always makes the code have a clearer structure, even code that does not consider reusability can also benefit from it.

In Java, methods cannot exist independently of classes. To this end, we can organize related processes into independent classes and define these processes as public static methods. Classes play an indispensable role in the process of organizing and encapsulating object data members. Classes are implemented through multiple interfaces. The ability of polymorphism itself also brings excellent code reuse support.

2. Choose the simplest parameter interface type

The simpler the interface required to describe the parameter object, the less chance other classes will implement the interface. The larger the value, the more classes whose objects can be used as parameters. This can be easily seen from the following example:

static public boolean areOverlapping(Window window1, Window window2) {...}

This method is used to check whether two windows overlapping. If this method only requires the rectangular coordinates of the two windows to be obtained from the parameters, it is a better choice to simplify the two parameters accordingly. Sometimes the interface that describes the parameter requirements may have too many methods.

At this point, we should define a new public interface in the global namespace for reuse by other code facing the same problem. When we need to use parameters like function pointers in C language, creating a unique interface describing the parameter requirements is the best choice. For example, assume the following process:

static public void sort(List list, SortComparison comp) {...}

This method uses the comparison object comp provided in the parameter and compares it to Sorts the objects in a given list list. The only requirement of sort on the comp object is to call a method for comparison, so the SortComparison interface cannot be reused elsewhere.

3. Change parameter types to interfaces

In object-oriented programming, the real point of code reuse is to utilize multiple interface parameter types Statefulness, rather than class inheritance, we achieve code reuse by programming interfaces instead of classes. If all the parameters of a method are references to some known interface, then this method can operate on objects whose classes do not even exist when we write the method's code. Technically, it is the method that is reusable, not the object passed to the method.

For example, let us assume that there is such a method:

static public boolean areAnyOverlapping(Collection rects) {...}

This method is used to check that the given collection Whether any of the rectangular objects overlap. Inside this method, when we use a loop to access each object in the collection in sequence, if we cannot cast the object into an interface type such as Rectangular, how can we access the rectangular area of ​​the object? The only option is to cast the object into its unique class form, which means that the method must know in advance the object type it operates on, thus limiting the reuse of the method to those object types.

For more relevant knowledge, please click: java tutorial

The above is the detailed content of Improve the reusability of java code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn