zoukankan      html  css  js  c++  java
  • 设计模式

    单例模式

    package com.designpattern;
    
    public class SingletonDemo {
        private static volatile SingletonDemo singleton;
        private SingletonDemo(){}
    
        private static SingletonDemo getInstance() {
            if (singleton == null) {
                synchronized (SingletonDemo.class) {
                    if (singleton == null) {
                        singleton = new SingletonDemo();
                    }
                }
            }
            return singleton;
        }
    }

    简单工厂模式

    package com.designpattern;
    
    /**
     * 简单工厂模式
     *
     * 抽象Product、Product类、工厂类、
     *
     */
    abstract class Phone{
        public abstract void sing();
    }
    
    class MiPhone extends Phone {
    
        @Override
        public void sing() {
            System.out.println("xiaomi");
        }
    }
    
    class IPhone extends Phone {
        @Override
        public void sing(){
            System.out.println("Iphone");
        }
    }
    
    class PhoneFactory{
        public Phone make(String kind){
            if(kind.equalsIgnoreCase("xiaOmI")){
                return new MiPhone();
            }else if(kind.equalsIgnoreCase("iphOne")){
                return new IPhone();
            }
            return null;
        }
    }
    
    public class SimpleFactoryDemo {
        public static void main(String[] args) {
            PhoneFactory pf = new PhoneFactory();
            Phone phone = pf.make("xiaoMI");
            phone.sing();
    
        }
    }

    工厂方法模式

    package com.designpattern;

    /**
    * 工厂方法模式
    *
    * 抽象目标类、具体目标类、工厂接口
    *
    */
    //abstract class Phone{
    // public abstract void sing();
    //}
    //
    //class MiPhone extends Phone {
    // @Override
    // public void sing() {
    // System.out.println("xiaomi");
    // }
    //}
    //
    //class IPhone extends Phone {
    //
    // @Override
    // public void sing() {
    // System.out.println("iphone");
    // }
    //}

    interface IFactory{
    Phone makePhone();
    }

    class MiFactory implements IFactory{
    @Override
    public Phone makePhone() {
    return new MiPhone();
    }
    }

    class IPhoneFactory implements IFactory {

    @Override
    public Phone makePhone() {
    return new IPhone();
    }
    }

    public class FactoryMethodDemo {
    public static void main(String[] args) {
    IFactory miFactory = new MiFactory();
    IFactory iphoneFactory = new IPhoneFactory();
    Phone miPhone = miFactory.makePhone();
    Phone iPhone = iphoneFactory.makePhone();
    miPhone.sing();
    iPhone.sing();

    }
    }
    一回生,二回熟
  • 相关阅读:
    Javascript的异步和回调
    JS-使用工厂方法创建对象
    PHPUnit使用教程——PHP环境变量+x-debug+composer+phpunit配置安装(超详细!)
    JQuery 纵向二级菜单与对齐方式
    图像映射<map>、<area>
    打开另一个窗口
    多行文本省略号
    replace 正则
    jquery each用法
    li前面的原点或者方的样式修改html中列表项li所显示的圆点的颜色?,以及相关样式的设定
  • 原文地址:https://www.cnblogs.com/zzytxl/p/12763951.html
Copyright © 2011-2022 走看看