庚子鼠年 己卯月 甲寅日
描述
今天录制了关下于java代码分析的视频,学习组合模式,装饰者模式
优秀博客推荐:https://blog.csdn.net/ThinkWon/article/details/102980571
随笔
组合模式
参考博客:https://baijiahao.baidu.com/s?id=1639579541093787227&wfr=spider&for=pc
https://www.cnblogs.com/WindSun/p/10263376.html
Component: 抽象构件
public abstract class Component {
public abstract void add(Component c);
public abstract void remove(Component c);
public abstract Component getChild(int i);
public abstract void operation();
}
Leaf: 叶子构件
public class Leaf extends Component {
@Override
public void add(Component c) {
//异常处理或错误提示
}
@Override
public void remove(Component c) {
//异常处理或错误提示
}
@Override
public Component getChild(int i) {
//异常处理或错误提示
return null;
}
@Override
public void operation() {
//实现代码
}
}
Composite: 容器构件
import java.util.ArrayList;
public class Composite extends Component {
private ArrayList<Component> list = new ArrayList();
@Override
public void add(Component c) {
list.add(c);
}
@Override
public void remove(Component c) {
list.remove(c);
}
@Override
public Component getChild(int i) {
return (Component)list.get(i);
}
@Override
public void operation() {
for(Object obj:list) {
((Component)obj).operation();
}
}
}