码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式——策略模式

时间:2017-02-27 20:40:51      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:tin   技术分享   ccf   参数   print   抽象   pac   src   pack   

策略模式的重点并不是策略的实现,而是策略的管理的使用!

应用场景:当同一种职责,有不同的实现方式的时候可以使用策略管理;

角色:1、策略抽象类   2、策略的具体实现    3、策略环境(此类的作用在代码中有详细说明)

策略模式是典型的依赖接口编程的实现!

策略模式的优点是将算法的责任和实现分割开来,便于系统扩展;

缺点是客户端需要知道各个算法的优缺点,由客户端决定调用哪种算法;

下面看具体实现:

1、责任抽象

技术分享
/*
 * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
 */

package com.pt.strategy;

import java.util.List;

/**
 * @description 职责的抽象
 * @author panteng
 * @date 17-2-27.
 */
public interface ISorting {
    void sort(List<Integer> list);
}
ISorting

2、策略具体实现

技术分享
/*
 * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
 */

package com.pt.strategy;

import java.util.Collections;
import java.util.List;

/**
 * @description 职责实现策略的实体类
 * @author panteng
 * @date 17-2-27.
 */
public class SortStrategy1 implements ISorting {
    public void sort(List<Integer> list){
        System.out.println("===========方式1排序==========");
        Collections.sort(list);
    }
}
SortStrategy1
技术分享
/*
 * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
 */

package com.pt.strategy;

import java.util.List;

/**
 * @description 职责实现策略的实体类
 * @author panteng
 * @date 17-2-27.
 */
public class SortStrategy2 implements ISorting {
    //插入排序
    public void sort(List<Integer> list){
        System.out.println("===========方式2排序==========");
        int i = 1;
        for (i = 1; i < list.size(); i++) {
            int key = list.get(i);
            int j = i;
            while (j > 0 && key < list.get(j - 1)) {
                list.set(j, list.get(j - 1));
                j--;
            }
            list.set(j, key);
        }
    }
}
SortStrategy2

3、策略环境(上下文)

此类除了代码中提到的必要之处,另外的作用在于当其中的某一种算法的入参和其他的不一致时,可以通过在context中增加成员变量,将该成员变量作为算法必须的参数使用,这样修改起来非常容易。比起使用重载或者是重写算法好很多。

 

或者是统计各种算法调用次数,此类也是必要的。

技术分享
/*
 * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
 */

package com.pt.strategy;

import java.util.List;

/**
 * @description 策略的上下文环境,之所以由此上下文环境是为了便于做一些共性处理,比如对参数的校验,执行时间统计等;假如没有此上下文,需要在每一个策略类里面校验
 * @author panteng
 * @date 17-2-27.
 */
public class SortContext {
    ISorting sorting;
    public SortContext(){
    }
    public SortContext(ISorting sorting){
        this.sorting = sorting;
    }
    public ISorting getSorting(){
        return sorting;
    }
    public void setSorting(ISorting sorting){
        this.sorting = sorting;
    }

    public void Sort(List<Integer> list){
        sorting.sort(list);
    }
}
SortContext

4、测试

技术分享
/*
 * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
 */

package com.pt.strategy;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @author panteng
 * @date 17-2-27.
 */
public class StrategyTest {
    @Test
    public void strategyTest(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(23);
        list.add(4);
        list.add(14);
        list.add(2);
        list.add(8);
        list.add(21);
        list.add(56);
        list.add(14);

        ISorting strategy1 = new SortStrategy1();
        ISorting strategy2 = new SortStrategy2();

        SortContext sortContext = new SortContext(strategy1);
        sortContext.Sort(list);
        System.out.println(list);

        /*SortContext sortContext = new SortContext();
        sortContext.setSorting(strategy2);
        sortContext.Sort(list);
        System.out.println(list);*/

    }
}
StrategyTest

 

设计模式——策略模式

标签:tin   技术分享   ccf   参数   print   抽象   pac   src   pack   

原文地址:http://www.cnblogs.com/tengpan-cn/p/6475934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!