[Lua]表驱动索引编程,form.lua
form.interface
local form = {_tag = 'form'}
function form.build(tag, super)
--[[
-- form to produce the target with super functional table and integrating multi-interface implement feature
-- produce a target and hold following feature
target:spec(interface) -- integrating the interface feature
target:spec(name, property) -- property holder
target:on(other) -- binding feature to other target and produce a delegater
--]]end
function form.on(target, spec)
--[[
-- produce a delegater binding target feature with itself from spec, that the feature defined in spec
--]]end
function form.spec(target)
--[[
--attach a form feature to the target
--if target builded from `form.build`
function target:spec(interface)
-- specify target reuse interface code
end
--following feature is flexible
function target:spec(prop, target)
-- specify target property initial value and produce getter/setter
-- example
-- target:spec('prop', property)
-- target:prop() return the property
-- target:prop(property) to set the property
-- target:spec('feature', interface)
-- target:spec('feature') return the interface delegater binding on the target
end
-- if target is a interface implementation of a certain specification
local spec = target
function spec:on(target)
-- produce the interface delegater binding on the target from spec
end
--]]end
查看实现代码:form.lua
form.test
local form = require('form')
-- 接口定义
local spec = {_tag = 'sepc'}
function spec:Show(msg)
print(self._tag..':Show(msg) not implemented')
end
function spec:Dependence(target)
print(self._tag..':Dependence(target) not implemented')
end
function spec:noImplementation()
print(self._tag..':noImplementation() not implemented')
end
-- 接口实现
local port = form.build('spec', spec)
print('\nport ###test start---------------------------')
port:Show()
port:Dependence()
port:noImplementation()
function port:Show(msg)
print('port:Show(msg)...')
print(msg)
end
print('\noverwrite port:Show(msg)--------------------')
port:Show('port:Show using spec.Show')
function port:Dependence(target)
print('port:Dependence(target)...')
target.Show('DI, Dependence Input; implementation binded on '..target._tag..' from port')
end
print('\nport ###test over---------------------------')
-- 派生复用
local target
= form.build('target'--[[, port]]) -- 可从#super 派生复用
:spec(port)-- 可指定接口形式复用
print('\ntarget ###test start---------------------------')
target:noImplementation() -- spec:noImplementation()
target:Show('target:Show using port.Show') -- port:Show(msg)
print('\nDepend on [port] implement---------------------')
local tar_ = port:on(target)-- binding on target, implementation from port
tar_.Show('tar_.Show()')
print('\ntarget:Dependence(tar_) using port:Show(msg)---')
target:Dependence(tar_) -- port:Dependence(target)
print('\noverwrite target:Show(msg)---------------------')
function target:Show(msg)
print('target:Show(msg)...')
print(msg)
end
print('\ntarget:Dependence(tar_) using port:Show(msg)----')
target:Dependence(tar_)
print('\nDepend on [target] implement--------------------')
print('\ntarget:Dependence(tar_) using target:Show(msg)--')
tar_ = form.on(target, port)-- using target implementation spec from port
tar_.Show('tar_.Show()')
target:Dependence(tar_)
print('\ntarget ###test over-----------------------------')
form.lua提供友好的lua协约式编程form,完整满足以下两点
- 派生复用机制
- 面向接口特性
form.test 运行成功。后续将给出Lua协约式编程范例;基于form.lua实现通知订阅方式
[Lua]表驱动索引编程,form.lua的更多相关文章
- [lua]原来这才是表驱动的正确表达方式
曾经写了个很煞笔的脚本模拟switch..case语法形式.[lua]尝试一种Case语法糖 而今实际项目应用中突发,原来这才是正确的表驱动方式表达.如下所贴: function event_do( ...
- <4>Lua表
lua表 1: lua没有数组,但是表可以代替数组的功能(数组部分与非数组部分); 开始的, 1, 2, 3 ...称连续的索引; b.Lua表的连续索引的长度(数组部分); #表的名字; --数组 ...
- Lua表(table)的用法_个人总结
Lua表(table)的用法_个人总结 1.表的创建及表的介绍 --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意 ...
- Lua 学习之基础篇四<Lua table(表)>
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- LUA表克隆方法归纳
lua表克隆 将lua一个表, 克隆出一份为一个独立的另外一个表. 对于一个module, 如果在require之后,获得的表对象, 不能直接修改, 例如lua缓存此表, 但是多次逻辑执行, 都使用的 ...
- Python编程和 Lua编程的比较
Python编程和 Lua编程的比较 2016.4.21 定义函数: python: def functionname( parameters ): "函数_文档字符串" func ...
- 表驱动方法(Table-Driven Methods)
表驱动方法(Table-Driven Methods) - winner_0715 - 博客园 https://www.cnblogs.com/winner-0715/p/9382048.html W ...
- Lua 学习之基础篇八<Lua 元表(Metatabble)&&继承>
讲到元表,先看一段table的合并动作. t1 = {1,2} t2 = {3,4} t3 = t1 + t2 attempt to perform arithmetic on a table val ...
- Lua 学习之基础篇二<Lua 数据类型以及函数库 汇总>
引言 前面讲了运算符,这里主要对Lua的数据处理相关的数据类型和函数库进行总结归纳,后面会再接着单独分开讲解具体使用. 首先因为Lua 是动态类型语言,变量不要类型定义,只需要为变量赋值. 值可以存储 ...
随机推荐
- JavaScript高级程序设计42.pdf
IE中的事件对象 要访问IE中的event对象有几种不同的方式,取决于指定事件处理程序的方法.在使用DOM0级方法添加事件处理程序时,event对象作为window对象的一个属性存在 wondow.e ...
- CSS样式学习笔记『W3School』
1.选择器+声明声明:属性+值eg:h1{color:red;font-size:14px;}颜色:p{color:#ff0000;}p{color:#f00;}p{color:rgb(255,0,0 ...
- Axure RP 8.0 中继器初体验
为了解决增删等复杂交互的问题,中继器是个不错的选择. 拖拽出一个默认的中继器 中继器的数据集感觉就像是数据库一样,在右边检视窗口中可以看到中继器的默认数据集,可以理解成一张二维表.默认有1列,现成的3 ...
- 研磨设计模式解析及python代码实现——(二)外观模式(Facade)
一.外观模式定义 为子系统中的一组接口提供一个一致的界面,使得此子系统更加容易使用. 二.书中python代码实现 class AModuleApi: def testA(self): pass cl ...
- Microsoft.Jet.Oledb.4.0 提供者並未登錄於本機電腦上
最近把一些 .NET2.0 的專案從 x86 的 Server 搬到 x64 的 Server 上,一直都相安無事,直到今天才發現使用 Oledb 讀取 Excel 的時候會跳出「'Microsoft ...
- /etc/init.d/syslog: No such file or directory
centos 6 使用的是 rsyslog 而 centos5 使用 syslog ,兩個不同版本使用軟體不同.因此,你得要修訂 /etc/rsyslog.conf 才行!軟體也變成 /etc/ini ...
- div+css的兼容性问题和解决方法
1. 默认的内外边距不同 问题: 各个浏览器默认的内外边距不同 解决: *{margin:0;padding:0;} 2. 水平居中的问题 问题: 设置 text-align: center ...
- c的基础 1. 无符号数和补码
计算机中储存和处理的信息是以二进制信号表示的.单个的位不是是很实用,而将这些位 组合在一起,加上某种解释,即给不同的可能位模式赋予含义,我们就行表示怎样有限集合的元素,即实现各种数据结构.计算机中使用 ...
- PHP安全外延
接下来,我们讲一下:php语言与Apache等中间.MySQL等数据库结合使用时所产生的一些安全问题. 1.文件解析漏洞分析 2.编码注入漏洞分析 3.is_numeric漏洞分析
- hdu2025java字符题
查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...