Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve imperfect Library classes in Java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to solve the imperfect library classes in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Imperfect library class

Imperfect library class (Incomplete Library Class)

When a class library can no longer meet the actual needs, you have to change the library (if the library is read-only, there is nothing you can do about it).

Cause of the problem

Many programming techniques are based on library classes. The authors of the library class do not use the ability of prophecy, and can not blame them for it. The trouble is that libraries are often not well constructed, and it is often impossible for us to modify the classes in them to meet our needs.

Solution method

If you only want to modify one or two functions in the class library, you can use the introduction of additional functions (Introduce Foreign Method)

If you want to add a lot of extra behavior, you have to use the introduction of local extensions (Introduce Local Extension).

Income

Reduce code duplication (you don't have to implement all the functions of a library yourself without a word disagreement, the cost is too high)

When to ignore

If you extend the library, it will bring extra work.

Description of refactoring method

Introduction of external function (Introduce Foreign Method)

problem

You need to add a function to the class that provides the service, but you cannot modify the class.

Class Report {

/ /...

Void sendReport () {

Date nextDay = new Date (previousEnd.getYear ()

PreviousEnd.getMonth (), previousEnd.getDate () + 1)

/ /...

}

}

Solve

Create a function in the customer class and pass in an instance of the service class in the form of a first parameter.

Class Report {

/ /...

Void sendReport () {

Date newStart = nextDay (previousEnd)

/ /...

}

Private static Date nextDay (Date arg) {

Return new Date (arg.getYear (), arg.getMonth (), arg.getDate () + 1)

}

}

Introduce local extensions (Introduce Local Extension)

problem

You need to provide some extra functions for the service class, but you cannot modify the class.

Middleman

Man in the middle (Middle Man)

If the function of one class is simply to delegate to another class, why does it exist?

Cause of the problem

One of the basic features of an object is encapsulation: hiding its internal details from the outside world. Encapsulation is often accompanied by delegation. But people may overuse delegation. For example, you may see that most of the useful work of one class is delegated to other classes, and the class itself becomes an empty shell and does nothing but delegate.

Solution method

You should use the removal of Remove Middle Man to deal directly with the person you are really responsible for.

Income

Reduce clunky code.

When to ignore

Do not delete the created middleman if:

Middlemen are added to avoid dependencies between classes.

Some design patterns purposefully create middlemen (such as proxy patterns and decorator patterns).

Description of refactoring method

Remove middleman (Remove Middle Man)

problem

A class does too many simple delegate actions.

Attachment complex

Attachment complex (Feature Envy)

A function accesses more data from other objects than from its own.

Cause of the problem

This smell may occur after the field is moved to the data class. If this is the case, you may want to move the operations of the data class to this class.

Solution method

As a basic rule, if things change at the same time, you should keep them in the same place. Usually data and functions that use this data are changed together (although exceptions are possible).

There is a basic principle: things that change at the same time should be put in the same place. Usually, the data and the functions that use it are changed together

A function should obviously be moved to another place, using the shift function (Move Method).

If only part of the code of the function accesses another object's data, use the Extract Method to move that part of the code to a separate function.

If a method uses functions from several other classes, first determine which class contains most of the data used. Then, put the method in this class along with other data. Alternatively, use Extract Method to split the method into parts that can be placed in different locations in different classes.

Income

Reduce repetitive code (if the code for data processing is placed in a central position).

When to ignore

Sometimes, the behavior is deliberately separated from the class that holds the data. The usual advantage of this is the ability to change behavior dynamically (see Policy Design patterns, Visitor Design patterns, and other patterns).

Description of refactoring method

Shift function (Move Method)

problem

In your program, there is a function that communicates more with another class than the one it resides in: call the latter, or be called by the latter.

Refinement function (Extract Method)

problem

You have a piece of code that can be organized together.

Void printOwing () {

PrintBanner ()

/ / print details

System.out.println ("name:" + name)

System.out.println ("amount:" + getOutstanding ())

}

Solve

Move this code to a new function and replace the old code with a call to the function.

Void printOwing () {

PrintBanner ()

PrintDetails (getOutstanding ())

}

Void printDetails (double outstanding) {

System.out.println ("name:" + name)

System.out.println ("amount:" + outstanding)

}

Intimate relationship

Intimate relationship (Inappropriate Intimacy)

One class makes extensive use of the internal fields and methods of another class.

Cause of the problem

Classes should perceive each other as little as possible (reduce coupling). Such classes are easier to maintain and reuse.

Solution method

The simplest solution is to use Move Method and Move Field to break between classes.

You can also see if you can change a two-way association to an one-way association (Change Bidirectional Association to Unidirectional) so that one class breaks up with the other.

If these two classes are really stronger than gold and are inseparable, you can use Extract Class to refine what they have in common into a new class and let them produce the crystallization of love. Alternatively, you can try to use hidden delegate relationships (Hide Delegate) to get another class to match them.

Inheritance often leads to excessive closeness between classes, because subclasses always know more about superclasses than the latter's subjective wishes. If you think you should let this subclass go its own way, use delegation instead of inheritance (Replace Inheritance with Delegation) to separate superclasses from subclasses.

Income

Improve the organization of code.

Improve code reusability.

Description of refactoring method

Shift function (Move Method)

problem

In your program, there is a function that communicates more with another class than the one it resides in: call the latter, or be called by the latter.

Solve

Create a new function with similar behavior in the class most frequently referenced by this function. Turn the old function into a simple delegate function, or remove the old function completely.

Move Field (Move Field)

problem

In your program, a field is used more often by a class other than the one in which it resides.

Solve

Create a new field in the target class and modify all users of the source field to use the new field instead.

Change two-way association to one-way association (Change Bidirectional Association to Unidirectional)

problem

There is a two-way association between the two classes, but one class now no longer needs the features of the other.

Solve

Remove unnecessary connections.

Refined class (Extract Class)

problem

A class does more than one thing.

Solve

Create a new class and move related fields and functions from the old class to the new class.

Hide delegation relationship (Hide Delegate)

problem

The client calls another object through a delegate class.

Solve

Create all the functions the customer needs on the service class to hide the delegate relationship.

Replace inheritance with delegation (Replace Inheritance with Delegation)

problem

A subclass uses only part of the superclass interface, or data that does not need to be inherited at all.

Solve

Create a new field in the subclass to hold the superclass; adjust the subclass function to delegate the superclass instead; and then remove the inheritance relationship between the two.

Overcoupled message chain

Overcoupled message chain (Message Chains)

The form of the message chain is similar to: obj.getA (). GetB (). GetC ().

Cause of the problem

If you see a user requesting another object from one object, then another object from the latter, and then another object. This is the message chain. What you see in the actual code may be a long list of getThis () or a long list of temporary variables. Taking this approach means that the customer code will be closely coupled to the navigation during the lookup process. Once there is any change in the relationship between objects, the client has to make changes accordingly.

Solution method

You can delete a message chain using a hidden delegate relationship (Hide Delegate).

Sometimes a better choice is to first observe what the final object of the message chain is used for. See if you can Extract Method the code that uses the object into a separate function, and then use the Move Method function to push the function into the message chain.

Income

Can reduce the dependency between classes in the chain.

Can reduce the amount of code.

When to ignore

Overly aggressive delegation can make it difficult for programmers to understand how functions are triggered.

Description of refactoring method

Hide delegation relationship (Hide Delegate)

problem

The client calls another object through a delegate class.

Solve

Create all the functions the customer needs on the service class to hide the delegate relationship.

Refinement function (Extract Method)

problem

You have a piece of code that can be organized together.

Void printOwing () {

PrintBanner ()

/ / print details

System.out.println ("name:" + name)

System.out.println ("amount:" + getOutstanding ())

}

Solve

Move this code to a new function and replace the old code with a call to the function.

Void printOwing () {

PrintBanner ()

PrintDetails (getOutstanding ())

}

Void printDetails (double outstanding) {

System.out.println ("name:" + name)

System.out.println ("amount:" + outstanding)

}

Shift function (Move Method)

problem

In your program, there is a function that communicates more with another class than the one it resides in: call the latter, or be called by the latter.

Solve

Create a new function with similar behavior in the class most frequently referenced by this function. Turn the old function into a simple delegate function, or remove the old function completely.

This is the end of this article on "how to solve imperfect library classes in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report