这是一个概念性问题。
在查看代码时,我想要完全理解复合设计模式。PBJ
是保持表观类型食品的叶子一个复合对象,但是,它是由封装保护,只有Bread
,Jelly
并且PeanutButter
可以被设置为的元素。
我并不是在倡导人们使用多个字段变量而不是一个List。我知道很多代码可以优化并且重复。然而,我和其他人将在未来对设计模式进行测试,一些教师会尝试一些棘手的事情,以确保他们的学生对材料有充分的了解。
所以你认为这个Java程序Run.java符合复合设计标准吗?
import java.util.*;interface Food{ public void inspect();}class PBJ implements Food{ private Food bread; private Food jelly; private Food peanutButter; public void setBread(Bread bread){this.bread=bread;} public void setJelly(Jelly jelly){this.jelly=jelly;} public void setPeanutButter(PeanutButter butter){this.peanutButter=butter;} public void inspect(){ if(bread==null || jelly == null || peanutButter == null){ System.out.println("Your PB&J is not finished being made yet"); } else { System.out.println("Yay time to enjoy your PBJ! Wow that was quick"); } }}class Bread implements Food{ private Flour flour; private Salt salt; public Bread(Flour flour, Salt salt){ this.flour = flour; this.salt = salt; } public void inspect(){System.out.println("Dang those are fine bunns");}}class PeanutButter implements Food{ private Peanuts peanuts; private Salt salt; public PeanutButter(Peanuts peanuts, Salt salt){ this.peanuts = peanuts; this.salt = salt; } public void inspect(){System.out.println("Ahhh I need water now");}}class Jelly implements Food{ private Fruit fruit; private Sugar sugar; public Jelly(Fruit fruit, Sugar sugar){ this.fruit = fruit; this.sugar = sugar; }
这是用于快速参考的UML图。
慕尼黑5688855
SMILET
相关分类