Android 设计模式
来源:互联网 发布:批处理软件 编辑:程序博客网 时间:2024/06/05 17:54
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.Build模式
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); } }
调用方法:
Person.Builder builder=new Person.Builder();Person person=builder .name("张三") .age(18) .height(178.5) .weight(67.4) .build();
阅读全文
0 0
- Android设计模式--适配器模式
- android设计模式--外观模式
- android设计模式-策略模式
- android设计模式-观察者模式
- android设计模式-工厂模式
- android设计模式-适配器模式
- android设计模式-组合模式
- android 设计模式 状态模式
- android 设计模式-组合模式
- Android 设计模式-----单利模式
- Android设计模式--观察者模式
- Android设计模式--适配器模式
- Android设计模式-适配器模式
- Android设计模式-适配器模式
- Android设计模式-常用模式
- [Android] 设计模式-策略模式
- Android设计模式--Builder模式
- Android设计模式-适配器模式
- 全是1的最大子矩阵
- 激活函数总结(一)
- Linux网络性能评估工具iperf
- 代码3 爬取贴吧多页数据
- redis的使用
- Android 设计模式
- Java初级入门
- NS2整体实现机制(转)
- 解决滑动条控件,子物体减少,滑动条不刷新的bug
- Java面试题二:基础语法(续面试题一)
- EventBus使用
- spring 底层实现IOC DI简单依赖反射
- mysql安装与基本操作
- 隐藏QT窗口在任务栏中的图标