"Cold River Snow" Go Implementation Template Method mode

Source: Internet
Author: User
Template Pattern

The template method design pattern allows the different parts of the object to be abstracted, executing the same logic in the same piece of code, and increasing extensibility. In the go language, implementations are implemented by the underlying object, and the behavior is controlled by the top-level method. Implement

Package template

Import "FMT"

type Tmpl interface{
    dosomething ()
    doanything ()
}

type Tmpla struct{

}

func (this *tmpla) dosomething () {
    fmt. Println ("tmpla.dosomething")
}

func (this *tmpla) doanything () {
    fmt. Println ("tmpla.doanything")
}

type TMPLB struct{

}

func (this *tmplb) dosomething () {
    fmt. Println ("tmplb.dosomething")
}

func (this *tmplb) doanything () {
    fmt. Println ("tmplb.doanything")
}

func Operate (Tmpl tmpl) {
    Tmpl. DoSomething ()
    Tmpl. Doanything ()
}

Use

Package main

Import (
    "Projects/designpatternsbygo/behavioralpatterns/template"
)

func main () {
    A: = template. tmpla{}
    B: = template. tmplb{}
    template. Operate (&a)
    template. Operate (&b)

}
Money to hold a court, no money in the holding of personal field. It's not easy to come out.

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.