场景:
机器人设备上展示的首页数据、其它模块数据均通过后台配置,机器人端获取数据保存,下次判断相关数据有无从本地获取,机器人设备端只负责展示并执行点击事件
弊端:
机器人环境下特别刚开机对网络存在需求,弱网情况下经常性数据无法获取,机器无法正常运行【各种展会弱网情况明显】
解决方案:
所有数据采用本地配置化。
具体方案:
本地数据以Json数据存储在.json文件中,读取后转换成JavaBean的集合即可。【此种方案最简便,且避免了String溢出现象】
具体导入实现只需要一行代码即可:
InitTableFactory.loadPropertyPath("homepage.json");
{
"Md5": "500eafc04370a29b4626433430c1e423",
"apkName": "null",
"apkUrl": "null",
"apkVersion": "null",
"bak1": "",
"bak2": "",
"catagoryId": "aab6438c84494dc1a2a116236df87812",
"catagoryName": "首页",
"channel": "儿童早教",
"channelMark": "儿童早教",
"id": 19,
"imageUrl": "file:///android_asset/recomend_configpic/home_children_education.png",
"type": "2",
"mark": "",
"third": "house_children_education",
"pageUrl": "",
"no": 2,
"isTitle": false
},
{
"Md5": "6f1e7e159208d19df1371546debba696",
"apkName": "null",
"apkUrl": "null",
"apkVersion": "null",
"bak1": "",
"bak2": "",
"catagoryId": "aab6438c84494dc1a2a116236df87812",
"catagoryName": "首页",
"channel": "音乐",
"channelMark": "音乐",
"id": 21,
"imageUrl": "file:///android_asset/recomend_configpic/home_musice.png",
"type": "2",
"mark": "",
"third": "house_music",
"pageUrl": "",
"no": 4,
"isTitle": false
},
{
"Md5": "e3a82245856bfecfae8848a53aa37881",
"apkName": "null",
"apkUrl": "null",
"apkVersion": "null",
"bak1": "",
"bak2": "",
"catagoryId": "aab6438c84494dc1a2a116236df87812",
"catagoryName": "首页",
"channel": "聊天互动",
"channelMark": "聊天互动",
"id": 23,
"imageUrl": "file:///android_asset/recomend_configpic/home_chat.png",
"type": "2",
"mark": "",
"third": "house_chat_interaction",
"pageUrl": "",
"no": 6,
"isTitle": false
},
{
"Md5": "29b681c27b30f56a3f48bd8b8fbe0b0f",
"apkName": "com.fgecctv.beautycamera",
"apkUrl": "file:///android_asset/recomend_configpic/0a937326a4d64c97b8b14c08b4f120a5.apk",
"apkVersion": "1",
"bak1": "meiyan_apk.apk",
"bak2": "",
"catagoryId": "aab6438c84494dc1a2a116236df87812",
"catagoryName": "首页",
"channel": "相机",
"channelMark": "相机",
"id": 24,
"imageUrl": "file:///android_asset/recomend_configpic/home_camera.png",
"type": "0",
"mark": "美颜apk",
"third": "xmly",
"pageUrl": "",
"no": 7,
"isTitle": false
},
{
"Md5": "772c573b5b2e4bea21562ab6072b50fb",
"apkName": "null",
"apkUrl": "null",
"apkVersion": "null",
"bak1": "",
"bak2": "",
"catagoryId": "aab6438c84494dc1a2a116236df87812",
"catagoryName": "首页",
"channel": "萌相册",
"channelMark": "萌相册",
"id": 25,
"imageUrl": "file:///android_asset/recomend_configpic/home_moe_photoalbum.png",
"type": "2",
"mark": "",
"third": "house_photo_album",
"pageUrl": "",
"no": 8,
"isTitle": false
}]
具体实现方式:策略模式+工厂模式
工厂类: InitTableFactory
策略接口:TableStrategy
工厂实现:RecommendTable
实现方式可扩展、低耦合。
工厂模式、策略模式复习:
简单工厂模式
简单工厂模式就是通过调用静态方法来返回(得到)对象的一种思路,这样在需要创建多个对象时,避免了需要先多次创建工厂对象,缺点是某种程度上违反了开放——封闭原则,因为每当需要增加新的对象类型时,就要修改已有的静态工厂类。简单工厂模式(包括别的工厂模式)更侧重于 “得到对象”,一般设计的时候这个对象应该是现实世界中某种事物的映射,有它自己的属性与方法,。策略模式
策略模式更偏重于算法实现,按《大话设计模式》中的说法,策略模式更多地使用接口(Interface)而不是父类来实现,(当然这并不是绝对的,对于静态工厂模式中得到的对象的抽象也可以用接口,策略模式也可以用基类。),而接口更侧重于对于行为的抽象而不是对于对象的抽象。策略模式往往侧重于实现方便地替换不同的算法类,而这些类是通过方法来实现某些业务功能的,自身很可能没有属于自己的属性
具体实现如下:
public interface TableStrategy {
void initTable();
void load(String path);
}
public class InitTableFactory {
public static String TAG = "InitTableFactory";
public static void loadPropertyPath(String path) {
String fileName = FileUtils.getFileName(path);
TableStrategy strategy = null;
switch (fileName) {
case "homepage.json":
strategy = new RecommendTable();
break;
}
strategy.load(path);
}
}
public class RecommendTable implements TableStrategy {
@Override
public void load(String path) {
List<RecommendConfigInfo> list = SQLite.select().from(RecommendConfigInfo.class).queryList();
if(list.size()>0)
return ;
InputStream dataInputStream = getClass().getResourceAsStream("/assets/"+path);
try {
final List<RecommendConfigInfo> recommendConfigInfos= readJsonStream(dataInputStream);
synchronized (MagicApp.getContext()) {
FlowManager.getDatabase(RobotDatabase.class).executeTransaction(new ITransaction() {
@Override
public void execute(DatabaseWrapper databaseWrapper) {
if (recommendConfigInfos.size() != 0) {
for (RecommendConfigInfo recommendConfigInfo : recommendConfigInfos) {
recommendConfigInfo.insert();
}
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void initTable() {
}
public List<RecommendConfigInfo> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
List<RecommendConfigInfo> list = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
Gson gson = new Gson();
RecommendConfigInfo message = gson.fromJson(reader, RecommendConfigInfo.class);
list.add(message);
}
reader.endArray();
reader.close();
return list;
}
}
具体实现:RecommendTable 类中 load()方法和readJsonStream