几种常用的设计模式代码

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:工厂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值