单例模式
确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
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;
}
}
Build模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
如果一个类的构造需要很多参数,而且这些参数并不都是必须的,那么这种情况下就比较适合Builder。
假设有一个Person类,我们通过该Person类来构建一大批人,这个Person类里有很多属性,最常见的比如name,age,weight,height等等,并且我们允许这些值不被设置,那么我们将需要重载很多构造方法。
Person增加一个静态内部类Builder类,并修改Person类的构造函数,代码如下。
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对象时
Person.Builder builder=new Person.Builder();
Person person=builder
.name("张三")
.age(18)
.height(178.5)
.weight(67.4)
.build();
常见的AlertDialog,OkHttp等就使用了build模式
观察者模式
定义对象间的一种一对多的依赖关系,当一个对象的状态发送改变时,所有依赖于它的对象都能得到通知并被自动更新
public class Observable
List
public void register(Observer
if (observer == null) {
throw new NullPointerException("observer == null");
}
synchronized (this) {
if (!mObservers.contains(observer))
mObservers.add(observer);
}
}
public synchronized void unregister(Observer
mObservers.remove(observer);
}
public void notifyObservers(T data) {
for (Observer
observer.onUpdate(this, data);
}
}
}
定义一个接口
public interface Observer
void onUpdate(Observable
}
用法
public class Main {
public static void main(String [] args){
Observable
Observer
@Override
public void onUpdate(Observable
System.out.println("观察者1:"+data);
}
};
Observer
@Override
public void onUpdate(Observable
System.out.println("观察者2:"+data);
}
};
observable.register(observer1);
observable.register(observer2);
observable.notifyObservers("发布消息1");
observable.notifyObservers("发布消息2");
observable.unregister(observer1);
observable.notifyObservers("发布消息3");
}
}
原型模式
用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 更快的获取到一个相同属性的对象,内容与原型一样,内存地址i不一样。
首先我们定义一个Person类 实现Cloneable接口重写clone方法
public class Person implements Cloneable {
private String name;
private String age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
protected Object clone() {
Person person=null;
try {
person=(Person)super.clone();
person.name=this.name;
person.age=this.age;
person.gender=this.gender;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person;
}
}
Person person = new Person();
person.setAge("20");
person.setGender("男");
person.setName("赞张");
Person person1 = (Person) person.clone();
此时person和person1相同的内容 但是内存指向不一样
代理模式
提供了一个新的对象,实现了对真实对象的操作,或成为真实对象的替身.
A找中介租房为Demo来构建代理模式,先定义一个抽象主题,IHouse
public interface IHouse {
void getHouseInfo();
void signContract();
void payFees();
}
定义真实主题
public class House implements IHouse{
private final String TAG = House.class.getSimpleName();
private String name;
private double price;
public House(String name, double price){
this.name = name;
this.price = price;
}
@Override
public void getHouseInfo() {
Log.i(TAG, "House Info- name:" + name + " ¥:" + price);
}
@Override
public void signContract() {
Log.i(TAG, "Contract:" + name + " signed at" +
new SimpleDateFormat("HH:mm:ss").format(SystemClock.uptimeMillis()));
}
@Override
public void payFees() {
Log.i(TAG, "Bill: name-" + name + " $-" + price);
}
}
定义房屋代理,同样需要实现IHouse接口
public class ProxyHouse implements IHouse{
private final String TAG = ProxyHouse.class.getSimpleName();
private IHouse house;
public ProxyHouse(IHouse house){
this.house = house;
}
@Override
public void getHouseInfo() {
Log.i(TAG, "searching");
house.getHouseInfo();
Log.i(TAG, "search finished");
}
@Override
public void signContract() {
Log.i(TAG, "prepare contract");
house.signContract();
}
@Override
public void payFees() {
house.payFees();
}
}
对于客户来说,完全不用跟House进行直接交互,而是跟代理交互
IHouse house = new House("Downton Abbey", 5000);
IHouse proxyHouse = new ProxyHouse(house);
Log.i(TAG, "looking for a perfect house");
proxyHouse.getHouseInfo();
Log.i(TAG, "thinking");
proxyHouse.signContract();
proxyHouse.payFees();
Log.i(TAG, "so easy");
工厂者模式:Factory
1.创建抽象产品类,定义公共接口:
public interface Product {
void show();
}
2.创建具体产品类,实现Product接口:
public class ProductA implements Product {
@Override
public void show() {
System.out.println("product A");
}
}
public class ProductB implements Product {
@Override
public void show() {
System.out.println("product B");
}
}
3.创建工厂类,创建具体的产品
public class Factory {
public static Product create(String productName) {
Product product = null;
//通过switch语句控制生产哪种商品
switch (productName) {
case "A":
product = new ProductA();
break;
case "B":
product = new ProductB();
break;
}
return product;
}
}
第3步也可以通过反射得到具体的产品类:
public Product create(Class extends Product> clazz) {
Product product = null;
try {
product = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return product;
}
测试:
Factory.create("A").show();//生产ProductA
Factory.create(ProductB.class).show();//生产ProductB