java查缺补漏之四十七天(懒汉式饿汉式单例模式,装饰者模式,观察者模式,工厂模式,适配器模式)

1.你知道几种单例模式?

(1)懒汉原始式:在反射面前没什么用,线程不安全

需要注意的式构造方法也是private

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

(2)懒汉线程同步式:在方法调用上加了同步,虽然线程安全了,但是每次都有同步,会影响性能,毕竟99%是不需要同步的

public class Singleton {
    private Singleton() {}
    private static Singleton singleton = null;

    //为了保证多线程环境下正确访问,给方法加上同步锁synchronized
    public static synchronized Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
}

(3)懒汉式双重校验锁:线程安全,且确保了只有第一次调用单例时才会做同步,避免了每次都同步的性能损耗;同样的,也降低了性能。

public class Singleton {
    private Singleton() {}
    private static Singleton singleton = null;

    //双重锁检查
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

(4)饿汉式:类初始化的时候就立刻实例化,天生线程安全,系统启动时,占用资源;如果后期这个单例没有被使用,会造成资源浪费

注意这里面创建的时候有关键字final

public class Singleton {
    private Singleton() {}
    private static final Singleton singleton = new Singleton();
    public static Singleton getInstance() {
        return singleton;
    }
}

2. 手写一个装饰者模式?

public class DecoratorTest {
    public static void main(String[] args)
    {
        //如果不用装饰者模式,那么就得一个继承一个
        //那么现在呢,都继承的式food,避免了臃肿的继承关系,多继承,继承复杂的问题
        //在这个装饰者下就可以随意的组合了
        //主要用到的知识点就是多态
        //实现主要就是带一个有参的构造方法,将父类传进去
        Meat meat=new Meat(new Milk(new Egg(new Food())));
        meat.make();
    }
    public static class Food
    {
        public void make()
        {
            System.out.print("面包");
        }
    }
    public static class Egg extends Food{
        private Food basicFood;
        public Egg(Food basicFood)
        {
            this.basicFood=basicFood;
        }
        public void make() {
            basicFood.make();
            System.out.print("+鸡蛋");
        }
    }
    public static class Milk extends Food{
        private Food basicFood;
        public Milk(Food basicFood)
        {
            this.basicFood=basicFood;
        }
        public void make() {
            basicFood.make();
            System.out.print("+牛奶");
        }
    }
    public static class Meat extends Food{
        private Food basicFood;
        public Meat(Food basicFood)
        {
            this.basicFood=basicFood;
        }
        public void make() {
            basicFood.make();
            System.out.print("+肉");
        }
    }
}

3.手写观察者模式?

public interface Person {
    void getMessage(String s);
}
import java.util.ArrayList;

public class ObserverTest {
    public static void main(String[] args)
    {
        //观察者模式很简单,简单来说就是多个事务在观察另一个事务
        //被观察的事务会通知观察者,通过方法调用的方式
        //用到了多态
        Xiaoli xiaoli=new Xiaoli();
        Xiaowei xiaowei=new Xiaowei();
        Xiaomei xiaomei=new Xiaomei();
        //将需要通知的对象加入到里面
        xiaomei.addPerson(xiaoli);
        xiaomei.addPerson(xiaowei);
        xiaomei.notifyPerson();
    }
    public static class Xiaoli implements Person
    {
        public void getMessage(String s)
        {
            System.out.println("小李收到了你的信息:"+s);
        }
    }
    public static class Xiaowei implements Person
    {
        public void getMessage(String s)
        {
            System.out.println("小魏收到了你的信息:"+s);
        }
    }
    public static class Xiaomei
    {
        ArrayList<Person> list=new ArrayList<>();
        public void addPerson(Person person)
        {
            list.add(person);
        }
        public void notifyPerson()
        {
            for(Person person:list)
            {
                person.getMessage("你们过来吧");
            }
        }
    }
}

4.手写工厂模式?

参考:https://blog.csdn.net/jason0539/article/details/23020989?utm_source=distribute.pc_relevant.none-task

 

 

5.手写适配器模式?

public class AdaptorTest {
    public static void main(String[] args)
    {
        //适配器模式就是将不同的事务联系在一起
        //这里是将手机和220V连接在一起
        //通过一个类充当适配器
        //可以传入这个适配器也可以不传入这个适配器
        Phone phone=new Phone();
        phone.charge();
        Adaptor adaptor=new Adaptor();
        phone.charge(adaptor);
    }
    public static class Phone
    {
        private static final int V=20;

        public void charge()
        {
            System.out.println("正在充电。。。");
            System.out.println("充电电压为:"+V);
        }
        public void charge(Adaptor adaptor)
        {
            int newV=adaptor.changeVol(V);
            System.out.println("正在充电。。。");
            System.out.println("充电电压为:"+newV);
        }
    }
    public static class Adaptor
    {
        public int changeVol(int V)
        {
            System.out.println("启用电压适配器");
            System.out.println("改变电压为:"+(int)(V+200));
            return V+200;
        }
    }
}

 

6.手写代理模式?

public interface Proxy {
    void marry();
}
public class ProxyTest {
    public static void main(String[] args)
    {
        //代理模式其实就是自己做的任务不复杂
        //将复杂的任务交给一个代理去做
        //应用到了多态
        //当然我这里只是做了简单的实现,可能我们不需要用构造方法来设置
        //其实可以用set方法来设置,然后再里面弄一个表或者队列什么的
        XiaoliHome xiaoliHome=new XiaoliHome();
        XiaoweiHome xiaoweiHome=new XiaoweiHome();
        WeddingCompany weddingCompany=new WeddingCompany(xiaoliHome);
        WeddingCompany weddingCompany1=new WeddingCompany(xiaoweiHome);
        weddingCompany.marry();
        weddingCompany1.marry();
    }
    public static class WeddingCompany implements Proxy
    {
        Proxy proxy;
        WeddingCompany(Proxy proxy)
        {
            this.proxy=proxy;
        }
        public void marry()
        {
            System.out.println("我们是婚庆公司的");
            System.out.println("我们在做结婚前的准备工作");
            System.out.println("节目彩排...");
            System.out.println("礼物购买...");
            System.out.println("工作人员分工...");
            System.out.println("可以开始结婚了");
            proxy.marry();
            System.out.println("结婚完毕,我们需要做后续处理,你们可以回家了,其余的事情我们公司来做");
        }
    }
    public static class XiaoliHome implements Proxy
    {
        public void marry()
        {
            System.out.println("小李家庭结婚了");
        }
    }
    public static class XiaoweiHome implements Proxy
    {
        public void marry()
        {
            System.out.println("小魏家庭结婚了");
        }
    }
}

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章