当前位置:网站首页>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 .
边栏推荐
- 机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
- mongodb
- 【code】if (list != null && list.size() > 0)优化,集合判空实现方式
- php安装swoole扩展
- Personally design a highly concurrent seckill system
- dataworks自定義函數開發環境搭建
- Abstract learning
- MySQL transaction rollback, error points record
- 100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
- Shim and Polyfill in [concept collection]
猜你喜欢
Software testing assignment - the next day
Jenkins
Daily question brushing record (11)
dataworks自定义函数开发环境搭建
Summary of remote connection of MySQL
[classes and objects] explain classes and objects in simple terms
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
Use the jvisualvm tool ----- tocmat to start JMX monitoring
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
JMeter JSON extractor extracts two parameters at the same time
随机推荐
每日刷題記錄 (十一)
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
How to plan well?
Arctic code vault contributor
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
Realize PDF to picture conversion with C #
修改MySQL密码
机械观和系统观的科学思维方式各有什么特点和作用
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
Basic teaching of crawler code
Summary of UI module design and practical application of agent mode
File links cannot be opened or downloaded in Google browser
My 2020 summary "don't love the past, indulge in moving forward"
How to specify the execution order for multiple global exception handling classes
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
Upgrade CentOS php7.2.24 to php7.3
Heap sort and priority queue
Simple understanding of bubble sorting