"Design pattern"--prototype mode vs Template method mode

Source: Internet
Author: User
Tags abstract inheritance

I do not know what the reason is always the prototype model and template method of mixing, really very distressed ah. Do not know whether we have the same trouble, some words of treatment, no words of prevention AH. Therefore this article carries on the detailed comparison study. prototype Mode

The so-called prototype pattern : Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.

explanation : Create another customizable object from one object without needing to know the details of any creation. It is important to note that the copy of the concept is two words, which is the essence of the prototype pattern. The key to prototype pattern implementation is the clone function.

Example : Resume copy, no copier before, completely handwritten, need to write a lot of duplicate resume, if the resume is wrong, change it is quite laborious. Later with the printer, in the presence of a resume error, only need to change a copy, and then copy the content is correct. This can be understood as a prototype pattern, whether it is a copy or a copy, and there is certainly an original first draft in the execution of these methods.

Note: It is important to change the content in the abstract class in order to change the content in the prototype class through prototype schema inheritance. Template Method Mode

Template Method Pattern : Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

explanation : In the process of doing the procedure, will use inheritance, it is necessary to inherit the more valuable, become the template of the subclass, all the duplicated code should rise to the parent class, rather than let each subclass to repeat. When we are going to complete a process or series of steps that are consistent in a particular level of detail, the implementation of the individual steps at a more detailed level may not be the same, using the template method pattern, moving the different places into subclasses, and redefining them when needed.

Example : The same above resume problem, most people can not be a fixed job in a lifetime, so in the process of writing a resume is doomed to have a column is the work experience. But each job experience is probably the same, nothing more than the entry year, the term of work content, separation time. But the specific content is not the same, with the constant job search, this change, not every job to rewrite a prototype it. The template method pattern can be used at this time. The work experience in the approximate process of the abstract class, the specific content to fill in the sub-class. This defines the skeleton of an operation algorithm in an abstract class, delaying the concrete steps into subclasses.

"Summary"

Personal feeling the biggest difference is that the prototype pattern changes must be changed at the root, and template method mode, you need to change the subclass of the row. Of course the two are different in scope. The template method pattern is more inclined to manipulate the algorithm structure, while the prototype mode is applicable to the overall scope. When learning to feel the two particularly like, the process has been confused, the real step down the heart of the analysis of the time really, this is not a thing. So the face of different problems, a method will not change, practical research to do.


I rookie one, personal views, if there is not the right place, there is hope that the great God will not hesitate to advise, it would be appreciated.

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.