简单的设计模式

一、策略设计模式

public class Apply {
	public static void process(Processor p, Object s) {
		System.out.println("Using Processor " + p.name());
		System.out.println(p.process(s));
	}
	public static String s = "Disagreement with beliefs is by definition incorrect";
	public static void main(String[] args) {
		process(new Upcase(), s);
		process(new Downcase(), s);
		process(new Splitter(), s);		
	}
}
class Processor {
	public String name() {
		return getClass().getSimpleName();
	}
	Object process(Object input) {
		return input;
	}
}
class Upcase extends Processor {
	String process(Object input) {
		return ((String)input).toUpperCase();
	}
}
class Downcase extends Processor {
	String process(Object input) {
		return ((String)input).toLowerCase();
	}
}
class Splitter extends Processor {
	String process(Object input) {
		return Arrays.toString(((String)input).split(" "));
	}
}

Output:

Using Processor Upcase
DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT
Using Processor Downcase
disagreement with beliefs is by definition incorrect
Using Processor Splitter
[Disagreement, with, beliefs, is, by, definition, incorrect]

创建一个能够根据所传递的参数对象的不同而具有不同行为的方法,被称为策略设计模式。这类方法包含所要执行的算法中固定不变的部分,而“策略”包含变化的部分。策略就是传递进去的参数对象,它包含要执行的代码。这里Processor对象就是一个策略。

如上例子,可用接口改造如下:

interface Processor {
	public String name();
	Object process(Object input);
}
public abstract class StringProcessor implements Processor {
	public String name() {
		return getClass().getSimpleName();
	}
	public abstract Object process(Object input);
	public static String s = "Disagreement with beliefs is by definition incorrect";
	public static void main(String[] args) {
		Apply.process(new Upcase(), s);
		Apply.process(new Downcase(), s);
		Apply.process(new Splitter(), s);		
	}
}
class Apply {
	public static void process(Processor p, Object s) {
		System.out.println("Using Processor " + p.name());
		System.out.println(p.process(s));
	}
}
class Upcase extends StringProcessor {
	public String process(Object input) {
		return ((String)input).toUpperCase();
	}
}
class Downcase extends StringProcessor {
	public String process(Object input) {
		return ((String)input).toLowerCase();
	}
}
class Splitter extends StringProcessor {
	public String process(Object input) {
		return Arrays.toString(((String)input).split(" "));
	}
}

二、适配器设计模式

class FilterAdapter implements Processor {
	Filter filter;
	public FilterAdapter(Filter filter) {
		this.filter = filter;
	}
	@Override
	public String name() {
		return filter.name();
	}
	@Override
	public Waveform process(Object input) {
		return filter.process((Waveform) input);
	}
}
public class FilterProcessor {
	public static void main(String[] args) {
		Waveform waveform = new Waveform();
		Apply.process(new FilterAdapter(new LowPass(1.0)), waveform);
		Apply.process(new FilterAdapter(new HighPass(2.0)), waveform);
		Apply.process(new FilterAdapter(new BandPass(3.0, 4.0)), waveform);
	}
}
class Waveform {
	private static long counter;
	private final long id = counter++;
	public String toString() {
		return "Waveform " + id;
	}
}
class Filter {
	public String name() {
		return getClass().getSimpleName();
	}
	public Waveform process(Waveform input) {
		return input;
	}
}
class LowPass extends Filter {
	double cutoff;
	public LowPass(double cutoff) {
		this.cutoff = cutoff;
	}
	public Waveform process(Waveform input) {
		return input;
	}
}
class HighPass extends Filter {
	double cutoff;
	public HighPass(double cutoff) {
		this.cutoff = cutoff;
	}
	public Waveform process(Waveform input) {
		return input;
	}
}
class BandPass extends Filter {
	double lowCutoff, highCutoff;
	public BandPass(double lowCutoff, double highCutoff) {
		this.lowCutoff = lowCutoff;
		this.highCutoff = highCutoff;
	}
	public Waveform process(Waveform input) {
		return input;
	}
}

Filter与Processor具有相同的接口元素,但是因为它并非继承自Processor,因此你不能将Filter用于Apply.process()方法。这时可以使用适配器设计模式。适配器中的代码将接受你所有的接口,并产生你所需要的接口。FilterAdapter的构造器接受你所拥有的接口Filter,然后生成具有你所需要的Processor接口的对象。而且,在FilterAdapter类中用到了代理。

三、单例模式

public class Singleton {
	void testPrivate() {
//		Soup1 soup1 = new Soup1();//私有构造器,无法实例化
	}
	void testStatic() {
		Soup1 soup1 = Soup1.makeSoup();
	}
	void testSingleton() {
		Soup2.access().f();
	}
}
class Soup1 {
	private Soup1() {}
	public static Soup1 makeSoup() {
		return new Soup1();
	}
}
class Soup2 {
	private Soup2() {}
	private static Soup2 ps1 = new Soup2();
	public static Soup2 access() {
		return ps1;
	}
	public void f() {}
}

 Soup2用到了所谓的单例设计模式,因为你始终只能创建它的一个对象。Soup2类的对象是作为Soup2的一个static private成员而创建的,所以有且仅有一个,而且除非是通过public方法access(),否则是无法访问它的。

四、工厂方法设计模式

public class Factories {
	public static void serviceConsumer(ServiceFactory fact) {
		Service s = fact.getService();
		s.method1();
		s.method2();
	}
	public static void main(String[] args) {
		serviceConsumer(new Inpl1Factory());
		serviceConsumer(new Inpl2Factory());
	}
}
interface Service {
	void method1();
	void method2();
}
interface ServiceFactory {
	Service getService();
}
class Impl1 implements Service {
	public Impl1() {}
	public void method1() {
		System.out.println("Impl1 method1");
	}
	public void method2() {
		System.out.println("Impl1 method2");
	}
}
class Inpl1Factory implements ServiceFactory {
	public Service getService() {
		return new Impl1();
	}
}
class Impl2 implements Service {
	public Impl2() {}
	public void method1() {
		System.out.println("Impl2 method1");
	}
	public void method2() {
		System.out.println("Impl2 method2");
	}
}
class Inpl2Factory implements ServiceFactory {
	public Service getService() {
		return new Impl2();
	}
}

 接口是实现多重继承的途径,而生成遵循某个接口的对象的典型方式就是工厂方法设计模式。这与直接调用构造器不同,我们在工厂对象上调用的是创建方法,而该工厂对象将生成接口的某个实现的对象。如果不是用工厂方法,代码就必须在某处指定将要创建的Service的确切类型,以便调用合适的构造器。

摘自《Java编程思想》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值