87 java反射_3 _设计模式

设计模式

  1. 什么是设计模式:一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。

    • 简单理解:特定问题的固定解决方法。
  2. 好处:使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、重用性。

  3. 在Gof的《设计模式》书中介绍了23种设计模式
    在这里插入图片描述

工厂设计模式

  1. 工厂模式主要负责对象创建的问题。
  2. 开发中有一个非常重要的原则“开闭原则”,对拓展开放、对修改关闭。
  3. 可通过反射进行工厂模式的设计,完成动态的对象创建。(通过类对象)

代码:

package com.wlw.chapter12_reflex.demo02_factory;
// 父类
public interface Usb {
    void service();
}
package com.wlw.chapter12_reflex.demo02_factory;
//子类
public class Mouse implements Usb {
    @Override
    public void service() {
        System.out.println("鼠标开始工作了");
    }
}


package com.wlw.chapter12_reflex.demo02_factory;
//子类
public class Fan implements Usb {
    @Override
    public void service() {
        System.out.println("风扇开始工作了");
    }
}


package com.wlw.chapter12_reflex.demo02_factory;
//子类
public class Upan implements Usb {
    @Override
    public void service() {
        System.out.println("U盘开始工作了");
    }
}


package com.wlw.chapter12_reflex.demo02_factory;
//子类 键盘
public class KeyBoard implements Usb {
    @Override
    public void service() {
        System.out.println("键盘开始工作了");
    }
}


package com.wlw.chapter12_reflex.demo02_factory;
public class Mouse2 implements Usb {
    @Override
    public void service() {
        System.out.println("鼠标2开始执行");
    }
}
package com.wlw.chapter12_reflex.demo02_factory;
//工厂类
public class UsbFactory {
    /*
    Usb是接口(父类),其他为子类(商品),如果增加一个子类(商品),
    我就需要更改UsbFactory里的方法,违背了开闭原则,需要优化
    反射优化:通过类对象来实例化 ,再增加一个子类(商品)话,就在属性文件中添加这个子类的全名称
    利用流和Properties获取后传给creatUsb()方法
     */
    public static Usb creatUsb(String type){ //传入的是每个子类的全名称 ,通过类对象来实例化
        Usb usb = null;
        Class<?> class1 = null;
        try {
            class1 = Class.forName(type);
            usb = (Usb)class1.newInstance();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return usb;
    }

    /*public static Usb creatUsb(int type){
        Usb usb = null;
        if(type == 1){ //1 表示鼠标
            usb = new Mouse();
        }else if(type == 2){ //2 表示风扇
            usb = new Fan();
        }else if(type == 3){ //3 表示U盘
            usb = new Upan();
        }else if(type == 4){
            usb = new KeyBoard();
        }
        return usb;
    }*/
}
package com.wlw.chapter12_reflex.demo02_factory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Scanner;
// 客户程序
public class Demo {
    public static void main(String[] args) throws Exception {
        System.out.println("===========请选择你想要购买的商品:1鼠标 2风扇 3U盘 4键盘 5鼠标2=======================");
        Scanner input = new Scanner(System.in);
        String choice = input.next();
        //1 = com.wlw.chapter12_reflex.demo02_factory.Mouse
        //2 = com.wlw.chapter12_reflex.demo02_factory.Fan
        //3 = com.wlw.chapter12_reflex.demo02_factory.Upan
        //4 = com.wlw.chapter12_reflex.demo02_factory.KeyBoard
        //可以通过 属性文件来 保存这些值
        Properties properties = new Properties();
        FileInputStream fis =new FileInputStream("usb.properties");
        properties.load(fis);
        fis.close();

        Usb usb = UsbFactory.creatUsb(properties.getProperty(choice));
        if(usb != null){
            System.out.println("购买成功");
            usb.service();
        }else {
            System.out.println("购买失败,你购买的商品不存在");
        }
    }
}

单例模式

  • 单例(Singleton):只允许创建一个该类的对象。

方式1:饿汉式

  • 类加载时创建,天生线程安全

    class Singleton{
        private static final Singleton instance = new Singleton();//静态常量
        private Singleton(){}
        public static Singleton getInstance(){
            return instance;
        }
    }
    

代码:

package com.wlw.chapter12_reflex.demo03_singleton;

/**
 * 饿汉式单例
 * (1)首先创建一个常量
 * (2)构造方法改成私有的。类外那不能创建对象
 * (3)通过一个公开的方法。返回这个对象
 */
public class Singleton {
    private static final Singleton instance = new Singleton();//静态常量
    private Singleton(){}
    public static Singleton getInstance(){
        return instance;
    }
}
package com.wlw.chapter12_reflex.demo03_singleton;

public class TestSingleton {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Singleton.getInstance().hashCode());
                }
            }).start();
        }
    }
}
/*执行结果:  三次的对象都是一样的
1543370641
1543370641
1543370641
*/

方式2:懒汉式

  • 使用时创建,线程不安全,加同步

    class Singleton{
        private static  Singleton instance = null;
        private Singleton(){}
        public static synchronized Singleton getInstance(){
            if(instance == null){
                instance = new Singleton();
            }
            return instance;
        }
    }
    

代码:

package com.wlw.chapter12_reflex.demo03_singleton;

/**
 * 懒汉式单例
 * (1)首先创建一个对象,赋值为null
 * (2)构造方法改成私有的。类外那不能创建对象
 * (3)通过一个公开的方法。返回这个对象() (需要同步:同步方法 和 同步代码块)
 */
public class Singleton2 {
    private static Singleton2 instance = null;
    private Singleton2(){}
    
    /*public static synchronized Singleton2 getInstance(){ //同步方法
        if(instance == null){
            instance = new Singleton2();
        }
        return instance;
    }*/

    public static Singleton2 getInstance(){
        if(instance == null){ //这一句是为了提高执行效率,不用每次都判断锁
            synchronized (Singleton2.class){ //同步代码块
                if(instance == null){
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
}
package com.wlw.chapter12_reflex.demo03_singleton;

public class TestSingleton2 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Singleton2.getInstance().hashCode());
                }
            }).start();
        }
    }
}
/*执行结果: 三次的对象都是一样的
1365888608
1365888608
1365888608
*/

单例模式两种方式的优缺点

  • 饿汉式:
    • 优点:线程安全
    • 缺点:声明周期太长,浪费空间
  • 懒汉式:
    • 优点:声明周期短,节省空间
    • 优点:有线程安全问题(可通过同步方法与同步代码块来改善)

方式3:静态内部类写法(懒汉式)

  • 线程安全,使用时创建

    class Singleton{
        private Singleton(){}
        private static class Holder{
            static Singleton s = new Singleton(); 
        }
        public static Singleton instance(){
    		return Holder.s;
        }
    }
    

代码:

package com.wlw.chapter12_reflex.demo03_singleton;

// 静态内部类写法
public class Singleton3 {
    private Singleton3(){}
    private static class Holder{ // 静态内部类不使用是不会执行的
        static Singleton3 instance = new Singleton3();
    }
    public static Singleton3 getInstance(){
        return Holder.instance;
    }
}
package com.wlw.chapter12_reflex.demo03_singleton;

public class TestSingleton3 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Singleton3.getInstance().hashCode());
                }
            }).start();
        }
    }
}
/*执行结果: 三次的对象都是一样的
1365888608
1365888608
1365888608
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悬浮海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值