A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kingowe 中级黑马   /  2015-5-21 11:56  /  1680 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


        适配器设计模式





        。该设计模式 在 GUI中常用,J2EE中并不常见


    如果一个类要 implements 一个 interface,则必须要重写该 interface 中的全部 abstract 方法,此时 如果一个 interface 中的抽象方法过多,
        并且在该类中用不到这么多的 abstract 方法,则会很麻烦,所以需要一个中间的 abstract class 来过渡,即 一个 interface 先被一个 abstract
            class 实现(该 abstract class 通常被称为 适配器 Adapter) ,并在该 abstract class 中实现若干方法(方法体为空), 则 以后的需要实现该 interface
                的 class 直接 extends 该 abstract class( Adapter )即可。





interface Window{                // 定义Window接口,表示窗口操作
        public void open() ;        // 打开
        public void close() ;        // 关闭
        public void activated() ;        // 窗口活动
        public void iconified() ;        // 窗口最小化
        public void deiconified();// 窗口恢复大小
}
abstract class WindowAdapter implements Window{               
// 在该class 中 实现了全部的 interface Window 中的方法,虽然是 abstract class,但是
//该类中的方法都为已经重写的 非 abstract 方法
        public void open(){} ;        // 打开
        public void close(){} ;        // 关闭
        public void activated(){} ;        // 窗口活动
        public void iconified(){} ;        // 窗口最小化
        public void deiconified(){};// 窗口恢复大小
};
class WindowImpl extends WindowAdapter{
        public void open(){
                System.out.println("窗口打开。") ;
        }
        public void close(){
                System.out.println("窗口关闭。") ;
        }
};
public class AdapterDemo{
        public static void main(String args[]){
                Window win = new WindowImpl() ;
                win.open() ;
                win.close() ;
        }
};



        工厂设计模式


        。main 方法一般就表示一个 client,在 main()  中的代码越少越好



原设计思路:

interface Fruit{        // 定义一个水果接口
        public void eat() ;        // 吃水果
}
class Apple implements Fruit{
        public void eat(){
                System.out.println("** 吃苹果。") ;
        }
};
class Orange implements Fruit{
        public void eat(){
                System.out.println("** 吃橘子。") ;
        }
};
public class InterfaceCaseDemo03{
        public static void main(String args[]){
                Fruit f = new Apple() ;       
                // 实例化接口  此时在 main() 中指定了要操作的 subclass,如果要更换 subclass,则肯定要修改 client,
                        // 所以此种设计 不合理
                f.eat() ;                       

        }
};



修改后:

interface Fruit{        // 定义一个水果接口
        public void eat() ;        // 吃水果
}
class Apple implements Fruit{
        public void eat(){
                System.out.println("** 吃苹果。") ;
        }
};
class Orange implements Fruit{
        public void eat(){
                System.out.println("** 吃橘子。") ;
        }
};
class Factory{        // 定义工厂类
        public static Fruit getInstance(String className){
                Fruit f = null ;
                if("apple".equals(className)){
                // 判断是否要的是苹果的子类 如果此处为 className.equals("apple");  则传入 null 时会有空指针异常
                        f = new Apple() ;
                }
                if("orange".equals(className)){        // 判断是否要的是橘子的子类 此处同 apple一样
                        f = new Orange() ;
                }
                return f ;
        }
};
public class InterfaceCaseDemo05{
        public static void main(String args[]){
                Fruit f = Factory.getInstance(args[0]) ;   // 实例化接口,
                if(f!=null){        // 判断是否取得实例
                        f.eat() ;
                }
        }
};


》》--》》  面  向  接  口  编  程 (command 命令模式)

public interface Command  // 通过一个 command interface 让 ProcessArray 与具体的处理方式分离
{
    void process(int[] target) ;  // 定义一个 处理数组 的方法,具体的处理方式 由其实现类来定义
}

class PrintCommand implements Command {
        public void process(int[] target) {
                System.out.print("数组中的元素:");
                for (int temp : target) {
                        System.out.print(temp+"   ");
                }
        }
}

class AddCommand implements Command {
        public void process(int[] target) {
                int sum = 0;
                for (int temp : target) {
                        sum += temp;
                }
                System.out.print("数组中元素的和:" + sum);
        }
}

class ProcessArray {
        public void arrayWork(int[] target, Command cmd) {
                cmd.process(target);
        }
}
// 测试类
public class Demo {
        public static void main(String[] arga) {
                ProcessArray pa = new ProcessArray();
                int[] arr = { 1, 3, 5, 7 };
                pa.arrayWork(arr, new PrintCommand());
                pa.arrayWork(arr, new AddCommand());
        }
}




13 个回复

倒序浏览
虽然很乱,看不清楚,但是还是赞一个
回复 使用道具 举报
qinhaihang 发表于 2015-5-21 16:08
虽然很乱,看不清楚,但是还是赞一个

谢谢啊,你也在努力攒分吧
回复 使用道具 举报
不错不错,学习了
回复 使用道具 举报
kingowe 发表于 2015-5-21 21:24
谢谢啊,你也在努力攒分吧

是啊,还在苦逼的看视频学习中,你报哪一期啊?
回复 使用道具 举报
qinhaihang 发表于 2015-5-24 12:31
是啊,还在苦逼的看视频学习中,你报哪一期啊?

我估计到8月份左右了,现在还在上学呢, 要攒分给你推荐个好方法:在入学技术交流里边发帖,主题分类就选入学交流,每天发几次,一次就能拿2个黑马币,你再两天就够了
回复 使用道具 举报
吼吼学习一下 谢谢分享
回复 使用道具 举报
kingowe 发表于 2015-5-24 12:39
我估计到8月份左右了,现在还在上学呢, 要攒分给你推荐个好方法:在入学技术交流里边发帖,主题分类就选 ...

我差不多也这样弄过,原来你还在上学,哈哈,我都毕业一年了
回复 使用道具 举报
不错不错,学习了
回复 使用道具 举报
不错学习了。

回复 使用道具 举报
学习了一下
回复 使用道具 举报
收藏了  
回复 使用道具 举报

恩、收藏了
回复 使用道具 举报
不错 学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马