Chain Constructionors

zhaozj2021-02-16  150

You have multiple constructs, which contain duplicate code.

The structure is string together to minimize the duplication code.

Public class loan {

...

Public LoaN (Float Notional, Float Outstanding, Int Rating, Date Expiry) {

THIS.STRATEGY = New Termroc ();

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

}

Public Loan (Float Notional, Float OutStanding, Int Rating, Date Expiry, Date Maturity) {

This.strategy = new revolvingtermroc ();

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

THIS.MATURITY = MatureTy;

}

Public Loan (CapitalStrategy Strategy, Float Notional, Float Outstanding,

INT Rating, Date Expiry, Date Maturity) {

THIS.STRATEGY = STRATEGY;

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

THIS.MATURITY = MatureTy;

}

}

Public class loan {

...

Public LoaN (Float Notional, Float Outstanding, Int Rating, Date Expiry) {

This (New Termroc (), Notional, Outstanding, Rating, Expiry, NULL;

}

Public Loan (Float Notional, Float OutStanding, Int Rating, Date Expiry, Date Maturity) {

This (New Revolvingtermroc (), Notional, OutStanding, Rating, Expiry, Maturity

}

Public Loan (CapitalStrategy Strategy, Float Notional, Float Outstanding,

INT Rating, Date Expiry, Date Maturity) {

THIS.STRATEGY = STRATEGY;

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

THIS.MATURITY = MatureTy;

}

}

The motive writes duplicate code in two or more constructors of the same class, which is to bury the harsh seeds. Others will add new variables in your class, then update a structure to initialize this variable, but I forgot to update other constructors. So, "" asked to the new bug. The more constructs in a class, the more you hurt you. If possible, you should try to reduce or remove the code repetition. This additional benefit is that you can help your code system to lose weight. In order to achieve this goal, we often use the constructor chaining mode to refactor: Specific constructor-purposed structure, repeat this process, until the most artifacts are also called . If your end of each call chain is the same constructor, I call it "catch-all" constructor because it processes all constructor calls. This Catch-ALL structure is usually accepting more parameters than other constructors, and may be (or may not be) private or protected.

If you find that multiple constructs reduce the availability of classes, consider the reconstruction method using "replacing multiple constructs with a Factory Method mode".

Communication Repeat Simplification If multiple constructs in a class are realizing duplicate work, in the communication of specialization and generalization, your code is failed. To achieve this kind of communication, it should make the structural subculture of the specialization, and let each structure do their own unique work. The duplicate code in the constructor makes your class easier to make mistakes, making it harder. Looking for a universal function, put it into the conformation of the conformation, forwarding the call to these artificial components, and achieves uncommon functions in other constructors. If more than one constructor contains the same code, it is difficult to see the difference between the structure. The structural subcarpse of the specialization is made, and a call chain is formed to simplify the structure.

Procedure 1. Look for two constructors that contain duplicate code (I called them A and B). Determine whether A can call B or B whether to call A, such a repeating code can be securely deleted from this one of this constructor (and relatively easy).

2. Compile, test.

3. Repeat steps 1 and 2 for each structure in the class to obtain as little repetitive code.

4. If a structure is unnecessary, it changes its visibility.

5. Compile, test.

Example 1. We will start from a simple code - a Loan class - start. This class has three constructors to show three different types of loan services. A large number of ugly repeating code.

Public LoaN (Float Notional, Float Outstanding, Int Rating, Date Expiry) {

THIS.STRATEGY = New Termroc ();

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

}

Public Loan (Float Notional, Float OutStanding, Int Rating, Date Expiry, Date Maturity) {

This.strategy = new revolvingtermroc ();

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY; this.maturity = maturity;

}

Public Loan (CapitalStrategy Strategy, Float Notional, Float Outstanding, INT Rating,

Date expiry, date maturity) {

THIS.STRATEGY = STRATEGY;

THIS.NOTIAL = NOTINATIONAL;

This.outstanding = OutStanding;

THIS. RATING = Rating;

THIS.EXPIRY = EXPIRY;

THIS.MATURITY = MatureTy;

}

I have studied the two constructors in front. They contain duplicate code, while the third structure is also. I consider which one should call which one should call and find that it should call the third construct. So I modified the first structure to:

Public LoaN (Float Notional, Float Outstanding, Int Rating, Date Expiry) {

This (New Termroc (), Notional, Outstanding, Rating, Expiry, NULL;

}

2. Compile the program, test whether this modification works normally.

3. Repeat steps 1 and 2, try to remove the code repetition. This leads to the second structure, and it also calls the third structure, as shown below:

Public Loan (Float Notional, Float OutStanding, Int Rating, Date Expiry, Date Maturity) {

This (New Revolvingtermroc (), Notional, OutStanding, Rating, Expiry, Maturity

}

Now I know that the third construct is my catch-all structure, because it processes all constructor details.

4. Check all the caller of these three constructs to determine if a visibility can be changed. In this case, I can't do this (assuming this - here you can't know other code).

5. Compile and test, complete this reconstruction.

转载请注明原文地址:https://www.9cbs.com/read-8980.html

New Post(0)