使用策略模式让集合自定义排序

阅读更多

 

package com.zj.exercise2;

import java.util.List;

/**
 * 功能:抽象策略角色
 * @author zhengjiong
 * time:2011-9-16 00:02:12
 */
public interface Strategy
{
	public void sort(List list);
	
}

 

 

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

/**
 * 功能:定义一个具体的策略角色(Strategy),也是一个比较器(Comparator),按username的升序排列
 * @author zhengjiong
 * time:2011-9-16 上午12:02:56
 */
public class StrategyByUsername	implements Strategy, Comparator
{

	@Override
	public void sort(List list)
	{
		Collections.sort(list, this);
	}

	/**
	 * 按照username的升序排列,如果username一样则按照id的升序排列
	 */
	@Override
	public int compare(Object o1, Object o2)
	{
		Person p1 = (Person)o1;
		Person p2 = (Person)o2;
		
		//如果姓名一样则按照id升序排列
		if(p1.getUsername().equals(p2.getUsername()))
		{
			return p1.getId() - p2.getId();
		}
		//按照username升序排列
		return p1.getUsername().compareTo(p2.getUsername());
	}
}

 

import java.util.List;

/**
 * 功能:策略模式的环境角色
 * @author zhengjiong
 * time:2011-9-16 上午01:08:40
 */
public class Environment
{
	private Strategy strategy;
	
	public Environment(Strategy strategy)
	{
		this.strategy = strategy;
	}
	
	public void sort(List list)
	{
		strategy.sort(list);
	}
	
	public void setStrategy(Strategy strategy)
	{
		this.strategy = strategy;
	}

	public Strategy getStrategy()
	{
		return strategy;
	}
}

 package com.zj.exercise2;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能:使用策略模式让集合排序
 * @author zhengjiong
 * time:2011-9-16 上午01:10:49
 */
public class Client
{
	public static void main(String[] args)
	{	
		Person p1 = new Person(1, "e", 16);
		Person p2 = new Person(5, "a", 15);
		Person p3 = new Person(4, "c", 20);
		Person p4 = new Person(2, "b", 23);
		Person p5 = new Person(3, "f", 18);
		Person p6 = new Person(6, "a", 18);
		Person p7 = new Person(8, "y", 20);
		Person p8 = new Person(7, "x", 16);
		Person p9 = new Person(9, "b", 25);
		Person p10 = new Person(10, "h", 19);
		
		
		List list = new LinkedList();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		list.add(p4);
		list.add(p5);
		list.add(p6);
		list.add(p7);
		list.add(p8);
		list.add(p9);
		list.add(p10);
		
		for(Iterator i = list.iterator(); i.hasNext();)
		{
			Person person = (Person)i.next();
			System.out.println(person);
		}
		System.out.println();
		System.out.println("------------id升序------------");
		
		//实例化具体的策略模式
		StrategyById sbi = new StrategyById();
		
		//实例化环境角色,使用id升序排列
		Environment env = new Environment(sbi);
		
		env.sort(list);
		
		for(Iterator i = list.iterator(); i.hasNext();)
		{
			Person person = (Person)i.next();
			System.out.println(person);
		}
		
		System.out.println();
		System.out.println("------------id降序------------");
		//实例化具体的策略模式
		StrategyByIdDESC sbidesc = new StrategyByIdDESC();
		//设置为降序模式
		env.setStrategy(sbidesc);
		env.sort(list);
		for(Iterator i = list.iterator(); i.hasNext();)
		{
			Person person = (Person)i.next();
			System.out.println(person);
		}
		
		System.out.println();
		System.out.println("------------username升序,如果名字一样按照id升序排列------------");
		//实例化具体的策略模式
		StrategyByUsername sbu = new StrategyByUsername();
		//设置为降序模式
		env.setStrategy(sbu);
		env.sort(list);
		for(Iterator i = list.iterator(); i.hasNext();)
		{
			Person person = (Person)i.next();
			System.out.println(person);
		}
		
		System.out.println();
		System.out.println("------------username降序,如果名字一样按照id升序排列------------");
		//实例化具体的策略模式
		StratigyByUsernameDESC sbdesc = new StratigyByUsernameDESC();
		//设置为降序模式
		env.setStrategy(sbdesc);
		env.sort(list);
		for(Iterator i = list.iterator(); i.hasNext();)
		{
			Person person = (Person)i.next();
			System.out.println(person);
		}
		
	}
}
 

你可能感兴趣的:(list,排序,策略模式)