Growing troubles (continued)

Source: Internet
Author: User

Another year......
Lens switching......
Because of the outstanding performance of Mr. Wang, Mr. Wang is now a small project manager. Another book, reconstruction: ImprovingCode. SortedAlgorithmIt is also relatively stable, and sorting has become an independent module, which is used in many places. However, the demand has changed, so we need to use the Bubble Sorting Algorithm. "My God, why didn't you say that? ." John looked up, and a total15The same code is used. The smell of code. "What should I do if I want to add more in the future? What if I want to change the Sorting Algorithm in the future ?" After all, the current Mr. Wang is not at the previous level and will think twice before encountering problems. A voice is calling: "using the factory method (Factory method." The code is reconstructed.UMLThe figure is as follows:

On the clientProgramInitialize the factory at the beginning:
Csortfactory *Psortfactory = new cbubblesortfactory;
Then, change all the calls to the following code:
Csort * psort= Psortfactory-> Create ();
Ccomparetype* Ptype = new cnamecompare;
Psort-> settype (ptype );
Psort-> sort (plist );
After half an hour 15 All the locations are modified. Do you know the benefits of doing so? In the future 50 Are you sure you want to modify it? Keep in your mind Requirements are constantly changing. If you want to change the algorithm, you only need to modify one of them. Cbubblesortfactory Replace.
Is it true that the problem is completely solved? Taking the initiative is to grasp the fate. After three years of training, Mr. Wang is always at the heart of the eye, preventing both risks and unexpected problems.
Here is an unstable factor Compare type , Won't it change? In fact, the above Code should be like this:
Csort * psort= Psortfactory-> Create ();
If (ntype = sortname)
Ccomparetype* Ptype = new cnamecompare;
Else if (ntype = sortdate)
Ccomparetype* Ptype = new cdatecompare;
Else
Ccomparetype* Ptype = new csizecompare;
Psort-> settype (ptype );
Psort-> sort (plist );
Mr. smith cannot tolerate this hidden danger and uses a simple factory model (Simple Factory. "Discover changes and encapsulate it ."UMLThe figure is as follows:

Set If-Else , Switch Move Csimplecomparefactory Of Create Function. Add the following sentence to the client program initialization code:
Csimplecomparefactory* Psimplefactory = new csimplecomparefactory;
The code of the calling place is changed:
Csort * psort= Psortfactory-> Create ();
Ccomparetype* Ptype = Psimplefactory-> C Reate (nsortname );
Psort-> settype (ptype );
Psort-> sort (plist );
"If-Else,SwitchIsn't the statement still there ?" One voice is asking.
"Why are there so many problems? Isn't itIf-Else,SwitchCannot the statement be used ?" Sometimes, you don't have to force the changes. You only need to minimize the changes. In most cases, the program code can be modified.
 

The above design can solve most of the expansion.
However, the days are still going on, the customer is still asking for the demand, and Mr. Smith continues his dream.
Only changes remain unchanged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.