设计模式——策略模式(Strategy) 笔记
来源:互联网 发布:cocos2dx安装 mac 编辑:程序博客网 时间:2024/06/05 20:12
引入:::
排序方法的种类: {冒择路(入)兮(希尔)快归堆}
package com.linux_lihuafeng.strategy;
public class Guest {
public static void main(String[] args) {
//站在使用者角度編程 ,, 會使考慮接口更加簡單
int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();
dataSorter.sort(a);
dataSorter.print(a);
}
}
package com.linux_lihuafeng.strategy;
public class DataSorter {
public void sort(int a[]) {
for(int i = 0 ; i < a.length -1 ; i++ ) {
for(int j = 0 ; j < a.length - i -1 ;j ++){
exchange( a, j , j+1);
}
}
}
public void exchange(int[] a , int i, int j) {
if(a[i] > a[j] ) {
int temp ;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
public void print(int a[]) {
for(int i = 0 ; i < a.length ; i++) {
System.out.print(" a["+ i+ "] = "+ a[i] );
}
}
}
public class DataSorter {
public void sort(Cat a[]) {
for(int i = 0 ; i < a.length-1 ; i++ ) {
for(int j = 0 ; j < a.length-i-1 ;j++){
if(a[j].getHeight() > a[j+1].getHeight() )
exchange( a, j , j+1);
}
}
}
public void exchange(Cat[] a , int i, int j) {
Cat temp ;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public void print(Cat a[]) {
for(int i = 0 ; i < a.length ; i++) {
System.out.print(" a["+ i+ "] = "+ a[i].getHeight() );
}
}
}
public class Cat {
public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
private int height;
private int weight;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
public class Guest {
public static void main(String[] args) {
//站在使用者角度編程 ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();
Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
dataSorter.sort(a);
dataSorter.print(a);
}
}
public interface Camparable {
public int campareTo(Object o) ;
}
public class Cat implements Camparable{
public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
private int height;
private int weight;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String toString () {
return "height = "+this.height;
}
public int campareTo(Object o) {
if (o instanceof Cat) {
Cat c = (Cat)o;
if(this.height > c.getHeight()){
return 1;
}else if(this.height == c.getHeight()){
return 0;
}else{
return -1;
}
}
try{
//这个地方写的不太对
return -1000;
}catch(Exception e){
System.out.println("the object is not a Cat");
}
return -100;
}
}
public class DataSorter {
public void sort(Object a[]) {
for(int i = 0 ; i < a.length -1 ; i++ ) {
for(int j = 0 ; j < a.length - i -1 ;j ++){
//接口的显示实现,需要转换成接口才能调用这些方法
Camparable o1 = (Camparable)a[j];
Camparable o2 = (Camparable)a[j+1];
if(o1.campareTo(o2) > 0 )
exchange( a, j , j+1);
// if(a[i].getHeight() > a[j].getHeight() )
// exchange( a, j , j+1);
}
}
}
public void exchange(Object[] a , int i, int j) {
Object temp ;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public void print(Object a[]) {
for(int i = 0 ; i < a.length ; i++) {
System.out.print(" a["+ i+ "] : "+ a[i].toString() );
}
}
}
public class Guest {
public static void main(String[] args) {
//站在使用者角度編程 ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();
Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
dataSorter.sort(a);
dataSorter.print(a);
}
}
public class Dog implements Camparable{
//定义狗的食量
private int capacity;
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public Dog(int capacity) {
super();
this.capacity = capacity;
}
public String toString () {
return "capacity = " + capacity;
}
@Override
public int campareTo(Object o) {
if(o instanceof Dog){
Dog d = (Dog)o;
if(this.capacity > d.getCapacity()){
return 1;
}else if (this.capacity == d.getCapacity()){
return 0;
}else {
return -1;
}
}
return -100;
}
}
public class Guest {
public static void main(String[] args) {
//站在使用者角度編程 ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();
Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
Dog[] d = {new Dog(7), new Dog(8), new Dog(1), new Dog(9)};
dataSorter.sort(d);
dataSorter.print(d);
}
}
public interface Comparator {
//interface 中的方法均默认为public
int compare(Object o1, Object o2);
}
public class CatHeightComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getHeight() > c2.getHeight()){
return 1;
}else if(c1.getHeight() < c2.getHeight()){
return -1;
}else {
return 0;
}
}
}
public class CatWeightComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getWeight() > c2.getWeight()){
return 1;
}else if(c1.getWeight() < c2.getWeight()){
return -1;
}else {
return 0;
}
}
}
public class Cat implements Camparable{
private int height;
private int weight;
private Comparator comparator = new CatHeightComparator();
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String toString () {
return "height = "+this.height;
}
public int campareTo(Object o) {
// if (o instanceof Cat) {
// Cat c = (Cat)o;
// if(this.height > c.getHeight()){
// return 1;
// }else if(this.height == c.getHeight()){
// return 0;
// }else{
// return -1;
// }
// }
// try{
// //这个地方写的不太对
// return -1000;
// }catch(Exception e){
// System.out.println("the object is not a Cat");
// }
// return -100;
return comparator.compare(this, o);
}
}
- 设计模式——策略模式(Strategy) 笔记
- 设计模式学习笔记——策略(Strategy)模式
- 设计模式学习笔记(三)—-Strategy策略模式
- 设计模式学习笔记(三)—Strategy策略模式
- 设计模式——策略模式(Strategy)
- 设计模式——Strategy策略模式
- JAVA设计模式—策略模式(Strategy)
- java设计模式——策略者模式(strategy)
- 设计模式——策略模式(Strategy)
- java设计模式13——策略模式(Strategy)
- 走进设计模式之——策略模式(Strategy)
- 【设计模式】——策略模式(Strategy)
- C++ 设计模式 —— 策略模式(Strategy)
- 设计模式 —— 策略模式(Strategy Pattern)
- 设计模式 II ——策略模式(Strategy)
- Java设计模式——策略模式(Strategy Pattern)
- 设计模式——策略模式(Strategy Pattern)
- Java设计模式——策略模式(Strategy Pattern)
- 【算法导论】01 运行时间,渐进分析
- dflk djfas圣诞快乐就防守对方开始大幅
- ubuntu中JDK安装
- Android Intent调用 Uri的使用几种格式
- 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构
- 设计模式——策略模式(Strategy) 笔记
- 百度校园招聘(笔试题集)
- 数字证书
- 二进制按位翻转
- Geotools使用-2,Maven的替代
- 1、输入10个整数,将其中最小的数与第一个数对换,把最大的数和最后一个数对换.
- dthdfjhfgjfjghjvghhjfhjfh
- 第一个Demo学到的东西(一)
- 阿拉伯数字的金额转换成中国传统的形式