|
@@ -1,15 +1,87 @@
|
|
|
-# 视频封装思路
|
|
|
+# 05.视频封装思路
|
|
|
#### 目录介绍
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+- 01.视频播放器内核封装需求
|
|
|
+- 02.播放器内核架构图
|
|
|
+- 06.介绍一下简单工厂模式
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 01.视频播放器内核封装需求
|
|
|
+
|
|
|
+
|
|
|
+### 02.播放器内核架构图
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### 06.介绍一下简单工厂模式
|
|
|
+- 首先,我们来看,什么是简单工厂模式。
|
|
|
+ - 通过一个例子来解释一下。在下面这段代码中,我们根据配置文件的后缀(json、xml、yaml、properties),选择不同的解析器(JsonRuleConfigParser、XmlRuleConfigParser……),将存储在文件中的配置解析成内存对象 RuleConfig。
|
|
|
+ ```java
|
|
|
+ public class RuleConfigSource {
|
|
|
+ public RuleConfig load(String ruleConfigFilePath) {
|
|
|
+ String ruleConfigFileExtension = getFileExtension(ruleConfigFilePath);
|
|
|
+ IRuleConfigParser parser = null;
|
|
|
+ if ("json".equalsIgnoreCase(ruleConfigFileExtension)) {
|
|
|
+ parser = new JsonRuleConfigParser();
|
|
|
+ } else if ("xml".equalsIgnoreCase(ruleConfigFileExtension)) {
|
|
|
+ parser = new XmlRuleConfigParser();
|
|
|
+ } else if ("yaml".equalsIgnoreCase(ruleConfigFileExtension)) {
|
|
|
+ parser = new YamlRuleConfigParser();
|
|
|
+ } else if ("properties".equalsIgnoreCase(ruleConfigFileExtension)) {
|
|
|
+ parser = new PropertiesRuleConfigParser();
|
|
|
+ } else {
|
|
|
+ throw new InvalidRuleConfigException(
|
|
|
+ "Rule config file format is not supported: " + ruleConfigFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ String configText = "";
|
|
|
+ //从ruleConfigFilePath文件中读取配置文本到configText中
|
|
|
+ RuleConfig ruleConfig = parser.parse(configText);
|
|
|
+ return ruleConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFileExtension(String filePath) {
|
|
|
+ //...解析文件名获取扩展名,比如rule.json,返回json
|
|
|
+ return "json";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ```
|
|
|
+- 为了让代码逻辑更加清晰,可读性更好,要善于将功能独立的代码块封装成函数。按照这个设计思路,我们可以将代码中涉及 parser 创建的部分逻辑剥离出来,抽象成 createParser() 函数。重构之后的代码如下所示:
|
|
|
+ - 如果我们非得要将 if 分支逻辑去掉,那该怎么办呢?比较经典处理方法就是利用多态。按照多态的实现思路,对上面的代码进行重构。重构之后的代码如下所示:
|
|
|
+ ```java
|
|
|
+ public interface IRuleConfigParserFactory {
|
|
|
+ IRuleConfigParser createParser();
|
|
|
+ }
|
|
|
+
|
|
|
+ public class JsonRuleConfigParserFactory implements IRuleConfigParserFactory {
|
|
|
+ @Override
|
|
|
+ public IRuleConfigParser createParser() {
|
|
|
+ return new JsonRuleConfigParser();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class XmlRuleConfigParserFactory implements IRuleConfigParserFactory {
|
|
|
+ @Override
|
|
|
+ public IRuleConfigParser createParser() {
|
|
|
+ return new XmlRuleConfigParser();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class YamlRuleConfigParserFactory implements IRuleConfigParserFactory {
|
|
|
+ @Override
|
|
|
+ public IRuleConfigParser createParser() {
|
|
|
+ return new YamlRuleConfigParser();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class PropertiesRuleConfigParserFactory implements IRuleConfigParserFactory {
|
|
|
+ @Override
|
|
|
+ public IRuleConfigParser createParser() {
|
|
|
+ return new PropertiesRuleConfigParser();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ```
|
|
|
|
|
|
|
|
|
|