多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 示例 ### 渲染页面 页面层作为抽象部分, 而渲染层则作为实现部分 <details> <summary>main.go</summary> ``` package main import ( "fmt" "strings" ) type Renderer interface { readerTitle(string) string readerTextBlock(string) string readerImage(string) string readerLink(string, string) string readerHeader() string readerFooter() string readerParts([]string) string } type Product interface { getId() string getTitle() string getDesc() string getImage() string } type BasePage struct { r Renderer } func (b *BasePage) SetReader(r Renderer) { b.r = r } func (b *BasePage) ChangeReader(r Renderer) { b.r = r } type Page struct { BasePage p Product } func (p *Page) SetProduct(pro Product) { p.p = pro } func (p *Page) view() string { return p.r.readerParts([]string{ p.r.readerHeader(), p.r.readerTitle(p.p.getTitle()), p.r.readerTextBlock(p.p.getDesc()), p.r.readerImage(p.p.getImage()), p.r.readerLink("/cart/add/"+p.p.getId(), "add kart"), p.r.readerFooter(), }) } type HtmlRender struct { } func (h HtmlRender) readerTitle(s string) string { return "<h1>" + s + "</h1>" } func (h HtmlRender) readerTextBlock(s string) string { return "<div class='text'>" + s + "</dib>" } func (h HtmlRender) readerImage(s string) string { return "<image src='" + s + "'></image>" } func (h HtmlRender) readerLink(s string, s1 string) string { return "<a href='" + s + "'>" + s1 + "</a>" } func (h HtmlRender) readerHeader() string { return "<html><body>" } func (h HtmlRender) readerFooter() string { return "</body></html>" } func (h HtmlRender) readerParts(s []string) string { return strings.Join(s, "\n") } type JsonRender struct { } func (h JsonRender) readerTitle(s string) string { return fmt.Sprintf(`"title":"%s"`, s) } func (h JsonRender) readerTextBlock(s string) string { return fmt.Sprintf(`"text":"%s"`, s) } func (h JsonRender) readerImage(s string) string { return fmt.Sprintf(`"img":"%s"`, s) } func (h JsonRender) readerLink(s string, s1 string) string { return fmt.Sprintf(`"link":{"hef":"%s","title":"%s"}`, s, s1) } func (h JsonRender) readerHeader() string { return "" } func (h JsonRender) readerFooter() string { return "" } func (h JsonRender) readerParts(s []string) string { return "{" + strings.Join(s, "\n") + "}" } type AProduct struct { id string title string desc string image string } func (A AProduct) getId() string { return A.id } func (A AProduct) getTitle() string { return A.title } func (A AProduct) getDesc() string { return A.desc } func (A AProduct) getImage() string { return A.image } func main() { a := &AProduct{ id: "123", title: "titleasdasd", desc: "descadsasdas", image: "iamge asdad", } html := HtmlRender{} json1 := JsonRender{} p := Page{} p.SetProduct(a) p.SetReader(html) fmt.Printf("%+v\n", p.view()) fmt.Println() p.SetReader(json1) fmt.Printf("%+v\n", p.view()) } ``` </details> <br /> 输出 ``` <html><body> <h1>titleasdasd</h1> <div class='text'>descadsasdas</dib> <image src='iamge asdad'></image> <a href='/cart/add/123'>add kart</a> </body></html> { "title":"titleasdasd" "text":"descadsasdas" "img":"iamge asdad" "link":{"hef":"/cart/add/123","title":"add kart"} } ```