Template method Mode

Source: Internet
Author: User

Templatemethod


UML Class Diagrams:


Instance implementation code:


    Abstract class AbstractClass    {public        abstract void PrimitiveOperation1 ();        public abstract void PrimitiveOperation2 ();        public void Templatemethod ()        {            PrimitiveOperation1 ();            PrimitiveOperation2 ();            Console.WriteLine ("");        }    }    Class Concreteclassa:abstractclass    {public        override void PrimitiveOperation1 ()        {            Console.WriteLine ("Concrete Class A Method 1 implementation");        }        public override void PrimitiveOperation2 ()        {            Console.WriteLine ("Concrete Class A Method 2 implementation");        }    }    Class Concreteclassb:abstractclass    {public        override void PrimitiveOperation1 ()        {            Console.WriteLine ("Concrete class B Method 1 Implementation");        }        public override void PrimitiveOperation2 ()        {            Console.WriteLine ("Concrete class B Method 2 Implementation");        }    }


Client implementations:

         AbstractClass C;         c = new Concreteclassa ();         C.templatemethod ();         c = new Concreteclassb ();         C.templatemethod ();         Console.read ();

Description

The skeleton of an algorithm in an operation is defined, and some steps are deferred to subclasses, and the template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

The template method pattern is to realize its advantage by moving the invariant behavior to the superclass and removing the duplicated code in the subclass.


PS: The template method pattern is somewhat similar to the builder pattern, which is achieved by putting stable behavior in the superclass, which is accomplished by introducing a new builder class.


Resources

-"Liar design Mode"

Template method Mode

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.