[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实现通知订阅方式