策略模式(Strategy Pattern)
策略模式(Strategy Pattern)中体现了 两个非常基本的面向对象设计的原
封装变化的概念
编程中使用接口,而不是对接口的实现
策略模式的定义
==定义一组算法,将每个算法都封装起来,并且 使它们之间可以互换。
– 策略模式使这些算法在客户端调用它们的时候 能够互不影响地变化 ==
策略模式的意义
– 策略模式使开发人员能够开发出由许多可替换 的部分组成的软件,并且各个部分之间是弱连 接的关系。
– 弱连接的特性使软件具有更强的可扩展性,易 于维护;更重要的是,它大大提高了软件的可 重用性
策略模式的组成
抽象策略角色:策略类,通常由一个接口或者 抽象类实现
– **具体策略角色:**包装了相关的算法和行为
– 环境角色:持有一个策略类的引用,最终给客 户端调用的
举个栗子
要求:假如有若干个类 Person 对象存在一个 List 当中,对他们进行排序,分别按照名字、 年龄、id 进行排序(要有正序与倒序两种排序方式)。假如年龄或者姓名重复,按照 id 的正序进行排序。要求使用策略模式进行
Person.java
public class Person
{
private int id;
private String name;
private int age;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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;
}
}
Environment.java
import java.util.List;
public class Environment
{
private SortInterface sortInterface;
public Environment(SortInterface sortInterface)
{
this.sortInterface = sortInterface;
}
public Environment()
{
}
public void setSortInterface(SortInterface sortInterface)
{
this.sortInterface = sortInterface;
}
public void sort(List<Person> list)
{
this.sortInterface.sort(list);
}
}
SortInterface.java
import java.util.List;
public interface SortInterface
{
public void sort(List<Person> list);
}
UpNameSort.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class UpNameSort implements SortInterface, Comparator<Person>
{
public void sort(List<Person> list)
{
Collections.sort(list, this);
}
public int compare(Person o1, Person o2)
{
int result = o1.getName().compareTo(o2.getName());
if(0 == result)
{
return o1.getId() - o2.getId();
}
return result;
}
}
Client.java 主函数在这里
import java.util.ArrayList;
import java.util.List;
public class Client
{
public static void main(String[] args)
{
Person p1 = new Person();
p1.setName("Tom");
p1.setId(1);
p1.setAge(20);
Person p2 = new Person();
p2.setName("Tonny");
p2.setId(2);
p2.setAge(50);
Person p3 = new Person();
p3.setName("Tom");
p3.setId(5);
p3.setAge(30);
Person p4 = new Person();
p4.setName("ABC");
p4.setId(8);
p4.setAge(10);
Person p5 = new Person();
p5.setName("Xyz");
p5.setId(9);
p5.setAge(15);
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
Environment env = new Environment();
UpNameSort uns = new UpNameSort();
env.setSortInterface(uns);
env.sort(list);
for (int i = 0; i < list.size(); i++)
{
Person p = list.get(i);
System.out.println("id: " + p.getId() + ", name: " + p.getName()
+ ", age:" + p.getAge());
}
System.out.println("--------------");
DownNameSort dns = new DownNameSort();
env.setSortInterface(dns);
env.sort(list);
for (int i = 0; i < list.size(); i++)
{
Person p = list.get(i);
System.out.println("id: " + p.getId() + ", name: " + p.getName()
+ ", age:" + p.getAge());
}
}
}
DownNameSort.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DownNameSort implements SortInterface, Comparator<Person>
{
public void sort(List<Person> list)
{
Collections.sort(list, this);
}
public int compare(Person o1, Person o2)
{
int result = o2.getName().compareTo(o1.getName());
if(0 == result)
{
return o1.getId() - o2.getId();
}
return result;
}
}