日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]策略模式的定義:定義一系列的算法類,將每一個(gè)算法封裝起來(lái),并讓他們可以互相替換。策略模式讓算法獨(dú)立于使用它的客戶而變化。下面是策略模式的結(jié)構(gòu)圖:其實(shí),策略模式離我們很近,接下來(lái)看兩個(gè)JDK中策略模式的

策略模式的定義:

定義一系列的算法類,將每一個(gè)算法封裝起來(lái),并讓他們可以互相替換。策略模式讓算法獨(dú)立于使用它的客戶而變化。

下面是策略模式的結(jié)構(gòu)圖:


其實(shí),策略模式離我們很近,接下來(lái)看兩個(gè)JDK中策略模式的例子。

Collections.sort()

在Collections類中,有個(gè)sort(List,Comparator)靜態(tài)方法用于對(duì)給定的數(shù)組排序,至于兩個(gè)對(duì)象怎么比較,java開(kāi)發(fā)人員上哪知道去,所以就是使用這個(gè)方法的人自已定義的了。這不就是策略模式嗎。下面從源代碼中分析策略模式的實(shí)現(xiàn)。

在Collections類中:


public?staticvoid?sort(Listlist,?Comparator?c)?{
??Object[]?a?=?list.toArray();
??Arrays.sort(a,?(Comparator)c);
??ListIterator?i?=?list.listIterator();
??for?(int?j=0;?j<a.length;?j++)?{
??????i.next();
??????i.set(a[j]);
??}
}


調(diào)用了Arrays類的sort()方法。ok,Arrays類走起。


public?staticvoid?sort(T[]?a,?Comparator?c)?{
??if?(LegacyMergeSort.userRequested)
??????legacyMergeSort(a,?c);
??else
??????TimSort.sort(a,?c);
}


經(jīng)過(guò)層層的調(diào)用,最終調(diào)用Arrays類的下面方法:


private?static?void?mergeSort(Object[]?src,?Object[]?dest,?int?low,?int?high,?int?off,?Comparator?c)?{
	int?length?=?high?-?low;
	
	//?Insertion?sort?on?smallest?arrays
	if?(length?<?INSERTIONSORT_THRESHOLD)?{
		for?(int?i=low;?ilow?&&?c.compare(dest[j-1],?dest[j])>0;?j--)
					swap(dest,?j,?j-1);
		return;
	}
	
	//?Recursively?sort?halves?of?dest?into?src
	int?destLow??=?low;
	int?destHigh?=?high;
	low??+=?off;
	high?+=?off;
	int?mid?=?(low?+?high)?>>>?1;
	mergeSort(dest,?src,?low,?mid,?-off,?c);
	mergeSort(dest,?src,?mid,?high,?-off,?c);
	
	//?If?list?is?already?sorted,?just?copy?from?src?to?dest.??This?is?an
	//?optimization?that?results?in?faster?sorts?for?nearly?ordered?lists.
	if?(c.compare(src[mid-1],?src[mid])?<=?0)?{
		System.arraycopy(src,?low,?dest,?destLow,?length);
		return;
	}
	
	//?Merge?sorted?halves?(now?in?src)?into?dest
	for(int?i?=?destLow,?p?=?low,?q?=?mid;?i?<?destHigh;?i++)?{		if?(q?>=?high?||?p?<?mid?&&?c.compare(src[p],?src[q])?<=?0)
			dest[i]?=?src[p++];
		else
			dest[i]?=?src[q++];
	}
}


可以看到確實(shí)是調(diào)用了Comparator接口定義的方法。這里的Comparator就是策略類了,由用戶自定義的算法,可以替換。下面看看使用這個(gè)方法的用戶怎么使用這個(gè)神奇的方法:


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

public?class?Test{
	
	public?static?void?main(String[]?args){
		Liststudents?=?new?ArrayList();
		students.add(new?Student(12,"zhangsan"));
		students.add(new?Student(13,"lisi"));
		students.add(new?Student(11,"wangwu"));
		Collections.sort(students,new?StudentComparator());
		for(Student?s:students){
			System.out.println(s);
		}
	}
	
	private?static?class?Student?{
		private?int?age;
		private?String?name;
		public?Student(int?age,String?name){
			this.age?=?age;
			this.name?=?name;
		}
		public?String?toString(){
			return?age+","+name;
		}
		public?int?getAge()?{
			return?age;
		}
	}
	static?class?StudentComparator?implements?Comparator{

		@Override
		public?int?compare(Student?o1,?Student?o2)?{
			//?TODO?Auto-generated?method?stub
			return?o1.getAge()-o2.getAge();
		}
		
	}


按學(xué)生的年齡給一個(gè)學(xué)生數(shù)組排序。哪天不想按年齡排序了,想按姓名排序,重新寫一個(gè)Comparator的實(shí)現(xiàn)類并在客戶端替換即可。從這里也可以看出這個(gè)設(shè)計(jì)模式是違反了開(kāi)閉原則的。

File類的list()

list(FilenameFilter)方法用于列出目錄下所有符合條件的文件。也是策略模式了,條件由用戶自己定義。只要傳入正則表達(dá)式即可。關(guān)于正則表達(dá)式,可以參看這里


public?class?FileTest?{
	
	public?void?list(String?path){
		File?f?=?new?File(path);
		String[]?files?=?f.list(new?FilenameFilterImpl(""));
		for(String?s:files)
			System.out.println(s);
	}

	public?static?void?main(String[]?args)?{
		//?TODO?Auto-generated?method?stub
		new?FileTest().list(".");
	}

}
class?FilenameFilterImpl?implements?FilenameFilter{	
	private?Pattern?p;	
	public?FilenameFilterImpl(String?regex){
		p?=?Pattern.compile(regex);
	}

	@Override
	public?boolean?accept(File?dir,?String?name)?{
		//?TODO?Auto-generated?method?stub
		if(p.matcher(name).matches())
			return?true;
		return?false;
	}	
}


ok,策略模式還是很常見(jiàn)的設(shè)計(jì)模式,通過(guò)兩個(gè)例子也可以看到其靈活性,就這樣吧。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