策略模式 —–> 用于回调处理
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class MyListTest {
public static void main(String[] args) {
new MyList().sort(new NumberComparator());
List<Long> numbers = new ArrayList<Long>();
Collections.sort(numbers, new Comparator<Long>() {
@Override
public int compare(Long o1, Long o2) {
return 0;
}
});
}
}
策略模式:男生追女生,结果是一样的,大部分都追到了,但是追的策略不一样,怎么样才能追到女生呢?那就要有好的策略。
原型模式(真假孙悟空)
孙悟空有个特殊的技能,就是拔跟猴毛就可以变出一个孙悟空,这个孙悟空和真的孙悟空是长的一模一样,但是他们是不同的对象。
应用场景:主要是用来拷贝复杂的数据结构对象的数据
---->
---->
package prototype.greatestsage;
import java.util.Date;
public class Monkey {
protected int height;
protected int weight;
protected Date birthday;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package prototype.greatestsage;
import java.io.Serializable;
/**
* 金箍棒
* @author Tom
*
*/
public class GoldRingedStaff implements Serializable{
private float height = 100;
private float diameter = 10;
/**
* 金箍棒长大
*/
public void grow(){
this.diameter *= 2;
this.height *= 2;
}
/**
* 金箍棒缩小
*/
public void shrink(){
this.diameter /= 2;
this.height /= 2;
}
}
package prototype.greatestsage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class TheGreatestSage extends Monkey implements Cloneable, Serializable{
private GoldRingedStaff staff;
public TheGreatestSage(){
this.staff = new GoldRingedStaff();
this.birthday = new Date();
this.height = 150;
this.weight = 30;
System.out.println("------------------------");
}
public Object clone(){
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
TheGreatestSage copy = (TheGreatestSage)ois.readObject();
copy.birthday = new Date();
return copy;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void change(){
TheGreatestSage copySage = (TheGreatestSage)clone();
System.out.println("大圣本尊生日是:" + this.getBirthday().getTime());
System.out.println("克隆大圣的生日是:" + copySage.getBirthday().getTime());
System.out.println("大圣本尊和克隆大圣是否为同一个对象:" + (this == copySage));
System.out.println("大圣本尊持有的金箍棒跟克隆大圣持有金箍棒是否为同一个对象:" + (this.getStaff() == copySage.getStaff()));
}
public GoldRingedStaff getStaff() {
return staff;
}
public void setStaff(GoldRingedStaff staff) {
this.staff = staff;
}
}
package prototype.greatestsage;
public class TestPrototype {
public static void main(String[] args) {
TheGreatestSage sage = new TheGreatestSage();
sage.change();
}
}
模板模式(固定的执行流程)
package template;
public abstract class Bevegrage {
public final void create(){
boilWater();
pourInCup();
brew();
addCoundiments();
}
public abstract void pourInCup();
public abstract void addCoundiments();
public void brew(){
System.out.println("将开水放入杯中进行冲泡");
};
public void boilWater(){
System.out.println("烧开水,烧到100度可以起锅了");
}
}
package template;
public class Coffee extends Bevegrage{
public void pourInCup() {
System.out.println("将咖啡倒入杯中");
}
public void addCoundiments() {
System.out.println("添加牛奶和糖");
}
}
package template;
public class Tea extends Bevegrage{
public void pourInCup() {
System.out.println("将茶叶放入杯中");
}
public void addCoundiments() {
System.out.println("添加蜂蜜");
}
}
package template;
public class TestTemplate {
public static void main(String[] args) {
Tea tea = new Tea();
tea.create();
}
}