几种常用的设计模式代码
来源:互联网 发布: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:builderpublic 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:工厂
- 几种常用的设计模式代码
- 几种常用的设计模式
- 几种常用的设计模式介绍
- 几种常用的设计模式介绍
- 几种常用的设计模式介绍
- 几种常用的设计模式
- 面试-几种常用的设计模式
- 常用的几种设计模式
- 几种常用的设计模式
- 几种常用的设计模式
- 几种常用的Java设计模式
- 几种常用的设计模式介绍
- 几种常用的设计模式
- 几种常用的ios设计模式
- iOS常用的几种设计模式
- 几种常用的设计模式介绍
- 几种常用的设计模式介绍
- 几种常用的设计模式介绍
- 制作ZedBoard-linaro-desktop-ubuntu全过程之编译Linux内核
- Win7 安装VS2010, .net framework 4.0 失败,错误HRESULT 0xc8000222解决办法
- Linux中遇到问题时常用命令
- SQL SERVER 数据库备份压缩机制
- 【JAVA】Properties文件以及XML文件的读取操作
- 几种常用的设计模式代码
- 设计模式——装饰模式
- codeforces 411div2-fake NP
- 10. Regular Expression Matching
- 配置_eclipse:项目在myeclipse与eclipse之间迁移
- 9-const和volatile分析
- Coscos2d初学的一些知识点01
- 解决linux服务器CentOS(6和7版本),/etc/sysconfig/下没有iptables的问题
- log4j2打印Mybatis执行的SQL语句及SQL语句的执行时间