Strategy策略模式在jdk中的应用

目前自己知道的策略模式在jdk中的应用有比较器Comparator和布局管理器LayoutManager

在Java的集合框架中,经常需要通过构造方法传入一个比较器Comparator,或者创建比较器传入Collections的静态方法中作为方法参数,进行比较排序等,使用的是策略模式。

在该比较架构中,Comparator就是一个抽象的策略;一个类实现该结构,并实现里面的compare方法,该类成为具体策略类;Collections类就是环境角色,他将集合的比较封装成静态方法对外提供api。

class Employee implements Comparable<Employee> {
    //fields
    private int empId;
    private String name;
    private int age;
    Employee(int empId, String name, int age) {
        this.empId = empId;
        this.name = name;
        this.age = age;
    }
    //Employee类实现Comparable接口,进行排序。实现compareTo方法
    public int compareTo(Employee o) {
        return this.empId - o.empId;
    }
    //seter and getter
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}
class EmpSortByName implements Comparator<Employee> {
    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
class EmpSortById implements Comparator<Employee> {
    public int compare(Employee o1, Employee o2) {
        return o1.getEmpId()-o2.getEmpId();
    }
}
public class TestEmployeeSort {
    public static void main(String[] args) {
        List<Employee> col = new ArrayList<Employee>();
        col.add(new Employee(5, "Frank", 28));
        col.add(new Employee(1, "Jorge", 19));
        col.add(new Employee(6, "Bill", 34));
        col.add(new Employee(3, "Michel", 10));
        col.add(new Employee(7, "Simpson", 8));
        col.add(new Employee(4, "Clerk", 16));
        col.add(new Employee(8, "Lee", 40));
        col.add(new Employee(2, "Mark", 30));     
        System.out.println("Sorting by empId field : sort method by implement comparable interface");
        // sort method by implementing comparable interface
        Collections.sort(col); 
        printList(col);
        System.out.println("Sorting by empId fieName : sort method implement comparator interface");
        Collections.sort(col, new EmpSortByName());
        //sort method by implementing comparator interface
        printList(col);
        System.out.println("Sorting by empId field : sort method implement comparator interface");
        //sort method by implementing comparator interface
        Collections.sort(col, new EmpSortById());
        printList(col);
    }
    private static void printList(List<Employee> list) {
        System.out.println("EmpId\tName\tAge");
        for (Employee e : list) {
            System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
        }
    }
}


在布局管理器LayoutManager的中的图如下:


       在Java SE开发中,用户需要对容器对象Container中的成员对象如按钮、文本框等GUI控件进行布局(Layout),在程序运行期间由客户端动态决定一个Container对象如何布局,Java语言在JDK中提供了几种不同的布局方式,封装在不同的类中,如BorderLayout、FlowLayout、GridLayout、GridBagLayout和CardLayout等。在图24-3中,Container类充当环境角色Context,而LayoutManager作为所有布局类的公共父类扮演了抽象策略角色,它给出所有具体布局类所需的接口,而具体策略类是LayoutManager的子类,也就是各种具体的布局类,它们封装了不同的布局方式。


体现了两个非常基本的面向对象设计原则:

1.封装变化的概念

2.编程中使用接口,而不是对接口的实现

 

策略模式的意义

 1.策略模式使开发人员能够开发出由许多可替换的部分组成的软件,并且各个部分之间是弱连接的关系。

 2.弱连接的特性使软件具有更强的可扩展性,易于维护;更重要的是,它大大提高了软件的可重用性。

 

策略模式的组成

抽象策略角色:策略类,通常由一个接口或者抽象类实现。(比如Comparator接口)

具体策略角色:包装了相关的算法和行为。(比如:我们要实现排序的实体类,要实现comparator的compare方法和equals方法,这个比较策略是由我们决定的,而且可以灵活指定,不同的比较指标就是不同的策略,而不像compareble接口那样只能指定一个写死)

环境角色:持有一个策略类的引用,最终给客户端用的。(比如java.util.Collections调用sort方法,在调用的时候传入具体实现的比较算法,即策略,java.util.Collections.sort(List,Comparator))

 

策略模式的实现

  策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以互相替换。

  策略模式使得算法可以在不影响到客户端的情况下发生变化。使用策略模式可以把行为和环境分割开来。

  环境类负责维持和查询行为类,各种算法则在具体策略中提供。由于算法和环境独立开来,算法的修改不会影响环境和客户端。

 

策略模式的缺点

  1.客户端必须知道所有的策略类,并自行决定使用哪一个策略类。

  2.造成很多的策略类。

  解决方案:采用工厂方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值