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("小魏家庭結婚了");
        }
    }
}

 

 

 

 

 

 

 

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