效果

  1. 出海打鱼~~~~
  2. 面团:普通面包团
  3. 酱料:普通酱料
  4. 25 分钟
  5. 切成6
  6. 装箱
  7. ======================================
  8. ==[海鲜披萨 杭州出品]==
  9. ==原料场地:杭州披萨原料加工厂==
  10. ======================================
  11. 面团:普通面包团
  12. 酱料:普通酱料
  13. 25 分钟
  14. 切片,铺上芝士
  15. 装箱
  16. ======================================
  17. ==[芝士披萨 杭州出品]==
  18. ==原料场地:杭州披萨原料加工厂==
  19. ======================================
  20. 做不出来,用普通披萨代替
  21. 出海打鱼~~~~
  22. 面团:普通面包团
  23. 酱料:普通酱料
  24. 25 分钟
  25. 切成6
  26. 装箱
  27. ======================================
  28. ==[普通披萨 杭州出品]==
  29. ==原料场地:杭州披萨原料加工厂==
  30. ======================================
  31. 出海打鱼~~~~
  32. 面团:北京面包团
  33. 酱料:广西酱料
  34. 25 分钟
  35. 切成6
  36. 装箱
  37. ======================================
  38. ==[海鲜披萨 福建出品]==
  39. ==原料场地:福建披萨原料加工厂==
  40. ======================================
  41. 做不出来,用普通披萨代替
  42. 出海打鱼~~~~
  43. 面团:北京面包团
  44. 酱料:广西酱料
  45. 25 分钟
  46. 切成6
  47. 装箱
  48. ======================================
  49. ==[普通披萨 福建出品]==
  50. ==原料场地:福建披萨原料加工厂==
  51. ======================================
  52. 面团:北京面包团
  53. 酱料:广西酱料
  54. 25 分钟
  55. 切成6
  56. 火箭打包----发射!
  57. ======================================
  58. ==[火箭披萨 福建出品]==
  59. ==原料场地:福建披萨原料加工厂==
  60. ======================================

说明

觉得就是俩个工厂模式的集合,不过用来接口和抽象类
使程序更灵活,修改也更方便。
不过逻辑有点乱,捋清楚了还是很方便的。

