各设计模式总结与对比
设计模式 | 总结 | 应用 |
---|---|---|
工厂模式 | 将创建对象过程封装 | BeanFactory Calendar |
原型模式 | 复制,克隆 | PrototypeBean |
单例模式 | 当前进程内独一无二 | ApplicationContext Calendar |
适配器模式 | 兼容转换 | AdvisorAdapter HandlerAdapter |
装饰者模式 | 通过构造函数,层层包装 | BufferReader InputStream OutputStream HttpHeadResponseDecorator |
代理模式 | 根据原有方法或类,重新创建类,去增强原有逻辑 | ProxyFacotryBean JDKDynamicAopProxy CglibAopproxy |
委托模式 | "领导"知道所有员工擅长做的事,根据事情选择由哪个员工去做 | DispatcherServlet BeanDefinitionParserDelegate |
模板模式 | 固化算法,将特性延迟到子类实现 | JdbcTemplate HttpServlet |
观察者模式 | 根据被观察对象变化,而进行相应的"更新" | ContextLoaderListener |
策略模式 | 根据不同选择,进行不同算法切换 | InstantitionStrategy |
DI
@Autowired
private DemoService demoService;
IOC
// IOC容器,通过XML配置文件,进行加载bean
ApplicationContext ctx =
new ClassPathXmlApplicationContext("bean.xml");
Food food = ctx.getBean("food", Food.class);
System.out.println(food.getName()+":"+food.getNum());
AOP
@Aspect
public class Aop {
@Before(value = "execution(* com.bardream.demo.AopTest.*(..))")
public void before() {
System.out.println("在调用方法,之前执行....");
}
@After(value = "execution(* com.bardream.demo.AopTest.*(..))")
public void after() {
System.out.println("在调用方法,之后执行....");
}
}