1、请举例3-5个使用门面模式的应用场景。
A.公司前台 B.银行大堂经理 C.建筑包工头
2.使用装饰器模式实现一个可根据权限动态扩展功能的导航条。
public class NavItem {
private String name;
public NavItem(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface INavBar {
List<NavItem> getNavItems();
}
public class BaseNavBar implements INavBar {
@Override
public List<NavItem> getNavItems() {
List<NavItem> navItems = new ArrayList<NavItem>();
navItems.add(new NavItem("问答"));
navItems.add(new NavItem("文章"));
navItems.add(new NavItem("精品课"));
navItems.add(new NavItem("冒泡"));
navItems.add(new NavItem("商城"));
return navItems;
}
}
public abstract class AbstractNavBarDecorator implements INavBar {
private INavBar iNavBar;
public AbstractNavBarDecorator(INavBar iNavBar){
this.iNavBar = iNavBar;
}
@Override
public List<NavItem> getNavItems() {
return this.iNavBar.getNavItems();
}
}
public class VipNavBarDecorator extends AbstractNavBarDecorator {
public VipNavBarDecorator(INavBar iNavBar) {
super(iNavBar);
}
@Override
public List<NavItem> getNavItems() {
List<NavItem> items = super.getNavItems();
items.add(new NavItem("作业"));
items.add(new NavItem("题库"));
return items;
}
}
public class Client {
public static void main(String[] args) {
INavBar bar = new BaseNavBar();
bar = new VipNavBarDecorator(bar);
System.out.println(bar.getNavItems());
}
}