背景
继续查缺补漏
1.策略设计模式
创建一个能够根据所传递的参数对象的不同而具有不同行为的方法,被称为策略设计模式。
2.完全解耦
只要一个方法操作的是类而非接口,那么你就使能使用这个类及其子类,如果你想要将这个方法应用于不在此
继承结构中的某个类就会出问题。接口可以放宽这种限制。通过适配器方式和代理方式实现上述情况。将接口
从具体实现中解耦,可以使接口应用于多种不同的具体实现,因此代码就更具可复用性。例如:
package c09;
public interface Processor {
String name();
Object process(Object input);
}
package c09;
public class Filter {
public String name(){
return getClass().getSimpleName();
}
public Waveform process(Waveform input){ return input;}
}
package c09;
import java.util.*;
import static tools.Print.*;
public class Apply {
public static void process(Processor p,Object s){
print("Using Processor"+p.name());
print(p.process(s));
}
}
package c09;
class FilterAdapter implements Processor{
Filter filter;
public FilterAdapter(Filter filter){
this.filter=filter;
}
public String name(){ return filter.name();}
public Waveform process(Object input){
return filter.process((Waveform)input);
}
}
public class FilterProcessor {
public static void main(String[] args){
Waveform w=new Waveform();
Apply.process(new FilterAdapter(new LowPass(1.0)),w);
Apply.process(new FilterAdapter(new HighPass(2.0)),w);
Apply.process(new FilterAdapter(new BandPass(3.0,4.0)),w);
}
}
3.适配接口
你可以用任何你想要的对象来调用我的方法,只要你的对象遵循我的接口。
package c09;
import java.util.*;
import java.nio.*;
public class RandomWords implements Readable {
private static Random rand=new Random(47);
private static final char[] capitals="ABCDEFGHIZKLMNOPQRSTUVWSYZ".toCharArray();
private static final char[] lowers="abcdefghizklmnopqrstuvwxyz".toCharArray();
private static final char[] vowels="aeiou".toCharArray();
private int count;
public RandomWords(int count){ this.count=count;}
public int read(CharBuffer cb){
if(count--==0)
return -1;
cb.append(capitals[rand.nextInt(capitals.length)]);
for(int i=0;i<4;i++){
cb.append(vowels[rand.nextInt(vowels.length)]);
cb.append(lowers[rand.nextInt(lowers.length)]);
}
cb.append(" ");
return 10;
}
public static void main(String[] args){
Scanner s=new Scanner(new RandomWords(10));
while(s.hasNext())
System.out.println(s.next());
}
}
让方法接受接口类型,是一种让任何类都可以对该方法进行适配的方式。
4. 嵌套接口
实现一个private接口只是一种方式,它可以强制该接口中的方法定义不要添加任何类型信息,也就是说,
不允许向上转型。
嵌套在另一个接口中的接口自动就是public,而不可以声明为private
当实现某个接口时,并不需要实现嵌套在其内部的任何接口。private接口不能在定义它的类之外被实现。
5.接口与工厂
工厂方法设计模式:我们在工厂对象上调用的是创建方法,而该工厂对象将生成接口的某个实现的对象。
我们的代码可以完全与接口的实现分离,使我们可以透明地将某个实现替换为另一个实现。例如:
package c09;
import static tools.Print.*;
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){} //Package access
public void method1(){ print("Implementation1 method1");}
public void method2(){ print("Implementation1 method2");}
}
class Implementation1Factory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2 implements Service{
Implementation2(){}//Package access
public void method1(){ print("Implementation2 method1");}
public void method2(){ print("Implementation2 method2");}
}
class Implementation2Factory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
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 Implementation1Factory());
//Implementations are conpletely interchangeable:
serviceConsumer(new Implementation2Factory());
}
}
可以用来创建框架,如果创建一个对弈游戏系统,例如,在相同的棋盘上下国际象棋和西洋跳棋:
package c09;
import static tools.Print.*;
interface Game{ boolean move();}
interface GameFactory{Game getGame();}
class Checkers implements Game{
private int moves=0;
private static final int MOVES=3;
public boolean move(){
print("Checkers move"+moves);
return ++moves!=MOVES;
}
}
class CheckersFactory implements GameFactory{
public Game getGame(){ return new Checkers();}
}
class Chess implements Game{
private int moves=0;
private static final int MOVES=4;
public boolean move(){
print("Check move "+moves);
return ++moves!=MOVES;
}
}
class ChessFactory implements GameFactory{
public Game getGame(){ return new Chess();}
}
public class Games {
public static void playGame(GameFactory factory){
Game s=factory.getGame();
while(s.move());
}
public static void main(String[] args){
playGame(new CheckersFactory());
playGame(new ChessFactory());
}
}
如果Games类表示一段复杂的代码,那么这种方式允许你在不用类型的游戏中复用这段代码。