第四十一章:WSaiOS EOM软件工程架构设计——基于PHP OOP与MVC实现
重点研究:
- EOM模型如何映射PHP类结构;
- Element、Object、Relationship如何设计Class;
- Model层如何管理认知数据;
- Controller如何调用认知服务;
- Smarty如何显示认知结果;
- MySQL如何保存对象关系。
41.1 从EOM模型到软件对象模型
EOM:
Element
↓
Object
↓
Relationship
↓
Knowledge
↓
Memory
转换为PHP:
Element Class
Object Class
Relationship Class
Knowledge Class
Memory Class
例如:
现实对象:
电动牙刷供应商
EOM:
Object
{
名称
属性
状态
关系
方法
}
PHP:
class CognitiveObject
{
public $name;
public $attributes;
public $state;
public $relations;
public function analyze()
{
}
}
41.2 为什么OOP适合认知对象模型
传统程序:
数据和行为分离。
例如:
数据库:
supplier表
product表
city表
程序:
另外处理。
OOP:
把:
数据 + 行为
组合。
例如:
供应商对象:
class Supplier
{
public $company;
public $product;
public $capacity;
public function supply()
{
}
}
对应:
EOM:
对象
属性
方法
41.3 MVC对应认知系统结构
MVC:
不是简单网页结构。
在WSaiOS中:
对应:
用户输入
|
↓
Controller
认知处理流程
↓
Model
知识库存取
↓
View
认知结果表达
结构:
Controller
|
|
Cognitive Service
|
|
Model
|
|
MySQL Knowledge Base
Smarty View
41.4 Model层设计
Model负责:
对象数据。
例如:
ObjectModel:
class ObjectModel
{
private $db;
public function find($id)
{
$sql="
SELECT *
FROM objects
WHERE id=$id
";
return $this->db->query($sql);
}
}
数据库:
objects
id
name
type
attributes
state
41.5 Service层设计(核心)
你的系统最重要的是:
Service。
因为:
认知逻辑不能放Controller。
结构:
Controller
↓
ElementService
↓
ObjectService
↓
MatchingService
↓
MemoryService
例如:
MatchingService:
class MatchingService
{
public function match($object)
{
//对象匹配
//关系匹配
//经验匹配
return $result;
}
}
41.6 Smarty表现层
Smarty负责:
认知结果展示。
例如:
Controller:
$smarty->assign(
"result",
$result
);
$smarty->display(
"result.tpl"
);
模板:
<h1>
认知分析结果
</h1>
产品:
{$result.product}
城市:
{$result.city}
匹配度:
{$result.score}
41.7 完整WSaiOS工程流程
实际运行:
用户输入
↓
Controller
↓
Element解析
↓
Object生成
↓
Relationship分析
↓
Knowledge查询
↓
Memory调用
↓
Matching计算
↓
Smarty显示
这才是你的核心价值。
后面的章节建议全部进入:
第四十二章:PHP OOP认知对象框架设计
研究:
- Class设计;
- Interface设计;
- Abstract Class;
- Object继承;
- 多态实现。