标签:
1 -module(chinese_cook). 2 -author("Russell-X-Shanso"). 3 4 -behaviour( factory ). 5 -behaviour( abstract_factory ). 6 -behaviour( builder ). 7 8 %% API 9 -export([ do_best_food/0 ]). 10 -export([ do_duck/0, do_beef/0, do_vegetables/0 ]). 11 -export([ do_breakfast/0, do_lunch/0, do_diner/0 ]). 12 13 %%%%%%%%%%%%%%%%%%%%%%% 14 %% factory callback 15 do_best_food()-> 16 { ok, <<"Roast Duck">> }. 17 18 %%%%%%%%%%%%%%%%%%%%%%% 19 %% abstract_factory callback 20 do_duck() -> 21 { ok, <<"Roast Duck">> }. 22 23 do_beef() -> 24 { ok, <<"Tomato beef stew">> }. 25 26 do_vegetables() -> 27 { ok, <<"Carve radish">> }. 28 29 %%%%%%%%%%%%%%%%%%%%%%% 30 %% builer callback 31 do_breakfast() -> 32 { ok, <<"Carve radish">> }. 33 34 do_lunch() -> 35 { ok, <<"Tomato beef stew">> }. 36 37 do_diner() -> 38 { ok, <<"Roast Duck">> }.
西餐厨师(western_cook.erl):
1 -module(western_cook). 2 -author("Russell-X-Shanso"). 3 4 -behaviour( factory ). 5 -behaviour( abstract_factory ). 6 -behaviour( builder ). 7 8 %% API 9 -export([ do_best_food/0 ]). 10 -export([ do_duck/0, do_beef/0, do_vegetables/0 ]). 11 -export([ do_breakfast/0, do_lunch/0, do_diner/0 ]). 12 13 %%%%%%%%%%%%%%%%%%%%%%% 14 %% factory callback 15 do_best_food()-> 16 { ok, <<"Duck liver">> }. 17 18 %%%%%%%%%%%%%%%%%%%%%%% 19 %% abstract_factory callback 20 do_duck() -> 21 { ok, <<"Duck liver">> }. 22 23 do_beef() -> 24 { ok, <<"Beefsteak">> }. 25 26 do_vegetables() -> 27 { ok, <<"Salad">> }. 28 29 %%%%%%%%%%%%%%%%%%%%%%% 30 %% builer callback 31 do_breakfast() -> 32 { ok, <<"Salad">> }. 33 34 do_lunch() -> 35 { ok, <<"Duck liver">> }. 36 37 do_diner() -> 38 { ok, <<"Beefsteak">> }.
韩国厨师(korea_cook.erl):
1 -module(korea_cook). 2 -author("Russell-X-Shanso"). 3 4 -behaviour( factory ). 5 -behaviour( abstract_factory ). 6 -behaviour( builder ). 7 8 %% API 9 -export([ do_best_food/0 ]). 10 -export([ do_duck/0, do_beef/0, do_vegetables/0 ]). 11 -export([ do_breakfast/0, do_lunch/0, do_diner/0 ]). 12 13 %%%%%%%%%%%%%%%%%%%%%%% 14 %% factory callback 15 do_best_food()-> 16 { ok, <<"Pickle">> }. 17 18 %%%%%%%%%%%%%%%%%%%%%%% 19 %% abstract_factory callback 20 do_duck() -> 21 { error, <<"Sorry, This cook Can‘t cook duck">> }. 22 23 do_beef() -> 24 { error, <<"Sorry, This cook can‘t cook beef">> }. 25 26 do_vegetables() -> 27 { ok, <<"Pickle">> }. 28 29 %%%%%%%%%%%%%%%%%%%%%%% 30 %% builer callback 31 do_breakfast() -> 32 { ok, <<"Pickle">> }. 33 34 do_lunch() -> 35 { ok, <<"Pickle">> }. 36 37 do_diner() -> 38 { ok, <<"Pickle">> }.
1 -module(factory). 2 -author("Russell-X-Shanso"). 3 4 %% API 5 -export([ cook1/1, cook2/1 ]). 6 7 -callback do_best_food() -> { ok, binary() }. 8 9 cook1( Style )-> 10 { ok, Food } = case Style of 11 chinese -> chinese_cook:do_best_food(); 12 western -> western_cook:do_best_food(); 13 korea -> korea_cook:do_best_food() 14 end, 15 io:format( "Food is:~p~n", [ Food ] ). 16 17 18 cook2( Style )-> 19 Cook = case Style of 20 chinese -> chinese_cook; 21 western -> western_cook; 22 korea -> korea_cook 23 end, 24 { ok, Food } = apply( Cook, do_best_food, [] ), 25 io:format( "Food is:~p~n", [ Food ] ).
上面例中用cook1/1与cook2/1两种风格的代码实现了工厂对于产品的生产;
执行结果如下:
1 -module(abstract_factory). 2 -author("Russell-X-Shanso"). 3 4 %% API 5 -export([ cook/2 ]). 6 7 8 -callback do_duck() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 9 -callback do_beef() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 10 -callback do_vegetables() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 11 12 cook( Style, Material ) -> 13 Cook = case Style of 14 chinese -> chinese_cook; 15 western -> western_cook; 16 korea -> korea_cook 17 end, 18 Action = case Material of 19 duck -> do_duck; 20 beef -> do_beef; 21 vegetables -> do_vagetables 22 end, 23 case apply( Cook, Action, [] ) of 24 { ok, Food } -> io:format( "Food is:~p~n", [ Food ] ); 25 { error, ErrorInfo } -> io:format( "Oops:~p~n", [ ErrorInfo ] ) 26 end.
执行效果:
1 -module(builder). 2 -author("Russell-X-Shanso"). 3 4 %% API 5 -export([ cook/1 ]). 6 7 -callback do_breakfast() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 8 -callback do_lunch() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 9 -callback do_diner() -> { ok, binary() } | { error, ErrorInfo :: binary() }. 10 11 cook( Style ) -> 12 Cook = case Style of 13 chinese -> chinese_cook; 14 western -> western_cook; 15 korea -> korea_cook 16 end, 17 director( Cook ). 18 19 20 director( Cook ) -> 21 { ok, Breakfast } = apply( Cook, do_breakfast, [] ), 22 io:format( "Breakfast: ~p~n", [ Breakfast ] ), 23 { ok, Lunch } = apply( Cook, do_lunch, [] ), 24 io:format( "Lunch: ~p~n", [ Lunch ] ), 25 { ok, Dinner } = apply( Cook, do_diner, [] ), 26 io:format( "Dinner: ~p~n", [ Dinner ] ).
执行效果如下:
标签:
原文地址:http://www.cnblogs.com/shanso/p/4762117.html