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(?)

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值