Java设计模式泛型化之装饰模式

装饰模式是Java开发中常见的设计模式之一。我们在Service层的设计实现中就用到了装饰模式。

对于它的理解是这样的:

在原有已经实现的功能上添加新的功能实现。

其结构是这样的:

一个原有的抽象层(接口或者抽象类)
对应既有抽象层的实现层
一个Decorator类来实现原有抽象层
一个或多个具体的Decorator类扩展这个Decorator实现
一个调用者

来看看代码:

原有的抽象层

public interface Origin {

	public void doSomething();
	
	public void doOtherthing();
}

既有抽象层的实现层

public class OriginImpl implements Origin {

	@Override
	public void doSomething() {
		System.out.println("Origin do something.");
	}

	@Override
	public void doOtherthing() {
		System.out.println("Origin do other thing.");
	}

}

Decorator类

public abstract class Decorator implements Origin {

	private Origin org;
	
	public Decorator(Origin org) {
		this.org = org;
	}

	@Override
	public void doSomething() {
		org.doSomething();
	}

	@Override
	public void doOtherthing() {
		org.doOtherthing();
	}
	
}

不同的装饰者

public class DecoratorA extends Decorator {
	
	public DecoratorA(Origin org) {
		super(org);
	}
	
	private void doNothing() {
		System.out.println("Decorator A do nothing.");
	}
	
	private void doAnotherthing() {
		System.out.println("Decorator A do another thing.");
	}
	
	@Override
	public void doSomething() {
		doNothing();
		super.doSomething();
	}

	@Override
	public void doOtherthing() {
		super.doOtherthing();
		doAnotherthing();
	}

}

public class DecoratorB extends Decorator {
	
	public DecoratorB(Origin org) {
		super(org);
	}
	
	private void before() {
		System.out.println("Decorator B do it before.");
	}
	
	private void after() {
		System.out.println("Decorator B do it after.");
	}

	@Override
	public void doSomething() {
		super.doSomething();
		after();
	}

	@Override
	public void doOtherthing() {
		before();
		super.doOtherthing();
	}

}

调用者

public class DecoratorCaller {

	public static void main(String[] args) {
		Origin org = new OriginImpl();
		DecoratorA decA = new DecoratorA(org);
		DecoratorB decB = new DecoratorB(org);
		decA.doSomething();
		decB.doOtherthing();
	}
}

那么,如何泛型化呢?

不难看出,Decorator才是关键所在。因此,泛型化处理是这样的:

Decorator

@SuppressWarnings("all")
public abstract class Decorator implements Origin {

	private T t;
	
	public Decorator(T t) {
	    this.t = t;
	}

	@Override
	public void doSomething() {
		t.doSomething();
	}

	@Override
	public void doOtherthing() {
		t.doOtherthing();
	}
	
}

具体实现类

public class DecoratorA extends Decorator {

	public DecoratorA(T t) {
		super(t);
	}
	
	private void doNothing() {
		System.out.println("Decorator A do nothing.");
	}
	
	private void doAnotherthing() {
		System.out.println("Decorator A do another thing.");
	}
	
	@Override
	public void doSomething() {
		doNothing();
		super.doSomething();
	}

	@Override
	public void doOtherthing() {
		super.doOtherthing();
		doAnotherthing();
	}

}

public class DecoratorB extends Decorator {
	
	public DecoratorB(T t) {
		super(t);
	}
	
	private void before() {
		System.out.println("Decorator B do it before.");
	}
	
	private void after() {
		System.out.println("Decorator B do it after.");
	}

	@Override
	public void doSomething() {
		super.doSomething();
		after();
	}

	@Override
	public void doOtherthing() {
		before();
		super.doOtherthing();
	}

}

调用者

public class DecoratorCaller {

	public static void main(String[] args) {
		Origin org = new OriginImpl();
		DecoratorA decA = new DecoratorA(org);
		DecoratorB decB = new DecoratorB(org);
		decA.doSomething();
		decB.doOtherthing();
	}
}



你可能感兴趣的:(Java,设计,Java技术)