接口 类复用

Thinking in java(5)

1. 策略模式

import java.io.*;
import java.util.*;

//策略模式

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();
	}
}

public class Apply {
	public static void apply(Processor p, Object s) {
		System.out.println(p.process(s));
	}
	public static String str = "just DO iT";
	public static void main(String[] args) {
		apply(new Upcase(), str);
		apply(new Downcase(), str);
	}
}

2. 使用接口复用类

ProcessorI.java

public interface ProcessorI {
	String name();
	Object process(Object input);
}

ApplyI.java

import java.io.*;
import java.util.*;

public class ApplyI {
	public static void process(ProcessorI p, Object s) {
		System.out.println("Using Processor " + p.name());
		System.out.println(p.process(s));
	}
}

ProcessString.java

import java.io.*;
import java.util.*;

abstract class DealString implements ProcessorI{
	public String name() {
		return getClass().getSimpleName();
	}
	public abstract String process(Object input);
}

class ToUp extends DealString {
	public String process(Object input) {
		return ((String)input).toUpperCase();
	}
}

class ToDown extends DealString {
	public String process(Object input) {
		return ((String)input).toLowerCase();
	}
}

public class ProcessString {
	static String str = "Just Do It";
	public static void main(String[] args) {
		ApplyI.process(new ToUp(), str);
		ApplyI.process(new ToDown(), str);
	}
}

1.3 适配器模式

实际中碰到的大多数类我们都无法修改,因此我们可以采取适配器模式

ProcessorI.java

public interface ProcessorI {
	String name();
	Object process(Object input);
}

ApplyI.java

import java.io.*;
import java.util.*;

public class ApplyI {
	public static void process(ProcessorI p, Object s) {
		System.out.println("Using Processor " + p.name());
		System.out.println(p.process(s));
	}
}

ProcessAdapter.java

import java.io.*;

abstract class ProcessBase {
	public String name() {
		return getClass().getSimpleName();
	}
	public abstract Object process(Object input);
}

class ToUpcase extends ProcessBase {
	public String process(Object input) {
		return ((String)input).toUpperCase();
	}
}

class ToDowncase extends ProcessBase {
	public String process(Object input) {
		return ((String)input).toLowerCase();
	}
}

public class ProcessAdapter implements ProcessorI {
	private ProcessBase process;
	public ProcessAdapter(ProcessBase process) {
		this.process = process;
	}
	@Override
	public String name() {
		// TODO Auto-generated method stub
		return process.name();
	}

	@Override
	public Object process(Object input) {
		// TODO Auto-generated method stub
		return process.process(input);
	}
	public static void main(String[] args) {
		String input = "Just do It";
		ApplyI.process(new ProcessAdapter(new ToDowncase()), input);
		ApplyI.process(new ProcessAdapter(new ToUpcase()), input);
	}
}

1.4 工厂模式

import java.io.*;

//工厂模式 需提供两个类 一个类负责执行服务 另外一个类负责获取服务

interface Service {
	void method1();
	void method2();
}

interface ServiceFactory {
	Service getService();
}

class Implementation1 implements Service {
	Implementation1() {}

	@Override
	public void method1() {
		// TODO Auto-generated method stub
		System.out.println("服务1");
	}

	@Override
	public void method2() {
		// TODO Auto-generated method stub
		System.out.println("服务2");
	}
}

class Implementation1Factory implements ServiceFactory {

	@Override
	public Service getService() {
		// TODO Auto-generated method stub
		return new Implementation1();
	}
}

public class Factories {
	public static void serviceConsumer(ServiceFactory fact) {  //传入工厂 执行服务
		Service serv = fact.getService();
		serv.method1();
		serv.method2();
	}
	public static void main(String[] args) {
		serviceConsumer(new Implementation1Factory());
	}
}

posted @ 2017-09-04 15:11  xing-xing  阅读(764)  评论(0编辑  收藏  举报