behaviour是啥,看了资料做了demo以后,感觉像接口,话不多说,祭代码
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).