来自:http://www.cnblogs.com/ziyouchutuwenwu/p/3722696.html
R15开始,回调模型使用callback来约定,更加好理解了
test_behavior.erl
-module(test_behavior). -callback ok_func()-> {OK::atom()}. -callback no_reply_func(Reason::atom())-> {noreply}.
test_impl.erl
-module(test_impl). -behaviour(test_behavior). -compile(export_all). ok_func()-> ok. no_reply_func(Reason)-> io:format("~p~n",[Reason]), noreply.
test.erl
-module(test). -compile(export_all). call_behavior_ok(Mod)-> Mod:ok_func(). call_behavior_no_reply(Mod)-> Mod:no_reply_func(fuck).
测试
test:call_behavior_ok(test_impl).
test:call_behavior_no_reply(test_impl).
本文通过具体的Erlang代码示例展示了如何定义和实现行为模式,包括使用-callback来指定行为接口的方法,以及如何在实现模块中遵循这些约定。

425

被折叠的 条评论
为什么被折叠?



