1. 程式人生 > >spring原始碼分析---策略模式、原型模式、模板模式

spring原始碼分析---策略模式、原型模式、模板模式

策略模式 —–> 用於回撥處理

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 //返回值是固定的 //0 、-1 、1 //0 、 >0 、<0 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 {
            //return super.clone();//預設淺克隆,只克隆八大基本資料型別和String
            //序列化
            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(){
        //1、把水燒開
        boilWater();
        //2、把杯子準備好、原材料放到杯中
        pourInCup();
        //3、用水沖泡
        brew();
        //4、新增輔料
        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) {

//      Coffee coffee = new Coffee();
//      coffee.create();

        Tea tea = new Tea();
        tea.create();

    }


    //SpringJDBC
    //是java規範,各個資料庫廠商自己去實現
    //1、載入驅動類DriverManager
    //2、建立連線
    //3、建立語句集(標準語句集、預處理語句集)(語句集?  MySQL、Oracle、SQLServer、Access)
    //4、執行語句集
    //5、結果集ResultSet 遊標
    //ORM(?)

}