@linux1s1s
2019-02-18T18:06:37.000000Z
字数 2816
阅读 1972
Java设计模式
前一节已经介绍了Java设计模式 -- 适配器/装饰器模式 ,因为一笔带过,所以这里再补充一下。
关于装饰器模式,在具体的Android
源码中 比较常见,一般都是以Wrapper
作为结尾
比较典型的是ContextWrapper,这里篇幅有限,暂不做解析,文章后面会出给UML图做个简单理解。
我们不从最基本的装饰器模式概念入手,先来看问题好了。
假如 有以下海关入关通用处理方法
public class ShangHaiCustoms {
public void procedure() {
System. out.println("ShangHai common procedure!" );
}
}
public class BeiJingCustoms{
public void procedure() {
System. out.println("BeiJing customs procedure!" );
}
}
现在我们需要新加入需求,原先的处理流程不变,但是需要加入security checks
和keep on record
, 这两个部分分别位于原先方法的前面和后面,如果我们不加任何思考可以直接简单粗暴
public void securityCheck() {
System. out.println("SeafoodCustoms security checks!" );
}
public void keepOnRecord() {
System. out.println("SeafoodCustoms keep on record!" );
}
直接把这两个方法加在原先类的合适位置即可,这个修改不会带来多大的负面影响,假如我们区分 食品和海产品的 security checks和keep on record分别都不同, 那么还要修改上面的两个方法 以区分食品和海产品, 如果后续还有什么需求,是不是修改起来比较费劲,因为要不断的修改原先的类,这个明显违背了设计模式对修改封闭原则。
下面我们一起来看看如何对上面的需求做设计
先提取接口
public interface Customs {
public void procedure();
}
接着修改原先的两个类如下
public class ShangHaiCustoms implements Customs {
@Override
public void procedure() {
System. out.println("ShangHai common procedure!" );
}
}
public class BeiJingCustoms implements Customs {
@Override
public void procedure() {
System. out.println("BeiJing customs procedure!" );
}
}
接着做个包装类
public class CustomsWrapper implements Customs {
private Customs customs;
public CustomsWrapper(Customs customs) {
this.customs = customs;
}
@Override
public void procedure() {
customs.procedure();
}
}
到这先停一下, 如果对于所有的商品security checks和keep on record都相同, 那么我们直接在这个类中加入上面的securityCheck()和keepOnRecord()即可,这种情况比较简单,也好理解。
如果对于不同品种的商品security checks和keep on record都不相同,该如何处理呢?
很明显我们可以这样做:这里仅仅举普通食品和海产品例子
public class FoodCustoms extends CustomsWrapper {
public FoodCustoms(Customs customs) {
super(customs );
}
@Override
public void procedure() {
// Before do someThing, security checks;
securityCheck();
super.procedure();
// After do someThing, keep on record
keepOnRecord();
}
public void securityCheck() {
System. out.println("FoodCustoms security checks!" );
}
public void keepOnRecord() {
System. out.println("FoodCustoms keep on record!" );
}
}
public class SeaFoodCustoms extends CustomsWrapper {
public SeaFoodCustoms(Customs customs) {
super(customs );
}
@Override
public void procedure() {
// Before do someThing, security checks;
securityCheck();
super.procedure();
// After do someThing, keep on record
keepOnRecord();
}
public void securityCheck() {
System. out.println("SeafoodCustoms security checks!" );
}
public void keepOnRecord() {
System. out.println("SeafoodCustoms keep on record!" );
}
}
然后我们可以在测试用例中体现多态了
public class TestCase {
public static void main(String[] args) {
/*
* ShangHai customs common procedure;
*/
commonProcedure();
/*
* ShangHai customs dynamic procedure;
*/
dynamicProcedure();
}
private static void dynamicProcedure() {
Customs dyCustoms = new ShangHaiCustoms();
CustomsWrapper dyWrapper = new FoodCustoms(dyCustoms );
dyWrapper.procedure();
}
private static void commonProcedure() {
Customs customs = new ShangHaiCustoms();
CustomsWrapper wrapper = new CustomsWrapper(customs);
wrapper.procedure();
}
}
用个简单的图表示如下:
上面提及的ContextWrapper结构图