Android scalability to talk about late development from the configuration file

1. Why use a configuration file

Often used in the project configuration file, android also contains a lot of configuration files, such local.properties, gradle.properties projects such as built-in configuration files, but also to develop their own maintenance of configuration files, such properties.properties.

Profile format is generally there to this profile :( json, xml, ini format, not in the scope of this article)

baseurl=www.baidu.com
host=127.0.0.1
port=8080

Generally key-value pairs present embodiment, like the ios NSDictionary as storage present in the form of key-value, typically read by the key data acquired value.

Profile is everywhere in the software system, using a configuration file in the end what good is it

2. benefit profile

Project development, regardless of demand or, architecture worth mentioning, change is in the affirmative, not a single project from scratch to do the tail, is unchanged.

For example:

  • Replace the server, the address has changed, the port has changed
  • Database account password changed
  • Maps Engine start with arcgis, and later with the osmdroid (configuration file is just a small step)
  • …………

Too many variables we can do is to minimize the workload of late, but later in the project scalability, portability higher, of course, pre-development work will increase. In a word, maintaining the status quo.

Here is a simple example:

web project on the line to the server address has changed, a corresponding manner:

① write the address in the configuration file: to modify the project configuration file server address, similar to the example above file. See at a glance


② address to write in class: go find a place to write server code, and then modify the code, and then recompile the release

Efficiency and workload of two ways Obviously, the profile does make maintenance easier. Of course, this is not the case in reality android, with respect to only a corresponding change in the interface. But in the actual development, using the configuration file is also great.

By simple example we can conclude that, using the features and benefits of the configuration file:

  • Improve maintainability
  • Reducing maintenance workload
  • Improve efficiency
  • Scalability (below speak)
  • Save the settings (such as window .ini file)
  • It can restart after modification
  • ……

Configuration files in the android

Personal summarized in two ways:

Android 3.1 resource files

android strings.xml to improve the unified management of a string andoird appear, we can borrow. Similar to the following

<!-- 地图引擎全类名 -->
<string name="map_engine">kmap.KMapWrapper</string>

Get of course very simple

String clazz=context.getResources().getString(R.string.map_engine);
//后续...

You can modify the time when we need to modify directly in strings.xml, of course, also can create a resource .xml file, as your configuration file, get the same manner.

3.2 new way of file

Our own resource files are generally placed in assets folder (Sound Video half in the raw), but in the next directory android studio in assets default folder is not automatically generated, and the standard android directory structure is generally:

-src/
  -androidTest/
  -main/
    -AndroidManifest.xml
    -res/
    -assets/
    -aidl/
    -rs/
    -jni/
    ... 

In the main direct new assets folder, the new file properties.properties, enter the following:

url=www.baidu.com

Note: key - value is in the form of characters, separated with =

Reads the configuration file of utility classes

public class ReadPropertyUtil {

public static String getProperty(Context ctx, String keyName) throws IOException {
    Properties pro = new Properties();

    InputStream in = ctx.getResources().getAssets().open("property.property");
    pro.load(in);
    return pro.getProperty(keyName, "没有获取到资源文件的内容");

}

}

The actual call

String url = null;
try {
    url = ReadPropertyUtil.getProperty(this, "url");
} catch (IOException e) {
    e.printStackTrace();
}

Toast.makeText(this, url, Toast.LENGTH_SHORT).show();

This, basically using the configuration file has been introduced almost

4. On the reflection profile + scalability

(There will be a separate article describes the map engine development, so the following is just a brief introduction)

Examples of actual projects, the project company would have used google maps, but because of fees, and other custom aspects of the problem to be replaced, said a higher degree of freedom Osmdriod map, because the project had not done the extraction, so change up super trouble, almost every place will need to re-use map changes, the workload is not an ordinary large.

There is always a solution, once involved, comfortable life, began the extraction of the road.

Define your own map interfaces IMap, interface methods based on actual projects, such as some basic operations such as drawing points, lines, there are some special custom map operations.

Then a third party api can not appear in the actual call, you can only call methods in IMap write their own, meaning that the call-dependent interface. So the whole process will not see anything about the emergence of a third party api.

Then start writing implement, for example, just started using google map engine, we define a GoogleWrapper achieve IMap interface, corresponding method, holds a google of Mapview objects in the constructor, similar to the way proxy mode, the interface method .

Implementation class written, how to instantiate it, by reflection (Why through reflection, direct new can not I?)

//clazz为配置文件中获取的地图引擎的全类名
Class cls = Class.forName(clazz);
Class[] paramTypes = { Context.class };// 参数
Object[] params = { ctx }; // 方法传入的参数
Constructor con = cls.getConstructor(paramTypes); // 根据参数调用构造器
IMap map = (IMap) con.newInstance(params); // 实例化对象

By way of example of the above to get a IMap objects, in fact, the full name of the class configuration file map engine. At this point, basically over, you may also see this in the end what are the benefits? So the question is, now is the domestic google maps wall, and suddenly can not use it, you say you can not tell the user to open a proxy, only a few dollars a month now, only for engine map, the next map change engine What steps?

1. 第一步:定义OsmWrapper继承IMap并实现相应的方法,用Osmdroid的Api
2. 第二部,修改配置文件中的全类名
3. 看电影
4. 听音乐
5. 唱歌
6. 下班

Benefits at a glance, right now, you can make time to come up with more because of who, playing games, sleeping ...... of course, if you want to improve your own ability, then use the saved time to learn it.

Cease to learn non-stop!

Guess you like

Origin blog.csdn.net/qq_30124547/article/details/53187156
Recommended