当前位置: 首页 > news >正文

分类网站建设方案做学校教务处网站

分类网站建设方案,做学校教务处网站,p2p贷款网站开发,东莞网站建设的价格在这个框架中#xff0c;构建可以分为两部分。一是对象的构建#xff0c;二是关系的构建。 对象的构建 自动构建 自动构建是指框架依据配置文件#xff0c;自行创建出其描述的对象。 在自动构建前#xff0c;我们需要向对象工厂注册各个自定义的类型。比如example_mix例…在这个框架中构建可以分为两部分。一是对象的构建二是关系的构建。 对象的构建 自动构建 自动构建是指框架依据配置文件自行创建出其描述的对象。 在自动构建前我们需要向对象工厂注册各个自定义的类型。比如example_mix例子中 func main() {factory : factory.NewFactory()factory.Register(reflect.TypeOf(examplelayera.ExampleA1Handler{}))factory.Register(reflect.TypeOf(examplelayera.ExampleA2Handler{}))factory.Register(reflect.TypeOf(examplelayera.ExampleADivider{}))……Factory在底层维护了一个类型名和其反射Type的映射。 func (f *Factory) Register(concreteType reflect.Type) (err error) {concreteName : concreteType.Name()if _, ok : f.concretesType[concreteName]; !ok {f.concretesType[concreteName] concreteType} else {err fmt.Errorf(concreteName is already registered.Please modify name.)panic(err.Error())}return nil }后续框架在读取配置文件的过程中会根据type字段的值构建对象。 func (f *Factory) Create(concreteTypeName string, configure []byte, constructorInterface any) (concrete any, err error) {concreteType, ok : f.concretesType[concreteTypeName]if !ok {err fmt.Errorf(concrete name not found for %s, concreteTypeName)panic(err.Error())}concrete reflect.New(concreteType).Interface()concreteInterface, ok : concrete.(frame.ConcreteInterface)if !ok || concreteInterface nil {err fmt.Errorf(concrete %s conver to ConcreteInterface error, concreteTypeName)panic(err.Error())}constructorSetterInterface, ok : concrete.(frame.ConstructorSetterInterface)if ok constructorSetterInterface ! nil constructorInterface ! nil {constructorSetterInterface.SetConstructorInterface(constructorInterface)}loadConfigFromMemoryInterface, ok : concrete.(frame.LoadConfigFromMemoryInterface)if ok loadConfigFromMemoryInterface ! nil {err : loadConfigFromMemoryInterface.LoadConfigFromMemory(configure)if err ! nil {err fmt.Errorf(concrete LoadConfigFromMemory error for %s .error: %v, concreteTypeName, err)panic(err.Error())}}loadEnvironmentConfInterface, ok : concrete.(frame.LoadEnvironmentConfInterface)if ok loadEnvironmentConfInterface ! nil {err : loadEnvironmentConfInterface.LoadEnvironmentConf()if err ! nil {err fmt.Errorf(concrete LoadEnvironmentConf error for %s .error: %v, concreteTypeName, err)panic(err.Error())}}concreteName : concreteInterface.Name()if concreteName {err fmt.Errorf(concretes Name is empty for %s, concreteTypeName)panic(err.Error())}if _, ok : f.concretes[concreteName]; !ok {f.concretes[concreteName] concreteInterface} else {err fmt.Errorf(concretes Name %s has already been used, type is %s, concreteName, concreteTypeName)panic(err.Error())}return }被自动构建的对象会自动保存起来并通过下面的方法获取 func (f *Factory) Get(concreteName string) (frame.ConcreteInterface, error) {if concrete, ok : f.concretes[concreteName]; ok {return concrete, nil} else {return nil, nil} }举个例子配置文件目录下存在layer_center.yaml文件 # layer_center.yaml type: LayerCenter name: layer_center layers: - layer_a- layer_b构建器会通过Get方法检查名字为layer_center的组件是否存在。如果不存在就调用Create方法创建type为LayerCenter、名字为layer_center的组件。LayerCenter在创建后会自动读取上述配置发现其layers下有两个组件layer_a和layer_b。然后会检查这两个组件是否存在。如果不存在则会在构建器中通过组件名寻找对应的配置文件——这就要求组件名和其配置名强一致。比如layer_a的配置名为layer_a.yamllayer_b的配置名为layer_b.yaml。 # layer_a.yaml name: layer_a type: Layer divider: ExampleADivider handlers: - ExampleA1Handler- ExampleA2Handler# layer_b.yaml name: layer_b type: Layer divider: ExampleBDivider handlers: - ExampleB1Handler- ExampleB2Handler这个创建过程通过下面函数实现 func (c *Constructor) createConcreteByObjectName(name string) error {confPath, err : c.concreteConfManager.GetConfPath(name)if err ! nil {return err}data, err : os.ReadFile(confPath)if err ! nil {return err}var constructorType ConstructorTypeif err : yaml.Unmarshal(data, constructorType); err ! nil {return err}switch constructorType.Type {case TypeNameHandler:_, err : c.handlerConstructorInterface.GetHandler(name)if err ! nil {return c.handlerConstructorInterface.CreateHandlerWithConfPath(confPath)}case TypeNameDivider:_, err : c.dividerConstructorInterface.GetDivider(name)if err ! nil {return c.dividerConstructorInterface.CreateDividerWithConfPath(confPath)}case TypeNameLayer:_, err : c.layerConstructorInterface.GetLayer(name)if err ! nil {return c.layerConstructorInterface.CreateLayerWithConfPath(confPath)}case TypeNameLayerCenter:_, err : c.layerCenterConstructorInterface.GetLayerCenter(name)if err ! nil {return c.layerCenterConstructorInterface.CreateLayerCenterWithConfPath(confPath)}case TypeNameHandlerGroup:_, err : c.handlerGroupConstructorInterface.GetHandlerGroup(name)if err ! nil {return c.handlerGroupConstructorInterface.CreateHandlerGroupWithConfPath(confPath)}case TypeNameAsyncHandlerGroup:_, err : c.asyncHandlerGroupConstructorInterface.GetAsyncHandlerGroup(name)if err ! nil {return c.asyncHandlerGroupConstructorInterface.CreateAsyncHandlerGroupWithConfPath(confPath)}default:err : c.createConcreteByTypeName(constructorType.Type, data)if err ! nil {return err}return nil}return fmt.Errorf(class name %s not found, name) }对于框架自有组件如LayerCenter、HandlerGroup等它们会由其构建器构建。对于其他自定义的组件比如自定义的各种Handler则通过default逻辑中createConcreteByTypeName实现。 func (c *Constructor) createConcreteByTypeName(name string, data []byte) error {if strings.HasSuffix(name, TypeNameAsyncHandlerGroup) {_, err : c.asyncHandlerGroupConstructorInterface.GetAsyncHandlerGroup(name)if err ! nil {asyncHandlerGroupInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : asyncHandlerGroupInterface.(frame.ConcreteInterface); ok {return c.asyncHandlerGroupConstructorInterface.RegisterAsyncHandlerGroup(namedInterface.Name(), asyncHandlerGroupInterface.(frame.AsyncHandlerGroupBaseInterface))} else {return c.asyncHandlerGroupConstructorInterface.RegisterAsyncHandlerGroup(name, asyncHandlerGroupInterface.(frame.AsyncHandlerGroupBaseInterface))}}return nil}if strings.HasPrefix(name, TypeNameHandlerGroup) {_, err : c.handlerGroupConstructorInterface.GetHandlerGroup(name)if err ! nil {handlerGroupInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : handlerGroupInterface.(frame.ConcreteInterface); ok {return c.handlerGroupConstructorInterface.RegisterHandlerGroup(namedInterface.Name(), handlerGroupInterface.(frame.HandlerGroupBaseInterface))} else {return c.handlerGroupConstructorInterface.RegisterHandlerGroup(name, handlerGroupInterface.(frame.HandlerGroupBaseInterface))}}return nil}if strings.HasSuffix(name, TypeNameDivider) {_, err : c.dividerConstructorInterface.GetDivider(name)if err ! nil {dividerInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : dividerInterface.(frame.ConcreteInterface); ok {return c.dividerConstructorInterface.RegisterDivider(namedInterface.Name(), dividerInterface.(frame.DividerBaseInterface))} else {return c.dividerConstructorInterface.RegisterDivider(name, dividerInterface.(frame.DividerBaseInterface))}}return nil}if strings.HasSuffix(name, TypeNameHandler) {_, err : c.handlerConstructorInterface.GetHandler(name)if err ! nil {handlerInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : handlerInterface.(frame.ConcreteInterface); ok {return c.handlerConstructorInterface.RegisterHandler(namedInterface.Name(), handlerInterface.(frame.HandlerBaseInterface))} else {return c.handlerConstructorInterface.RegisterHandler(name, handlerInterface.(frame.HandlerBaseInterface))}}return nil}if strings.HasSuffix(name, TypeNameLayer) {_, err : c.layerConstructorInterface.GetLayer(name)if err ! nil {layerInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : layerInterface.(frame.ConcreteInterface); ok {return c.layerConstructorInterface.RegisterLayer(namedInterface.Name(), layerInterface.(frame.LayerBaseInterface))} else {return c.layerConstructorInterface.RegisterLayer(name, layerInterface.(frame.LayerBaseInterface))}}return nil}if strings.HasSuffix(name, TypeNameLayerCenter) {_, err : c.layerCenterConstructorInterface.GetLayerCenter(name)if err ! nil {layerCenterInterface, err : c.Create(name, data, c)if err ! nil {return err}if namedInterface, ok : layerCenterInterface.(frame.ConcreteInterface); ok {return c.layerCenterConstructorInterface.RegisterLayerCenter(namedInterface.Name(), layerCenterInterface.(frame.LayerCenterBaseInterface))} else {return c.layerCenterConstructorInterface.RegisterLayerCenter(name, layerCenterInterface.(frame.LayerCenterBaseInterface))}}return nil}return fmt.Errorf(object name %s not found, name) }在底层我们需要设计一种规则用于标志这个自定义组件是哪个框架基础组件的子类。这儿就引出这个框架的第二个强制性约定——自定义类型的名称需要以框架基础组件名结尾。比如自定义的ExampleA1Handler是以Handler结尾这样在底层我们就知道将其构造成一个Handler对象。 所有的自动构建都依赖于配置文件。于是我们设计了ConcreteConfManager来遍历配置文件目录这个目录在我们创建构建器时传入的。 ……runPath, errGetWd : os.Getwd()if errGetWd ! nil {fmt.Printf(%v, errGetWd)return}concretePath : path.Join(runPath, conf)constructor : constructorbuilder.BuildConstructor(factory, concretePath)……然后我们告诉构建器初始组件名它就会自动像爬虫一样通过配置文件和之前注册的反射类型将对象和关系都构建出来。 ……mainProcess : layer_centerrun(constructor, mainProcess) }func run(constructor *constructor.Constructor, mainProcess string) {if err : constructor.CreateConcrete(mainProcess); err ! nil {fmt.Printf(%v, err)}单一对象 单一对象是指一个类型只有一个对象。 我们在写业务时往往需要一个简单的逻辑单元处理一个单一的事情即在任何场景下它只需要存在一份——属性一样且不会改变。 这个时候对象名变得不太重要。我们只要让其Name方法返回其类型名而不需要再搞一个配置文件就能实现自动构建。这种场景占绝大多数。 func (e *ExampleA1Handler) Name() string {return reflect.TypeOf(*e).Name() } 多个对象 有时候我们希望一个类可以处理一种逻辑但是其在不同场景下其属性不一样。这样我们就需要通过配置文件来描述它——描述它不同的名字和对应的属性。比如下面的例子就是从配置文件中读取了名字和其相应属性。 package samplehandlerimport (fmtghgroups/frameghgroupscontext ghgroups/frame/ghgroups_contextgopkg.in/yaml.v2 )// 自动构建handler它会自动从配置文件中读取配置然后根据配置构建handler // 因为系统使用名称作为唯一检索键所以自动构建handler在构建过程中就要被命名而名称应该来源于配置文件 // 这就要求配置文件中必须有一个名为name的字段用于指定handler的名称 // 下面例子中confs配置不是必须的handler的实现者需要自行解析配置文件以确保Name方法返回的名称与配置文件中的name字段一致type SampleAutoConstructHandlerConf struct {Name string yaml:nameConfs []SampleAutoConstructHandlerEnvConf yaml:confs }type SampleAutoConstructHandlerEnvConf struct {Env string yaml:envRegionsConf []SampleAutoConstructHandlerRegionConf yaml:regions_conf }type SampleAutoConstructHandlerRegionConf struct {Region string yaml:regionAwsRegion string yaml:aws_regionAccessKeyId string yaml:access_key_idSecretAccessKey string yaml:secret_access_keyIntKey int32 yaml:int_key }type SampleAutoConstructHandler struct {frame.HandlerBaseInterfaceframe.LoadConfigFromMemoryInterfaceconf SampleAutoConstructHandlerConf }func NewSampleAutoConstructHandler() *SampleAutoConstructHandler {return SampleAutoConstructHandler{} }// /// // LoadConfigFromMemoryInterface func (s *SampleAutoConstructHandler) LoadConfigFromMemory(configure []byte) error {sampleHandlerConf : new(SampleAutoConstructHandlerConf)err : yaml.Unmarshal([]byte(configure), sampleHandlerConf)if err ! nil {return err}s.conf *sampleHandlerConfreturn nil }// /// // ConcreteInterface func (s *SampleAutoConstructHandler) Name() string {return s.conf.Name }// /// // HandlerBaseInterface func (s *SampleAutoConstructHandler) Handle(*ghgroupscontext.GhGroupsContext) bool {fmt.Sprintln(s.conf.Name)return true }// /// 于是我们在不同组件关系中通过该类型的不同对象名来组织关系从而实现一套逻辑不同配置的应用场景。 比如下面的两个配置描述了同一个类型的不同配置 # sample_handler_a.yaml type: SampleAutoConstructHandler name: sample_handler_a confs: - env: Onlineregions_conf:- region: us-east-1aws_region: us-east-1int_key: 1- region: us-east-2aws_region: us-east-2# sample_handler_b.yaml type: SampleAutoConstructHandler name: sample_handler_b confs: - env: Onlineregions_conf:- region: us-east-1aws_region: us-east-1int_key: 2- region: us-east-2aws_region: us-east-2然后在下面关系中予以区分调用 name: Sample divider: divider_sample_a handlers: - handler_sample_a- handler_sample_b手工构建 如果由于某些原因自动构建不能满足需求我们可以手工构建对象。这个时候我们不需要向对象工厂注册其反射Register只要手工构建出对象后调用工厂的注册对象方法比如RegisterHandler告诉框架某个名字的组件存在。这样在构建关系时就会自动识别——这就要求手工构建要在自动构建之前完成否则框架无法识别它们。 package samplehandlerimport (fmtghgroups/frameghgroupscontext ghgroups/frame/ghgroups_context )// 这是相对简单的handler它只用实现HandlerInterface两个接口 // 系统使用名称作为唯一检索键通过构造不同的对象拥有不同的名字可以在系统中有多个该名字的handler实例即一个类型struct)可以有多个该名字的handler实例type SampleSelfConstructHandlerMulti struct {frame.HandlerBaseInterfacename string }func NewSampleSelfConstructHandlerMulti(name string) *SampleSelfConstructHandlerMulti {return SampleSelfConstructHandlerMulti{name: name,} }// /// // ConcreteInterface func (s *SampleSelfConstructHandlerMulti) Name() string {return s.name }// /// // HandlerBaseInterface func (s *SampleSelfConstructHandlerMulti) Handle(*ghgroupscontext.GhGroupsContext) bool {fmt.Sprintln(s.Name())return true }// /// 注册代码如下 ……constructor : utils.BuildConstructor()sampleSelfConstructHandlerMultiNameA : sample_self_construct_handler_multi_asampleSelfConstructHandlerMultiA : NewSampleSelfConstructHandlerMulti(sampleSelfConstructHandlerMultiNameA)constructor.RegisterHandler(sampleSelfConstructHandlerMultiA.Name(), sampleSelfConstructHandlerMultiA)sampleSelfConstructHandlerMultiNameB : sample_self_construct_handler_multi_bsampleSelfConstructHandlerMultiB : NewSampleSelfConstructHandlerMulti(sampleSelfConstructHandlerMultiNameB)constructor.RegisterHandler(sampleSelfConstructHandlerMultiB.Name(), sampleSelfConstructHandlerMultiB)……关系的构建 自动构建 关系的自动构建依赖于配置文件的描述。 每个组件在读取配置文件后会构建不存在的子组件并加载其配置。 在这个递归过程中整个关系网就会被构建起来。 比如LayerCenter的构建过程 func (l *LayerCenter) LoadConfigFromMemory(configure []byte) error {var layerCenterConf LayerCenterConferr : yaml.Unmarshal(configure, layerCenterConf)if err ! nil {return err}l.conf layerCenterConfreturn l.init() }func (l *LayerCenter) init() error {for _, layerName : range l.conf.Layers {if err : l.constructorInterface.CreateConcrete(layerName); err ! nil {return err}if someInterface, err : l.constructorInterface.GetConcrete(layerName); err ! nil {return err} else {if layerBaseInterface, ok : someInterface.(frame.LayerBaseInterface); !ok {return fmt.Errorf(layer %s is not frame.LayerBaseInterface, layerName)} else {l.layers append(l.layers, layerBaseInterface)}}}return nil }其在底层会创建Layer对象进而触发Layer子组件的构建 func (l *Layer) LoadConfigFromMemory(configure []byte) error {var layerConf LayerConferr : yaml.Unmarshal(configure, layerConf)if err ! nil {return err}l.conf layerConfreturn l.init() }func (l *Layer) init() error {if l.handlers nil {l.handlers make(map[string]frame.HandlerBaseInterface)}err : l.initDivider(l.conf.Divider)if err ! nil {return err}err l.initHandlers(l.conf.Handlers)if err ! nil {return err}return nil }func (l *Layer) initDivider(dividerName string) error {if err : l.constructorInterface.CreateConcrete(dividerName); err ! nil {return err}if someInterface, err : l.constructorInterface.GetConcrete(dividerName); err ! nil {return err} else {if dividerInterface, ok : someInterface.(frame.DividerBaseInterface); !ok {return fmt.Errorf(handler %s is not frame.DividerBaseInterface, dividerName)} else {err l.SetDivider(dividerName, dividerInterface)if err ! nil {return err}}}return nil }func (l *Layer) initHandlers(handlersName []string) error {for _, handlerName : range handlersName {if err : l.constructorInterface.CreateConcrete(handlerName); err ! nil {return err}if someInterface, err : l.constructorInterface.GetConcrete(handlerName); err ! nil {return err} else {if handlerInterface, ok : someInterface.(frame.HandlerBaseInterface); !ok {return fmt.Errorf(handler %s is not frame.HandlerBaseInterface, handlerName)} else {err l.AddHandler(handlerName, handlerInterface)if err ! nil {return err}}}}return nil }手工构建 手工构建是不推荐的形式因为它可能会让维护成本上升但是框架仍然支持这种形式。 这儿只是做个简单介绍如下例子 constructor : utils.BuildConstructor()layerCenter : NewLayerCenter(constructor)testLayer : layer.NewLayer(test_layer, constructor)layerCenter.Add(testLayer)layerCenter通过Add新增了一个Layer。 更多具体例子可以参考源码中的单测文件。
http://www.zqtcl.cn/news/536153/

