go设计模式之外观模式

栏目: Go · 发布时间: 5年前

内容简介:这篇是设计模式中结构模式的第一篇。微服务架构现在是系统的架构的主流,它将系统拆分成一个个独立的服务,服务之间通过通信建立起关联关系。假设现在有一个博客的系统,它由四个微服务组成。用户服务,文章管理服务,分类服务,评论服务。系统的微服务间会发生以下的服务关系。服务间的调用关系比较混乱,微服务架构中通过一个网关来解决这种混乱的服务间调用,通过网关统一对外服务。

这篇是 设计模式 中结构模式的第一篇。微服务架构现在是系统的架构的主流,它将系统拆分成一个个独立的服务,服务之间通过通信建立起关联关系。假设现在有一个博客的系统,它由四个微服务组成。用户服务,文章管理服务,分类服务,评论服务。系统的微服务间会发生以下的服务关系。

go设计模式之外观模式

服务间的调用关系比较混乱,微服务架构中通过一个网关来解决这种混乱的服务间调用,通过网关统一对外服务。

看一下改进后的调用关系图。

go设计模式之外观模式

这样改进后,调用关系就变得清晰明了。结构图中的网关就是一个要展开的外观模式结构。

接下来通过 go 语言实现这种外观模式。

package main

import "fmt"

type Facade struct {
	UserSvc UserSvc
	ArticleSvc ArticleSvc
	CommentSvc CommentSvc
}

// 用户登录
func (f *Facade) login(name, password string) int {
	user := f.UserSvc.GetUser(name)
	if password == user.password {
		fmt.Println("登录成功!!!")
	}
	return user.id
}

func (f *Facade) CreateArticle(userId int, title, content string) *Article {
	articleId := 12345
    article := f.ArticleSvc.Create(articleId, title, content, userId)
	return article
}

func (f *Facade) CreateComment(articleId int, userId int, comment string) *Comment {
	commentId := 12345
	cm := f.CommentSvc.Create(commentId, comment, articleId, userId)
	return cm
}

// 用户服务
type UserSvc struct {
}

type User struct {
	id int
	name string
	password string
}

func (user *UserSvc) GetUser(name string) *User {
    if name == "zhangsan" {
		return &User{
			id: 12345,
			name: "zhangsan",
			password: "zhangsan",
		}
	} else {
		return &User{}
	}
}

// 文章服务
type ArticleSvc struct {
}

type Article struct {
	articleId int
	title string
	content string
	authorId int
}

func (articleSvc *ArticleSvc) Create(articleId int, title string, content string, userId int) *Article {
   return &Article {
	   articleId: articleId,
	   title: title,
	   content: content,
	   authorId: userId,
   }
}

// 评论服务
type CommentSvc struct {
}

type Comment struct {
	commentId int
	comment string
	articleId int
	userId int
}

func (commentSvc *CommentSvc) Create(commentId int, comment string, articleId int, userId int)  *Comment {
	return &Comment{
		commentId: commentId,
		comment: comment,
		articleId: articleId,
		userId: userId,
	}
}


func main() {
	f := &Facade{}
	userId := f.login("zhangsan", "zhangsan")
	fmt.Println("登录成功,当前用户Id", userId)

	title := "go设计模式外观模式"
	content := "外观模式是结构模式的一种。。。。"
	article := f.CreateArticle(userId, title, content)
	fmt.Println("文章发表成功,文章id", article.articleId)

	comment := f.CreateComment(article.articleId, userId, "介绍的很详细")
	fmt.Println("评论提交成功,评论id", comment.commentId)
}复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网运营之道

互联网运营之道

金璞、张仲荣 / 电子工业出版社 / 2016-1 / 49.00

《互联网运营之道》内容由运营方法论切入,包括运营的江湖地位、运营的基本逻辑、运营的三大手段(内容运营、活动运营和用户运营)、数据统计方法,等等。虽然是讲方法论,但内容上却有着深入的运营逻辑思考和大量实战案例验证。在讲解了方法论之后,《互联网运营之道》逐步深入剖析如何反脆弱,如何做运营创新,以及如何从小到大切入细分市场,等等。 对于互联网公司来说,产品设计部门和研发部门保证了创意的实现,是从0......一起来看看 《互联网运营之道》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具