几种常用的设计模式代码

来源:互联网 发布:js随机数1到22 编辑:程序博客网 时间:2024/05/29 08:09

1:单例

public class Singleton {    private static volatile Singleton instance = null;    private Singleton(){    }     public static Singleton getInstance() {        if (instance == null) {            synchronized (Singleton.class) {                if (instance == null) {                    instance = new Singleton();                }            }        }        return instance;    }}
2:builder

public class Person {    private String name;    private int age;    private double height;    private double weight;    privatePerson(Builder builder) {        this.name=builder.name;        this.age=builder.age;        this.height=builder.height;        this.weight=builder.weight;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public double getHeight() {        return height;    }    public void setHeight(double height) {        this.height = height;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }    static class Builder{        private String name;        private int age;        private double height;        private double weight;        public Builder name(String name){            this.name=name;            return this;        }        public Builder age(int age){            this.age=age;            return this;        }        public Builder height(double height){            this.height=height;            return this;        }        public Builder weight(double weight){            this.weight=weight;            return this;        }        public Person build(){            return new Person(this);        }    }}
3:观察者 
4:适配器

笔记本类/** *  * 笔记本 * @author studyjun * */public class Jotter {         private VoltageAdapter adapter;         public VoltageAdapter getAdapter() {        return adapter;    }     public void inputVoltage(){        System.out.println("笔记本得到输入电压" +adapter.transformVoltage()+"v");    }         public void setAdapter(VoltageAdapter adapter){        this.adapter =adapter;    }}供电器220v,提供220v的电压/** *  * 供电器 * @author studyjun * */public class PowerSupplyDevice {     //标准电压    private int standardVoltage =220;              /**     * 输出220v电压     * @return     */    public int powerSupply(){        System.out.println("标准电压提供220v");        return standardVoltage;    }}适配器接口,有个电压转换接口/** *  * 电源适配器 * @author studyjun * */public interface VoltageAdapter {         /**     * 转换电压     */    public int transformVoltage();}笔记本电源适配器类,把220v转为笔记本的15v的电压/** *  * 电源适配器 * @author studyjun * */public class MyVoltageAdapter implements VoltageAdapter{         private int voltage; //电压    /**     *  标准电压设备220v     */    private PowerSupplyDevice powerSupplyDevice ;              public MyVoltageAdapter(PowerSupplyDevice powerSupplyDevice) {        this.powerSupplyDevice = powerSupplyDevice;    }       public int getVoltage() {        return voltage;    }       public void setVoltage(int voltage) {        this.voltage = voltage;    }         public PowerSupplyDevice getPowerSupplyDevice() {        return powerSupplyDevice;    }       public void setPowerSupplyDevice(PowerSupplyDevice powerSupplyDevice) {        this.powerSupplyDevice = powerSupplyDevice;    }      /**     * 转换电压     */    public int transformVoltage(){        int voltage = powerSupplyDevice.powerSupply();        int lowerVoltage = voltage/14;        System.out.println("转化中电压为="+lowerVoltage+"v");        return lowerVoltage;             }}测试public class Test {         public static void main(String[] args) {                 Jotter jt = new Jotter();        PowerSupplyDevice powerSupplyDevice = new PowerSupplyDevice();        VoltageAdapter adapter=new MyVoltageAdapter(powerSupplyDevice);        jt.setAdapter(adapter);        jt.inputVoltage();    }     }

5:工厂