1

#yyds干货盘点#什么是MVC模式

 2 years ago
source link: https://blog.51cto.com/u_12040959/5155800
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1.原理简介

#yyds干货盘点#什么是MVC模式_sed

MVC是一个架构,或者说是一个设计模式,它就是强制性使应用程序的输入,处理和输出分开。将一个应用程序分为三个部分:Model,View,Controller。

2.1控制器

controller就是与前端界面进行交互的程序,常用的就是rest接口接收前端请求,并把结果返回给前端。

package v1alpha2

import (
"container_cloud/pkg/api"
"container_cloud/pkg/apiserver/query"
"container_cloud/pkg/apiserver/runtime"
"container_cloud/pkg/controller"
"container_cloud/pkg/domain"
"github.com/emicklei/go-restful"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"net/http"
)

const (
GroupName = "job.ictnj.io"
Version = "v1alpha2"
Ok = "OK"
)

var GroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}

func AddToContainer() *restful.WebService {
ws := runtime.NewWebService(GroupVersion)
job := controller.NewSingleClusterJob()
ws.Route(ws.GET("clusters/{cluster}/namespaces/{namespace}/jobName/{name}").
To(job.IsExist).
Doc("Check duplicate names").
Param(ws.PathParameter("cluster", "cluster used to do filtering")).
Param(ws.PathParameter("namespace", "namespace used to do filtering")).
Param(ws.PathParameter("name", "name used to do filtering")).
Returns(http.StatusOK, "ok", domain.Exist{}))
ws.Route(ws.POST("/namespaces/{namespace}/jobs").
To(job.CreateSingleClusterJobs).
Doc("Create a job for a specified federatednamespaces.").
Param(ws.PathParameter("namespace", "the watching namespace of the job")).
Returns(http.StatusOK, "ok", batchv1.Job{}))

ws.Route(ws.DELETE("/clusters/{cluster}/namespaces/{namespace}/jobs/{name}").
To(job.DeleteSingleClusterJob).
Doc("delete a specified job").
Param(ws.PathParameter("cluster", "cluster used to do filtering")).
Param(ws.PathParameter("namespace", "namespace used to do filtering")).
Param(ws.PathParameter("name", "name used to do filtering")).
Returns(http.StatusOK, Ok, batchv1.Job{}))

ws.Route(ws.GET("/jobs").
To(job.GetSingleClusterJobList).
Doc("get job list").
Param(ws.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, Ok, api.ListResult{}))

ws.Route(ws.GET("/clusters/{cluster}/namespaces/{namespace}/jobs/{name}").
To(job.GetJob).
Doc("get a job").
Param(ws.PathParameter("cluster", "cluster used to do filtering")).
Param(ws.PathParameter("namespace", "namespace used to do filtering")).
Param(ws.PathParameter("name", "name used to do filtering")).
Returns(http.StatusOK, Ok, domain.Job{}))

return ws
}

比如上述代码就是控制器的代码,rest接口交互

2.2 Model模型

主要完成业务逻辑,数据库交互。以及各种数据对象。

package vpc

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

/*
@Time : 2022/3/10 14:44
@Author :dengfeng_hu
@File : subnet
@Software: GoLand
*/

type Subnet struct {

//uuid
Id string `json:"subnetId,omitempty"`

//subnet名称
SubnetName string `json:"subnetName,omitempty"`

//vpc名称
VpcName string `json:"vpcName,omitempty"`

//vpcId
VpcId string `json:"vpcId,omitempty"`

//集群名称
ClusterName string `json:"clusterName,omitempty"`

//关联命名空间
Namespace string `json:"namespaces,omitempty"`

//子网网段
SubnetNetwork string `json:"subnetNetwork,omitempty"`
}

type SubnetItem struct {

//集群名称
ClusterName string `json:"clusterName"`

//vpc名称
VpcName string `json:"vpcName"`

//命名空间
Namespace string `json:"namespace"`

//vpc网段
VpcNetwork string `json:"vpcNetwork"`

//子网网段
SubnetNetwork string `json:"subnetNetwork"`

//子网名称,前端
SubnetName string `json:"subnetName"`

//用户名
UserName string `json:"userName"`

//vpcId
VpcId string `json:"vpcId"`

//返回给前端用的
CreateTime string `json:"createTime"`
}
type SubnetTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec SubnetSpec `json:"spec"`
}
type SubnetSpec struct {
CidrBlock string `json:"cidrBlock,omitempty"`
Vpc string `json:"vpc,omitempty"`
}

2.3.view视图

这个就是前端界面需要做的。前端开发人员开发的界面,因为小编是纯后端,就不介绍了。

​1、​分工明确​(开发人员可以只关注整个结构中的其中某一层):使用MVC可以把数据库开发,程序业务逻辑开发,页面开发分开,每一层都具有相同的特征,方便以后的代码维护,它使程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中精力于表现形式上。​

​2、​松耦合​(可以降低层与层之间的依赖):视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的业务流程或者业务规则的改变只需要改动MVC的模型层即可。因为模型与控制器和视图相分离,所以很容易改变应用程序的数据层和业务规则。 ​

​3、​复用性高​(利于各层逻辑的复用):像多个视图能够共享一个模型,不论你视图层是用flash界面或是wap界面,用一个模型就能处理他们。将数据和业务规则从表示层分开,就可以最大化从用代码。、​

​4、​有利于标准化​(有利于通过工程化、工具化产生管理程序代码);​

​概括来说,分层式设计可以达至如下目的:分散关注、松散耦合、逻辑复用、标准定义​

4.缺点:

分层式结构也不可避免具有一些缺陷:

1、有时会导致级联的修改。这种修改尤其体现在自上而下的方向。如果在表示层中需要增加一个功能,为保证其设计符合分层式结构,可能需要在相应的业务逻辑层和数据访问层中都增加相应的代码。

2、降低了系统的性能。这是不言而喻的。如果不采用分层式结构,很多业务可以直接造访数据库,以此获取相应的数据,如今却必须通过中间层来完成。

3、由于它没有明确的定义,所以完全理解MVC并不是很容易。使用MVC需要精心的计划,由于它的内部原理比较复杂,所以需要花费一些时间去思考。

4、MVC并不适合小型甚至中等规模的应用程序,花费大量时间将MVC应用到规模并不是很大的应用程序通常会得不偿失。 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK