当前位置:网站首页>Strategy mode
Strategy mode
2022-07-03 06:59:00 【W_ Meng_ H】
Java23 Learning notes of design patterns :https://www.cnblogs.com/meet/p/5116504.html
according to java Design principles , We should close the modification , Open to expansion . So we need to try not to modify the source code .
For the top Cat class , We want to follow weight Sort , If we want to follow height Sort it out ?
So we define our own Comparable, Use generics to control the type of incoming parameters .
public interface Comparator<T> {
int compare(T o1,T o2);
}public class Sorter<T> {
public void sort(T[] arr,Comparator<T> comparator) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos =comparator.compare(arr[j],arr[minPos]) == -1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
void swap(T[] arr, int i, int j) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}public class Cat {
int weight;
int height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}public class CatComparator implements Comparator<Cat>{
@Override
public int compare(Cat o1, Cat o2) {
if(o1.weight<o2.weight){
return -1;
}else if(o1.weight>o2.weight){
return 1;
}else {
return 0;
}
}
}import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Cat[] cats={new Cat(3,3),new Cat(1,1),new Cat(5,5)};
Sorter<Cat> sorter=new Sorter<Cat>();
sorter.sort(cats, new CatComparator());
System.out.println(Arrays.toString(cats));
}
}You can see , We are main Function :
sorter.sort(cats, new CatComparator());new CatComparator() Into Cat Class comparison strategy , according to weight Sort . If you want to modify the sorting strategy , Just change the policy class .
This is the strategy model .
边栏推荐
- The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
- Thoughts in Starbucks
- New knowledge! The virtual machine network card causes your DNS resolution to slow down
- 服务器如何设置多界面和装IIS呢?甜甜给你解答!
- 10000小时定律不会让你成为编程大师,但至少是个好的起点
- MySQL transaction rollback, error points record
- 如何迁移或复制VMware虚拟机系统
- SQL implementation merges multiple rows of records into one row
- [Code] occasionally take values, judge blanks, look up tables, verify, etc
- Shim and Polyfill in [concept collection]
猜你喜欢

JMeter test result output
![[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle](/img/f8/0e3fbfd13bf06291a73200552ff17a.png)
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle

Notes on the core knowledge of Domain Driven Design DDD

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners

Software testing learning - the next day

The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list

Practice of enterprise ab/testing platform

10000小时定律不会让你成为编程大师,但至少是个好的起点

Pits encountered in the use of El checkbox group

Inno Setup 制作安装包
随机推荐
How to migrate or replicate VMware virtual machine systems
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
golang操作redis:写入、读取kv数据
New knowledge! The virtual machine network card causes your DNS resolution to slow down
Practical plug-ins in idea
On the practice of performance optimization and stability guarantee
Basic teaching of crawler code
EasyExcel
vmware虚拟机C盘扩容
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Software testing assignment - day 1
MySQL mistakenly deleted the root account and failed to log in
[classes and objects] explain classes and objects in simple terms
php artisan
Resttemplate configuration use
[LeetCode]404. 左叶子之和
Win 10 find the port and close the port
Software testing assignment - day 3