完整代码

  1. package factoryMode;
  2. // ============ 原料相关 =================
  3. class Dough{
  4. public String destation() {
  5. // TODO Auto-generated method stub
  6. return "普通面包团";
  7. }
  8. }
  9. class BeijinDough extends Dough{
  10. public String destation() {
  11. // TODO Auto-generated method stub
  12. return "北京面包团";
  13. }
  14. }
  15. class Sauce{
  16. public String destation() {
  17. // TODO Auto-generated method stub
  18. return "普通酱料";
  19. }
  20. }
  21. class GuanxiSauce extends Sauce{
  22. public String destation() {
  23. // TODO Auto-generated method stub
  24. return "广西酱料";
  25. }
  26. }
  27. //============ 原料加工厂相关 =================
  28. // 披萨原料工厂接口
  29. interface PizzaIngredientFactory{
  30. public Dough CreateDough();
  31. public Sauce CreateSauce();
  32. public String getName();
  33. }
  34. // 杭州原料加工厂
  35. class HanzhouPizzaIngredientFactory implements PizzaIngredientFactory{
  36. public Dough CreateDough()
  37. {
  38. return new Dough();
  39. }
  40. public Sauce CreateSauce()
  41. {
  42. return new Sauce();
  43. }
  44. public String getName() {
  45. return "杭州披萨原料加工厂";
  46. }
  47. }
  48. // 福建原料加工厂
  49. class FujianPizzaIngredientFactory implements PizzaIngredientFactory{
  50. public Dough CreateDough()
  51. {
  52. return new BeijinDough();
  53. }
  54. public Sauce CreateSauce()
  55. {
  56. return new GuanxiSauce();
  57. }
  58. public String getName() {
  59. return "福建披萨原料加工厂";
  60. }
  61. }
  62. //============ 披萨相关 =================
  63. // 披萨抽象类
  64. abstract class Pizza
  65. {
  66. public String name;
  67. String IngredientFactoryName;
  68. Dough dough;
  69. Sauce sauce;
  70. public Pizza() {
  71. // TODO Auto-generated constructor stub
  72. name = "普通披萨";
  73. }
  74. abstract void prepare();
  75. void bake() {
  76. System.out.println("烤 25 分钟");
  77. }
  78. void cut() {
  79. System.out.println("切成6片");
  80. }
  81. void box() {
  82. System.out.println("装箱");
  83. }
  84. void lable() {
  85. System.out.println("======================================");
  86. System.out.println("==[" + name + "]==");
  87. System.out.println("==原料场地:" + IngredientFactoryName + "==");
  88. System.out.println("======================================");
  89. }
  90. }
  91. //普通披萨
  92. class NormolPizza extends Pizza{
  93. PizzaIngredientFactory pizzaIngredientFactory;
  94. public NormolPizza(PizzaIngredientFactory pizzaIngredientFactory) {
  95. name = "普通披萨";
  96. this.pizzaIngredientFactory = pizzaIngredientFactory;
  97. IngredientFactoryName = pizzaIngredientFactory.getName();
  98. }
  99. void prepare() {
  100. this.dough = pizzaIngredientFactory.CreateDough();
  101. this.sauce = pizzaIngredientFactory.CreateSauce();
  102. System.out.println("出海打鱼~~~~");
  103. System.out.println("面团:" + dough.destation());
  104. System.out.println("酱料:" + sauce.destation());
  105. }
  106. }
  107. //海鲜披萨
  108. class SeafoodPizza extends Pizza{
  109. PizzaIngredientFactory pizzaIngredientFactory;
  110. public SeafoodPizza(PizzaIngredientFactory pizzaIngredientFactory) {
  111. name = "海鲜披萨";
  112. this.pizzaIngredientFactory = pizzaIngredientFactory;
  113. IngredientFactoryName = pizzaIngredientFactory.getName();
  114. }
  115. void prepare() {
  116. this.dough = pizzaIngredientFactory.CreateDough();
  117. this.sauce = pizzaIngredientFactory.CreateSauce();
  118. System.out.println("出海打鱼~~~~");
  119. System.out.println("面团:" + dough.destation());
  120. System.out.println("酱料:" + sauce.destation());
  121. }
  122. }
  123. //芝士披萨
  124. class CheesePizza extends Pizza{
  125. PizzaIngredientFactory pizzaIngredientFactory;
  126. public CheesePizza(PizzaIngredientFactory pizzaIngredientFactory) {
  127. name = "芝士披萨";
  128. this.pizzaIngredientFactory = pizzaIngredientFactory;
  129. IngredientFactoryName = pizzaIngredientFactory.getName();
  130. }
  131. void cut() {
  132. System.out.println("切片,铺上芝士");
  133. }
  134. @Override
  135. void prepare() {
  136. // TODO Auto-generated method stub
  137. this.dough = pizzaIngredientFactory.CreateDough();
  138. this.sauce = pizzaIngredientFactory.CreateSauce();
  139. System.out.println("面团:" + dough.destation());
  140. System.out.println("酱料:" + sauce.destation());
  141. }
  142. }
  143. // 火箭披萨
  144. class RocketPizza extends Pizza{
  145. PizzaIngredientFactory pizzaIngredientFactory;
  146. public RocketPizza(PizzaIngredientFactory pizzaIngredientFactory) {
  147. name = "火箭披萨";
  148. this.pizzaIngredientFactory = pizzaIngredientFactory;
  149. IngredientFactoryName = pizzaIngredientFactory.getName();
  150. }
  151. void box() {
  152. System.out.println("火箭打包----发射!");
  153. }
  154. @Override
  155. void prepare() {
  156. // TODO Auto-generated method stub
  157. this.dough = pizzaIngredientFactory.CreateDough();
  158. this.sauce = pizzaIngredientFactory.CreateSauce();
  159. System.out.println("面团:" + dough.destation());
  160. System.out.println("酱料:" + sauce.destation());
  161. }
  162. }
  163. //============ 披萨工厂相关 =================
  164. // 工厂抽象类
  165. abstract class PizzaStore{
  166. String StoreName;
  167. public Pizza OrderPizza(String type)
  168. {
  169. Pizza pizza = CreatePizza(type);
  170. pizza.prepare();
  171. pizza.bake();
  172. pizza.cut();
  173. pizza.box();
  174. pizza.lable();
  175. return pizza;
  176. }
  177. public abstract Pizza CreatePizza(String type);
  178. }
  179. // 杭州工厂
  180. class HanzhouPizzaStore extends PizzaStore
  181. {
  182. public HanzhouPizzaStore() {
  183. // TODO Auto-generated constructor stub
  184. StoreName = "杭州分店";
  185. }
  186. @Override
  187. public Pizza CreatePizza(String type) {
  188. // TODO Auto-generated method stub
  189. HanzhouPizzaIngredientFactory hanzhouPizzaIngredientFactory = new HanzhouPizzaIngredientFactory();
  190. Pizza pizza=null;
  191. switch(type)
  192. {
  193. case "海鲜披萨":
  194. pizza = new SeafoodPizza(hanzhouPizzaIngredientFactory);
  195. break;
  196. case "芝士披萨":
  197. pizza=new CheesePizza(hanzhouPizzaIngredientFactory);
  198. break;
  199. default :
  200. pizza = new NormolPizza(hanzhouPizzaIngredientFactory);
  201. System.out.println("做不出来,用普通披萨代替");
  202. break;
  203. }
  204. pizza.name += " 杭州出品";
  205. return pizza;
  206. }
  207. }
  208. // 福建工厂
  209. class FujianPizzaStore extends PizzaStore
  210. {
  211. public FujianPizzaStore() {
  212. // TODO Auto-generated constructor stub
  213. StoreName = "福建分店";
  214. }
  215. @Override
  216. public Pizza CreatePizza(String type) {
  217. // TODO Auto-generated method stub
  218. FujianPizzaIngredientFactory fujianPizzaIngredientFactory = new FujianPizzaIngredientFactory();
  219. Pizza pizza=null;
  220. switch(type)
  221. {
  222. case "海鲜披萨":
  223. pizza = new SeafoodPizza(fujianPizzaIngredientFactory);
  224. break;
  225. case "火箭披萨":
  226. pizza=new RocketPizza(fujianPizzaIngredientFactory);
  227. break;
  228. default :
  229. pizza = new NormolPizza(fujianPizzaIngredientFactory);
  230. System.out.println("做不出来,用普通披萨代替");
  231. break;
  232. }
  233. pizza.name += " 福建出品";
  234. return pizza;
  235. }
  236. }
  237. public class main {
  238. static public void main(String[] args) {
  239. HanzhouPizzaStore pizzaStore1 = new HanzhouPizzaStore();
  240. pizzaStore1.OrderPizza("海鲜披萨");
  241. pizzaStore1.OrderPizza("芝士披萨");
  242. pizzaStore1.OrderPizza("火箭披萨");
  243. FujianPizzaStore pizzaStore2 = new FujianPizzaStore();
  244. pizzaStore2.OrderPizza("海鲜披萨");
  245. pizzaStore2.OrderPizza("芝士披萨");
  246. pizzaStore2.OrderPizza("火箭披萨");
  247. }
  248. }