Growing Pains: Preliminary design Patterns

Source: Internet
Author: User
Tags bool comparison file size return sort
Design believe that a lot of people like to see this comedy, I am very fond of, including the growth of the vicissitudes of life, you can find the footprints of your growth.

is the path of programming growth not like this?

This is where the story begins.

Xiao Wang, a newly-graduated student, entered a software company with a good salary. Young people are full of energy and have lofty goals. The first three days attended the company's training, three days did not write code, preface. On the fourth day, the project manager came over and said, "Xiao Wang, write an integer list of sorting algorithm, we need to use in the project." ”

Bubble is the first in the mind of Xiao Wang emerged. Open a certain Bible, pick up the paragraph bubble algorithm, modify some of the Code writing style (some of the biblical code style is not our), the code is roughly the same:

BOOL Sort (Listint)

{

Bubble Sort algorithm

{

Comparison statements

}

return TRUE;

}

Xiao Wang examined it and tested it with a test case to ensure that it was foolproof and handed over to the manager. The manager said a good word, happy little Wang.

The next day, the manager ran over and said: "Change your code yesterday, now to compare floating-point type, and can speed up a bit?" ”

Xiao Wang online check, select a fast sorting algorithm, do not forget to write a backup yesterday, and then in the function of the basis of yesterday's change. The code is roughly the same:

BOOL Sort (Listint)

{

Fast sorting algorithm

{

Comparison statements

}

return TRUE;

}

Is it easy? Test.

A year later ...

LENS switch ...

Xiao Wang sits in front of the computer and skillfully writes the program, and next to this "design pattern" book. Know the object-oriented programming, know the design pattern, but the understanding is not deep enough. The sort algorithm also evolves to compare file names.

One day the manager came over and said, "Xiao Wang, now our sorting algorithm to use in embedded platform, you do some algorithmic research work, give a report." ”

Is this not a typical application of the strategy model? Define a series of algorithms that encapsulate them one by one and enable them to convert to each other.

Xiao Wang drew a uml diagram:

In this way, Xiao Wang put some popular sorting algorithms have tried again, a total of seven or eight kinds, a new algorithm speed is also very fast, the novel algorithm inserted into the system, the old algorithm from the system "retirement", implement plug-in replacement.

Csort *psort = new Cbubblesort;

Cclient.listsort (Psort);

If you want to change to a quick sort, as long as:

Csort *psort = new Cquicksort;

Cclient.listsort (Psort);

Test, of course, the manager himself also have ideas, and let Xiao Wang tried another several algorithms, Xiao Wang can easily complete. The role of the strategy model here incisively and vividly, Xiao Wang has a special sense of accomplishment.

After a few days, the customer proposed to be sorted by filename, date, Xiao Wang thought it was relatively simple, changed the UML diagram:

The main job of changing code is copy-paste, four functions, also quickly completed.

Customer needs will not stop, in order to enhance the function, put forward the need by file size, file type sorting, God knows what customers will ask.

"Can no longer live like this", Xiao Wang listened to the song, fell into meditation.

What about the sorting algorithm and the comparison algorithm? Decoupling them allows the two to change independently. I'm sure I've seen it before. "Xiao Wang busy Open the design mode, and began to consult."

"Got it, this is not the bridge model." "A moment of rejoicing, do it at once." After half an hour, the UML diagram came out, as follows:

The client code is as follows:

Csort *psort = new Cquicksort;

Ccomparetype *ptype = new Cnamecompare;

Psort->settype (PType);

Psort->sort (plist);

Haha, customers, please do not hesitate to ask.



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.