In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1 internal implementation of the module
1.1 implementation starting point
In order to better understand the internal implementation architecture of the configuration management module, we first take the simplest implementation structure as the starting point, adopt the way of refactoring, and gradually apply the relevant design patterns, from simple to complex, so that we can better see how to choose the design pattern to be used, how to actually apply the design pattern, and how to make a variety of design patterns work together.
1.1.1 Let's first take a look at the starting point for the implementation of configuration management. First, according to the definition of the data structure provided to the outside, we create the corresponding data model.
(1) first, let's take a look at how to describe NeedGen in GenConf.xml. The corresponding xml snippet is as follows:
UserGenConf.xml
The following data model can be designed according to the definition, and the code is as follows:
/
Describe the registration information that needs to be generated
/
Publicclass NeedGenModel {
/
Identification of the module that needs to be generated
/
Private String id
/
The program identity used to read and parse the configuration file that needs to be generated
/
Private String provider
/
Theme that needs to be used by the generation module
/
Private String theme
/ *
Some parameters that need to be generated for the module, usually for provider use
, /
Private Map mapParams = new HashMap ()
/ / getter/setter is omitted
}
(2) then take a look at ThemeModel, which will involve GenTypeModel. First, take a look at the xml fragment corresponding to GenType. The example is as follows:
Business.ebi
Business/Ebi.txt
Ebi.java
Then, the corresponding model is designed according to the definition. The example is as follows:
/
The types of functions that can be generated by the topic
/
Publicclass GenTypeModel {
/
Identification of the type of function that can be generated
/
Private String id
/
Classes that are specifically used to generate functions
/
Private String genTypeClass
/
Generate the parameters required for the function, key: parameter identification, value: parameter value
/
Private Map mapParams = new HashMap ()
/ / getter/setter is omitted
}
(3) next, take a look at the data model corresponding to theme, and design the corresponding model according to the definition. The example is as follows:
/
Data model of the topic
/
Publicclass ThemeModel {
/
Identification of the subject
/
Private String id = ""
/
The location of the theme, passed from GenConf
/
Private String location = ""
/
The type provided by the topic that can generate the function, key: the identification of the type, value: the model that describes the generated type
/
Private Map mapGenTypes = new HashMap ()
/
The output type provided by the topic, key: identification of the output type, value: the class that implements the output type
/
Private Map mapGenOutTypes = new HashMap ()
/
The identity of the provider,key:provider that reads the configuration file provided by the topic, value: the class that implements provider
/
Private Map mapProviders = new HashMap ()
/ / getter/setter is omitted
}
(4) next, let's take a look at GenConfModel, which combines the first two. Examples are as follows:
/
Configure the corresponding model for the x-gen core framework
/
Publicclass GenConfModel {
/
Describe the registration of multiple modules that need to be generated model
/
Private List needGens = new ArrayList ()
/
Model that describes multiple topics registered
/
Private List themes = new ArrayList ()
/
General definition of constants, key: identification of constants, value: values of constants
/
Private Map mapConstants = new HashMap ()
/ * * obtain the configuration data of the corresponding theme according to the id of theme * @ paramthemeId * @ return * / public ThemeModel getThemeById (String themeId) {for (ThemeModel tm: themes) {if (tm.getId (). Equals (themeId)) {return tm;}} returnnew ThemeModel ();}
/ / getter/setter is omitted
}
(5) then look at ModuleConfModel, which will involve ExtendConfModel. First, let's take a look at the xml fragment corresponding to ExtendConfModel. The example is as follows:
User
Then, the corresponding model is designed according to the definition. The example is as follows:
/
Model of extended data in module configuration
/
Publicclass ExtendConfModel {
/
Identification of extended data
/
Private String id= ""
/
Describe whether the data is a single value or multiple values
/
Privatebooleansingle = true
/
Describe a single value
/
Private String value= ""
/ *
Describe multiple values
, /
Private String values [] = null
/ / getter/setter is omitted
}
(6) then let's take a look at ModuleConfModel. An example is as follows:
/
Describe the data model configured by the module
/
Publicclass ModuleConfModel {
/
Module identification
/
Private String moduleName = ""
/
Identification of the theme used by the module generation
/
Private String useTheme = ""
/
The module generates the required extended data
/
Private Map mapExtends = new HashMap ()
/ *
The function that the module needs to generate, key: the identity of the function that needs to be generated, value: a collection of identities of multiple output types
, /
Private Map mapNeedGenTypes = new HashMap ()
/ / getter/setter is omitted
}
1.2 for the previously defined API, provide a most basic implementation, only need to meet the most basic functions, need to achieve the function of reading configuration files, and then have the function of caching configuration data, and finally to achieve the required functions in API.
According to these requirements, you can write the following schematic code:
/
Schematic implementation: preliminary implementation of configuration management
/
Publicclass GenConfEbo implements GenConfEbi {
/
Model used to store the core framework configuration
/
Private GenConfModel genConf = new GenConfModel ()
/ *
Used to store the configuration model for each module that needs to be generated
Key: identification of each module that needs to be generated
Value: the configuration model for each module that needs to be generated
, /
Private Map mapModuleConf = new HashMap ()
Public GenConfEbo () {/ / read configuration data here} privatevoid readConf () {/ / 1: get configuration data, such as read configuration file / / for simplicity, omit / / 2: set the obtained configuration data to the property and cache it} / * is the basic implementation of the external interface. If you observe carefully, you will find that as long as you read the configuration data. These implementations are basically in memory, and very simple data acquisition exists * / public GenConfModel getGenConf () {returnthis.genConf } public Map getMapModuleConf () {returnthis.mapModuleConf;} public ExtendConfModel getModuleExtend (ModuleConfModel moduleConf, String extendId) {return moduleConf.getMapExtends (). Get (extendId);} public String getThemeGenOutType (ModuleConfModel moduleConf, String genOutTypeId) {returnthis.genConf.getThemeById (moduleConf.getUseTheme ()). GetMapGenOutTypes (). Get (genOutTypeId);} public GenTypeModel getThemeGenType (ModuleConfModel moduleConf, String genTypeId) {returnthis.genConf.getThemeById (moduleConf.getUseTheme ()). Get (genTypeId);}
}
2 join the simple factory
2.1 problems faced
Observing the above implementation, the interface is provided to the outside of the module, but the outside does not know the specific implementation of the module at all, so how can the outside of the module get an implementation object that implements the interface?
2.2 solve it with a simple factory
Simple factory is a reasonable solution to the above problems. So let's review some of the basics of a simple factory, and then look at how to apply it to solve the above problems.
2.2.1 Review of simple factory foundation
1: schema definition
Provides the ability to create an instance of an object, regardless of its implementation. The type of instance being created can be an interface, an abstract class, or a concrete class.
2: the essence of model
The essence of a simple factory is to choose implementation.
3: pattern structure
Diagram of the structure of a simple factory
4: basic knowledge
(1) the simple factory is located in the module that provides the interface.
(2) the main function of simple work is to create object instances, which can be interfaces, abstract classes or ordinary classes.
(3) A simple factory can be realized as a singleton or a static factory.
(4) the internal implementation of the simple factory is mainly to "choose the appropriate implementation". The implementation has already been done, and the simple factory can only choose to use it.
(5) when selecting a simple factory, the required parameters can be passed in from the client, the configuration file, or the result of a run-time program, etc.
(6) if you use reflection + configuration file, you can write a general simple factory.
5: common application scenarios:
A simple factory is usually used to provide interface objects to the outside of the module, which can effectively hide the internal implementation of the module.
2.2.2 ideas for using simple factories to solve problems
The idea of a simple factory to solve this problem is to add a class to the configuration management module, implement a method in this class, let the method create an interface object and return it, and then provide the class to the client. Let the client get the interface object by calling the method of this class.
2.2.3 Core Code example
Make a simple factory. The example is as follows:
Publicclass GenConfFactory {
Private GenConfFactory () {
} publicstatic GenConfEbi createGenConfEbi () {returnnew GenConfEbo ();}
}
Summary
With external interfaces and data objects, it is naturally necessary to provide a factory to allow the outside to obtain the corresponding interface objects through the factory, so as to effectively hide the internal implementation.
At this point, the structure outside the module is defined, and then focus on the internal implementation of the module.
This article is launched by the official account of Wechat-- Architecture Design.
You can click "read the original" to learn more!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.