常用的设计模式

单例设计模式

饿汉式-方式1(静态变量方式)

public class Singleton {
    //私有构造方法
    private Singleton() {}

    //在成员位置创建该类的对象
    private static Singleton instance = new Singleton();

    //对外提供静态方法获取该对象
    public static Singleton getInstance() {
        return instance;
    }
}

懒汉式-方式1(线程不安全)

public class Singleton {
    //私有构造方法
    private Singleton() {}

    //在成员位置创建该类的对象
    private static Singleton instance;

    //对外提供静态方法获取该对象
    public static Singleton getInstance() {

        if(instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

懒汉式-方式2(线程安全)

public class Singleton {
    //私有构造方法
    private Singleton() {}

    //在成员位置创建该类的对象
    private static Singleton instance;

    //对外提供静态方法获取该对象
    public static synchronized Singleton getInstance() {

        if(instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

懒汉式-方式3(双重检查锁)

public class Singleton { 

    //私有构造方法
    private Singleton() {}

    private static Singleton instance;

   //对外提供静态方法获取该对象
    public static Singleton getInstance() {
        //第一次判断,如果instance不为null,不进入抢锁阶段,直接返回实例
        if(instance == null) {
            synchronized (Singleton.class) {
                //抢到锁之后再次判断是否为null
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

双重检查方式  volatile

public class Singleton {

    //私有构造方法
    private Singleton() {}

    private static volatile Singleton instance;

   //对外提供静态方法获取该对象
    public static Singleton getInstance() {
        //第一次判断,如果instance不为null,不进入抢锁阶段,直接返回实际
        if(instance == null) {
            synchronized (Singleton.class) {
                //抢到锁之后再次判断是否为空
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

这里还说一下volatile关键字的第二个作用,保证变量在多线程运行时的可见性:

public class Test01 {
    public static void main(String[] args) throws Exception{
        T t=new T();
        t.start();

        Thread.sleep(2000);
        System.out.println("主线程设置t线程的参数来止损失");
        t.setFlag(false);
    }
}
class T extends Thread{
    private volatile boolean flag=true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        System.out.println("进入run方法");
        while(flag){

        }
    }
}

2.什么是工厂模式

简单工厂模式:

public class BMW320 {
    public BMW320(){
        System.out.println("制造-->BMW320");
    }
}
 
public class BMW523 {
    public BMW523(){
        System.out.println("制造-->BMW523");
    }
}
 
public class Customer {
    public static void main(String[] args) {
        BMW320 bmw320 = new BMW320();
        BMW523 bmw523 = new BMW523();
    }
}

产品类:

abstract class BMW {
    public BMW(){}
}
 
public class BMW320 extends BMW {
    public BMW320() {
        System.out.println("制造-->BMW320");
    }
}
public class BMW523 extends BMW{
    public BMW523(){
        System.out.println("制造-->BMW523");
    }
}

工厂类:

public class Factory {
    public BMW createBMW(int type) {
        switch (type) {
        
        case 320:
            return new BMW320();
 
        case 523:
            return new BMW523();
 
        default:
            break;
        }
        return null;
    }
}

用户类:

public class Customer {
    public static void main(String[] args) {
        Factory factory = new Factory();
        BMW bmw320 = factory.createBMW(320);
        BMW bmw523 = factory.createBMW(523);
    }
}

工厂方法模式:

产品类:

abstract class BMW {
    public BMW(){}
}
public class BMW320 extends BMW {
    public BMW320() {
        System.out.println("制造-->BMW320");
    }
}
public class BMW523 extends BMW{
    public BMW523(){
        System.out.println("制造-->BMW523");
    }
}

工厂类:

interface FactoryBMW {
    BMW createBMW();
}
 
public class FactoryBMW320 implements FactoryBMW{
 
    @Override
    public BMW320 createBMW() {
        return new BMW320();
    }
 
}
public class FactoryBMW523 implements FactoryBMW {
    @Override
    public BMW523 createBMW() {
        return new BMW523();
    }
}

客户类:

public class Customer {
    public static void main(String[] args) {
        FactoryBMW320 factoryBMW320 = new FactoryBMW320();
        BMW320 bmw320 = factoryBMW320.createBMW();
 
        FactoryBMW523 factoryBMW523 = new FactoryBMW523();
        BMW523 bmw523 = factoryBMW523.createBMW();
    }
}

1. 适配器模式

/**
 * 源角色(Adaptee):现在需要适配的接口。
 */
public class AC220 {
    /**
     * 输出220V交流电
     *
     * @return
     */
    public int output220V() {
        int output = 220;
        return output;
    }
}

/**
 * 目标角色(Target):这就是所期待得到的接口。
 * 注意:由于这里讨论的是类适配器模式,因此目标不可以是类。
 */
public interface DC5 {
    /**
     * 输出5V直流电(期待得到的接口)
     *
     * @return
     */
    int output5V();
}

/**
 * 类适配器
 * 适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。
 */
public class PowerAdapter extends AC220 implements DC5 {
    /**
     * 输出5V直流电
     *
     * @return
     */
    @Override
    public int output5V() {
        int output = output220V();
        return (output / 44);
    }
}
 

/**
 * 测试类适配器
 */
public class TestClassAdapter {
    public static void main(String[] args) {
        DC5 dc5 = new PowerAdapter();
        System.out.println("输出电流:" + dc5.output5V() + "V");
    }
}

适配器模式之对象适配器模式

/**
 * 源角色(Adaptee):现在需要适配的接口。
 */
public class AC220 {
    /**
     * 输出220V交流电
     *
     * @return
     */
    public int output220V() {
        int output = 220;
        return output;
    }
}

/**
 * 目标角色(Target):这就是所期待得到的接口。
 */
public interface DC5 {
    /**
     * 输出5V直流电(期待得到的接口)
     *
     * @return
     */
    int output5V();
}

/**
 * 对象适配器
 */
public class PowerAdapter implements DC5 {
    private AC220 ac220;

    public PowerAdapter(AC220 ac220) {
        this.ac220 = ac220;
    }

    /**
     * 输出5V直流电
     *
     * @return
     */
    @Override
    public int output5V() {
        int output = this.ac220.output220V();
        return (output / 44);
    }
}

/**
 * 测试对象适配器
 */
public class TestObjectAdapter {
    public static void main(String[] args) {
        AC220 ac220 = new AC220();
        PowerAdapter powerAdapter = new PowerAdapter(ac220);
        System.out.println("输出电流:" + powerAdapter.output5V() + "V");
    }
}

代理模式

public class Singer{
    public void sing(){
        System.out.println("唱一首歌");
    }  
}


public void sing(){
    System.out.println("向观众问好");
    System.out.println("唱一首歌");
    System.out.println("谢谢大家");
}  

静态代理


public interface ISinger {
    void sing();
}
/**
 *  目标对象实现了某一接口
 */
public class Singer implements ISinger{
    public void sing(){
        System.out.println("唱一首歌");
    }  
}
/**
 *  代理对象和目标对象实现相同的接口
 */
public class SingerProxy implements ISinger{
    // 接收目标对象,以便调用sing方法
    private ISinger target;
    public UserDaoProxy(ISinger target){
        this.target=target;
    }
    // 对目标对象的sing方法进行功能扩展


public interface ISinger {
    void sing();
}
/**
 *  目标对象实现了某一接口
 */
public class Singer implements ISinger{
    public void sing(){
        System.out.println("唱一首歌");
    }  
}
/**
 *  代理对象和目标对象实现相同的接口
 */
public class SingerProxy implements ISinger{
    // 接收目标对象,以便调用sing方法
    private ISinger target;
    public UserDaoProxy(ISinger target){
        this.target=target;
    }
    // 对目标对象的sing方法进行功能扩展
   public void sing() {
       System.out.println("向观众问好");
       target.sing();
       System.out.println("谢谢大家");
   }

}

测试


/**
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        //目标对象
        ISinger target = new Singer();
        //代理对象
        ISinger proxy = new SingerProxy(target);
        //执行的是代理的方法
        proxy.sing();
    }
}

动态代理(也叫JDK代理)


 public interface ISinger {
     void sing();
 }
 
 /**
  *  目标对象实现了某一接口
  */
 public class Singer implements ISinger{
     public void sing(){
         System.out.println("唱一首歌");
     }  
 }


 public class Test{
     public static void main(String[] args) {
   Singer target = new Singer();
         ISinger proxy  = (ISinger) Proxy.newProxyInstance(
                 target.getClass().getClassLoader(),
                 target.getClass().getInterfaces(),
                 new InvocationHandler() {
                     @Override
                     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                         System.out.println("向观众问好");
                         //执行目标对象方法
                         Object returnValue = method.invoke(target, args);
                         System.out.println("谢谢大家");
                         return returnValue;
                     }
                 });
         proxy.sing();
     }

Cglib代理

/**
 * 目标对象,没有实现任何接口
 */
public class Singer{

    public void sing() {
        System.out.println("唱一首歌");
    }
}

/**
 * Cglib子类代理工厂
 */
public class ProxyFactory implements MethodInterceptor{
    // 维护目标对象
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    // 给目标对象创建一个代理对象
    public Object getProxyInstance(){,
        //1.工具类
        Enhancer en = new Enhancer();
        //2.设置父类
        en.setSuperclass(target.getClass());
        //3.设置回调函数
        en.setCallback(this);
        //4.创建子类(代理对象)
        return en.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("向观众问好");
        //执行目标对象的方法
        Object returnValue = method.invoke(target, args);
        System.out.println("谢谢大家");
        return returnValue;
    }
}

/**
 * 测试类
 */
public class Test{
    public static void main(String[] args){
        //目标对象
        Singer target = new Singer();
        //代理对象
        Singer proxy = (Singer)new ProxyFactory(target).getProxyInstance();
        //执行代理对象的方法
        proxy.sing();
    }
}

观察者模式

观察者模式分为四个角色:
    1.抽象被观察者角色
        抽象出一个公众号接口/抽象类
        公共的方法:
            关注方法
            取关方法
            设置推送消息方法
            群发方法

    2.被观察者角色
        有一个具体的公众号,实现/继承 抽象被观察者

    3.抽象观察者角色
        抽象出一个客户 接口/抽象类
        公共方法:
            接收公众号推送的消息
    4.观察者角色
        具体用户  实现/继承 抽象观察者

抽象观察者角色:

package demo02;


public interface UserInterface {
    /**
     * 接受公众号发生的消息
     * @param msg
     */
    public void getMsg(String msg);
}

观察者角色:

public class User implements UserInterface {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public void getMsg(String msg) {
        System.out.println(name+"接受到公众号发送的消息:"+msg);
    }
}
抽象公众号:

public interface PublicHaoInterface {

    public void add(UserInterface userInterface);

    public void remove(UserInterface userInterface);

    public void setMsg(String msg);

    public void qunfa();

}

具体公共号:

public class AAAPublicHao implements PublicHaoInterce {
    List<UserInterface> list=new ArrayList<UserInterface>();
    //设置消息
    private String msg;
    public void add(UserInterface userInterface) {
        list.add(userInterface);
    }

    public void remove(UserInterface userInterface) {
        list.remove(userInterface);
    }

    public void setMsg(String msg) {
        this.msg=msg;
    }

    public void qunfa() {
            for(UserInterface userInterface:list){
                userInterface.getMsg(msg);
            }
    }
}

模板方法

1.取材
 2.处理食材
 3.起锅烧油
 4.放入食材
 5.翻炒
 6.放入各种调料
 7.翻炒均匀
 8.出锅

炒菜接口:

public abstract class ChaoCai {
    //不能被子类重写
    public final void chaocai(){
        quShiCai();
        handle();
        qiGuoShaoYou();
        putShiCai();
        fanChao();
        putTiaoLiao();
        junYun();
        outGuo();
    }

    //1.取食材
    void quShiCai(){};
    //2.处理食材
    void handle(){};
    //3.起锅烧油
    void qiGuoShaoYou(){};
    //4.放入食材
    void putShiCai(){};
    //5.翻炒
    void fanChao(){};
    //6.放入各种调料
    void putTiaoLiao(){};
    //7.翻炒均匀
    void junYun(){};
    //8.出锅
    void outGuo(){};
}

番茄炒菜

public class FanQieChaoDan extends ChaoCai{

    @Override
    void quShiCai() {
        System.out.println("取鸡蛋和番茄");
    }

    @Override
    void handle() {
        System.out.println("处理鸡蛋和番茄");
    }

    @Override
    void qiGuoShaoYou() {
        System.out.println("起锅烧油");
    }

    @Override
    void putShiCai() {
        System.out.println("翻入鸡蛋和番茄");
    }

    @Override
    void fanChao() {
        System.out.println("翻炒");
    }

    @Override
    void putTiaoLiao() {
        System.out.println("翻入调料");
    }

    @Override
    void junYun() {
        System.out.println("翻炒均匀");
    }

    @Override
    void outGuo() {
        System.out.println("出锅!");
    }
}

测试:

public class ChaoCaiTest {

    public static void main(String[] args) {
        FanQieChaoDan fanQieChaoDan = new FanQieChaoDan();
        fanQieChaoDan.chaocai();
    }
}

策略模式

促销活动

public class CuXiaoContext {

    private CuXiao cuXiao;

    public CuXiaoContext(CuXiao cuXiao) {
        this.cuXiao = cuXiao;
    }
    //定义一个使用算法的方法
    public void use(){
        cuXiao.cuxiao();
    }
}

public interface CuXiao {
    void cuxiao();
}

public class SuanFa618 implements CuXiao {

    @Override
    public void cuxiao() {
        System.out.println("使用6.18的促销算法");
    }
}

public class SuanFa1111 implements CuXiao{

    @Override
    public void cuxiao() {
        System.out.println("使用11.11的促销算法");
    }
}

public class SuanFa1212 implements CuXiao{

    @Override
    public void cuxiao() {
        System.out.println("使用12.12的促销算法");
    }
}

public class CuXiaoTest {

    public static void main(String[] args) {
        //SuanFa618 suanFa618 = new SuanFa618();
        SuanFa1111 suanFa1111 = new SuanFa1111();
        CuXiaoContext context = new CuXiaoContext(suanFa1111);
        context.use();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值