使用state模式构建comparator,并使用factory模式创建comparator

来源:互联网 发布:ubuntu安装图形化界面 编辑:程序博客网 时间:2024/06/06 20:40

今天在写一个插入排序的算法.

写完之后,利用代码重构的思想重构代码,就迁出了state模式和factory模式.

顺便把这个过程记录下来.以便之后学习用...

一:插入排序一个person数组

//Person类, 这里使用jdk的comparable进行排序功能public class Person implements Comparable<Person> {    private String name;    public Person(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public int compareTo(Person o) {        String selfName =  this.getName();        String comparaName = o.getName();        if(this == o || selfName.equals(comparaName)){            return 0;        } else if (selfName.getBytes().length > comparaName.getBytes().length) {            return 1;        } else {            return -1;        }    }}//排序类public class InsertSorted {    public static void main(String[] args) {        Person[] persons = new Person[] {                new Person("22"),                new Person("333"),                new Person("1")        };                System.out.println("排序前的数组: ");        InsertSorted.printArr(persons);                InsertSorted.insertSort(persons);                System.out.println("排序后的数组: ");        InsertSorted.printArr(persons);    }        /**     *  插入排序一个数组     * @param persons     */    public static void insertSort(final Person[] persons) {                for(int i = 1; i < persons.length; i++){            for(int j = i; j > 0; j--){                if(persons[j].compareTo(persons[j-1]) < 0){                    InsertSorted.swap(persons,j,j-1);                }else{                    break;                }            }        }    }        /**     * 交换数组 i j 的元素     * @param persons     * @param i     * @param j     */    public static void swap(final Person[] persons,final int i, final int j) {                Person transport = persons[i];        persons[i] = persons[j];        persons[j] = transport;    }    /**     * 打印数组中的对象,调用toString方法     *      * @param persons     */    public static void printArr(final Person[] persons) {        for (Person person : persons) {            System.out.print(person.getName() + ", ");        }    }
二: 如果这里的排序规则要变化,可能增加一个排序标准,可能增加很多标准...在可能进行扩展的地方使用 设计模式

对于多个比较器---->明显的state模式.

并且我还想监控他们的创建过程--->factory模式.

//person类public class Person {private String name;public Person(String name) {super();this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}//person比较器接口public interface PersonComparator extends Comparator<Person> {    }//PersonComparato其中之一的实现类型public class BytePersonComparator implements PersonComparator {    @Override    public int compare(Person o1, Person o2) {        String selfName =  o1.getName();        String comparaName = o2.getName();        if(o1 == o2 || selfName.equals(comparaName)){            return 0;        } else if(selfName.getBytes().length > comparaName.getBytes().length){//名字的字节长度比较            return 1;        } else{            return -1;        }    }}//抽象工厂接口public interface CompratorFactory {    public PersonComparator createPersonComparator();}// byte抽象工厂实现类型public class ByteCompratorFactory implements CompratorFactory {        private static BytePersonComparator personByteComparator ;            @Override    public synchronized PersonComparator createPersonComparator() {                if(personByteComparator == null){            personByteComparator = new BytePersonComparator();        }        return personByteComparator;    }} //排序类型public class InsertSorted {    public static void main(String[] args) {        Person[] persons = new Person[] {                new Person("22"),                new Person("333"),                new Person("1")        };                System.out.println("排序前的数组: ");        InsertSorted.printArr(persons);                InsertSorted.insertSort(persons, new ByteCompratorFactory().createPersonComparator());        System.out.println("排序后的数组: ");        InsertSorted.printArr(persons);    }        /**     *  插入排序一个数组, 这个时候需要传递一个 comparator对象, 来决定使用哪个比较器     * @param persons     */    public static void insertSort(final Person[] persons, Comparator<Person> comparator) {                for(int i = 1; i < persons.length; i++){            for(int j = i; j > 0; j--){                if(comparator.compare(persons[j], persons[j-1]) < 0){                    InsertSorted.swap(persons,j,j-1);                }else{                    break;                }            }        }    }        /**     * 交换数组 i j 的元素     * @param persons     * @param i     * @param j     */    public static void swap(final Person[] persons,final int i, final int j) {                Person transport = persons[i];        persons[i] = persons[j];        persons[j] = transport;    }    /**     * 打印数组中的对象,调用toString方法     *      * @param persons     */    public static void printArr(final Person[] persons) {        for (Person person : persons) {            System.out.print(person.getName() + ", ");        }    }}
类图结构:


调用序列图:



0 0
原创粉丝点击