相关文章:

  • 如何做链接淘宝客的网站只做画册的网站
  • docker可以做网站吗专业的营销型网站
  • 重庆市建设工程安全网站上海制造网站公司
  • 咨询网站公司建设计划书安卓软件开发软件
  • 手机网站建设文章直播平台开发多少钱
  • 站长综合查询工具常用的网站开发语言有哪些
  • 免费网站看v片在线第一次做乌市seo网络营销流程
  • 社交网站模板下载柬埔寨网赌网站开发
  • 网站开发合同是否要交印花税杭州集团网站建设
  • 企业网站建设排名资讯一个公司做两个网站可以吗
  • 简单门户网站开发灰色行业seo大神
  • 网站开发学那种语言外贸推广网站建设
  • 公司网站建设及推广中国优秀企业网站欣赏
  • 个人代做网站建设京东类的网站需要什么流程
  • 建设一个地方门户网站厦门网站开发排名
  • 网站建设公司广告标题语网站设计主题有哪些
  • 网站推广方式主要通过做网站所需的知识技能
  • 我想在阿里巴巴网站开店_怎么做app建设网站公司
  • 西安做百度网站的制作网站公司选 择乐云seo
  • 网站优化建设河南手机模拟器
  • 网站建设运维标准深圳企业vi设计公司
  • 做网站怎么挣钱中小型企业网站建设
  • 深圳如何搭建建网站学校网站的建设与应用
  • 免费推广网站入口2023燕wordpress看图插件
  • 网站做不做301四川省住建设厅网站
  • 优化方案官网电子版一个网站做两个优化可以做吗
  • 企业网站排名提升软件智能优化上海网站制作的费用
  • 建分类信息网站西安高端模板建站
  • 南昌做网站哪家好成都三合一网站建设
  • 中国市政建设局网站做外单网站