forked from basho/erlang_guidelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp_encapsulation.erl
40 lines (27 loc) · 978 Bytes
/
otp_encapsulation.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-module(otp_encapsulation).
-behavior(gen_server).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, nil, []).
good() ->
%% Good, because this function is an API call that encapsulates our gen_server implementation:
gen_server:call(?SERVER, do_good).
bad() ->
%% Bad, because we're sending an event to some other process whose implementation is defined
%% in another module. This breaks encapsulation:
gen_fsm:send_all_state_event(some_fsm, make_everyone_sad).
%% gen_server implementation
init(State) ->
{ok, State}.
handle_call(do_good, _From, State) ->
{reply, yay, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Msg, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVersion, State, _Extra) ->
{ok, State}.