虽然我在学习的过程中很少写过interface,但是却经常使用到implements,来让写的类遵循某个特定的接口。学习interface,首先我们还是先来了解一下抽象类吧:
抽象方法:仅由声明,没有方法体的方法。
包含抽象方法的类叫做抽象类,如果一个类包含一个或者多个抽象方法,则该类必须为抽象类,然而抽象类不一定必须具有抽象方法。
抽象类的功能:阻止产生这个类的任何对象(想一想单例模式??),非常有用的重构工具(我也不清楚,等以后知道了再来分析)。
而对于接口interface,interface这个关键字产生一个完全抽象的类,没有任何具体的实现,接口内的方法自动都是为public的。interface与抽象类比较而言,其核心优势在于:接口允许我们可以创建一个能够向上转型为多种基类的类,来实现类似多重继承的特性(这也是我们前面分析继承与组合该如何选择的重要分析点)。
看了看感觉接口并没有什么难点,这里重点学习下两个设计模式吧:适配器设计模式和工厂方法设计模式。
①适配器模式
我们经常遇到的情况是:需要将已有的实现类来满足特定的接口的调用。
1)已有应用Apply可以调用处理器
- public class Apply {
- public static void process(Processor p, Object s){
- System.out.println("Using Processor" + p.name());
- System.out.println(p.process(s));
- }
- }
2)已有的处理器Processor接口
- public interface Processor {
- String name();
- Object process(Object input);
- }
3)我们已经具有了高通滤波器HighPass和低通滤波器LowPass
- public class Filter {
- public String name(){
- return getClass().getSimpleName();
- }
- public String process(String input){
- return input + "";
- }
- }
- public class HighPass extends Filter{
- double cutoff;
- @Override
- public String process(String input){
- return input + "lower than " + cutoff + "can't pass.";
- }
- public HighPass(double cutoff){
- this.cutoff = cutoff;
- }
- }
- public class LowPass extends Filter{
- double cutoff;
- @Override
- public String process(String input){
- return input + "higher than " + cutoff + "can't pass.";
- }
- public LowPass(double cutoff){
- this.cutoff = cutoff;
- }
- }
我们希望Apply可以直接使用高通与低通滤波器。
我们可以建立如下的适配器类:
- public class FilterAdapter implements Processor{
- private Filter filter;
- public String name(){
- return filter.name();
- }
- public String process(Object input){
- return filter.process(input.toString());
- }
- public FilterAdapter(Filter filter){
- this.filter = filter;
- }
- }
这样便可以通过完成调用。
- Apply.process(new FilterAdapter( new LowPass(5.5)), "test");
- Apply.process(new FilterAdapter( new HighPass(5.5)), "test");
可以看出FilterAdapter接收已经拥有的接口Fiter,生成满足需要Processor接口的对象。考虑一下这种方式和以前所提到的代理(避免由于继承而导致基类中的方法过度的暴露)有着异曲同工之妙。
②工厂方法模式
假设我们需要建立一个游戏平台,可以在上面玩耍不同的游戏,而不用修改代码。
- interface Game{
- void play();
- }
- interface GameFactory{
- Game getGame();
- }
- class Checker implements Game{
- public void play(){
- System.out.println("checker game start");
- }
- }
- class CheckersFactory implements GameFactory{
- public Game getGame(){
- return new Checker();
- }
- }
- class Chess implements Game{
- public void play(){
- System.out.println("chess game start");
- }
- }
- class ChessFactory implements GameFactory{
- public Game getGame() {
- return new Chess();
- }
- }
- public class GamePlatform {
- public static void playGame(GameFactory factory){
- factory.getGame().play();
- }
- public static void main(String[] args) {
- playGame(new CheckersFactory());
- playGame(new ChessFactory());
- }
- }
我们可以看出工厂方法的优点在于我们不必事先确定game的类型,我们game的获取不是通过构造器,而是通过工厂对象调用创建game的方法,这样我们就可以将代码与接口的实现相分离,使得GamePlatform内的代码得到复用。
③接口内的field都自动为static和final,所以不能为空值,即声明时就必须初始化。