当前位置:网站首页>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 essence of interview
- Jenkins
- [untitled] 8 simplified address book
- 万卷书 - 价值投资者指南 [The Education of a Value Investor]
- instanceof
- Create your own deep learning environment with CONDA
- Tool class static method calls @autowired injected service
- php artisan
- 利用C#实现Pdf转图片
- Software testing assignment - the next day
猜你喜欢

每日刷題記錄 (十一)

Personally design a highly concurrent seckill system

Realize PDF to picture conversion with C #

每日刷题记录 (十一)

Inno Setup 制作安装包
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

In depth analysis of reentrantlock fair lock and unfair lock source code implementation
![[untitled]](/img/72/4ff9354634342580a135debb58b60e.jpg)
[untitled]
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

多个全局异常处理类,怎么规定执行顺序
随机推荐
UTC time, GMT time, CST time
Setting up the development environment of dataworks custom function
Stream stream
php安装composer
Yolov2 learning and summary
Upgrade CentOS php7.2.24 to php7.3
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
Climb movie paradise 2021 hot
Create your own deep learning environment with CONDA
Resttemplate configuration use
Inno Setup 制作安装包
php安装swoole扩展
Software testing learning - day one
VMware virtual machine C disk expansion
Daily question brushing record (11)
卡特兰数(Catalan)的应用场景
Laravel Web Framework
The essence of interview
【code】偶尔取值、判空、查表、验证等
PHP install the spool extension