Android 設計模式——觀察者模式,單例模式,Build模式,原型模式,命令模式
1、實現Build步驟
1、建立實體類 2、建立實體內部build類 3、實體內部類返回實體類 4、實體類建立內部類建構函式 /** * 作者:created by meixi * 郵箱:[email protected] * 日期:2018/12/14 11 */ public class PersonBuildertest { private String name; private int idd; public PersonBuildertest(Builder builder) { this.name = builder.name; this.idd = builder.idd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIdd() { return idd; } public void setIdd(int idd) { this.idd = idd; } public static class Builder { private String name; private int idd; public Builder name(String namein) { this.name = namein; return this; } public Builder idd(int id){ this.idd =id; return this; } public PersonBuildertest build(){ return new PersonBuildertest(this); } } }
使用方法:
PersonBuildertest buildertest = builder.name("那麼").idd(2).build();
Log.i("lgq","builertttt==="+buildertest.getName());
12-14 13:45:03.313 21756-21756/? I/lgq: builertttt===那麼
2、原型模式
實現步驟
1、實現Cloneable介面
2、重寫Object的clone方法
3、實現clone方法中的拷貝邏輯
public class Clonetest implements Cloneable{ private String name; private int age; public Clonetest(){ } 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; } @Override public Object clone(){ Clonetest person=null; try { person=(Clonetest)super.clone(); person.name=this.name; person.age=this.age; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; } }
使用方法
Clonetest p=new Clonetest();
p.setAge(18);
p.setName("張三");
Clonetest p1= (Clonetest) p.clone();
p1.setName("李四");
Log.i("lgq","builertttt==="+p1.getName()+"...."+p1.getAge());
12-14 13:45:03.313 21756-21756/? I/lgq: builertttt===李四....18
3、單例模式的實現步驟
1、靜態例項
2、私有化構造方法
3、靜態獲取例項的函式,雙重null判斷synchronize同步處理——
使用synchronized 進行同步處理,並且雙重判斷是否為null,我們看到synchronized (Singleton.class)裡面又進行了是否為null的判斷,這是因為一個執行緒進入了該程式碼,如果另一個執行緒在等待,這時候前一個執行緒建立了一個例項出來完畢後,另一個執行緒獲得鎖進入該同步程式碼,例項已經存在,沒必要再次建立,因此這個判斷是否是null還是必須的。
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;
}
}
4、觀察者模式
主要三個類
1、
public interface Observer {
void update(int id,String param);
}
2、
public interface Subject {
/**
* 註冊一個觀察者
*/
void addObserver(Observer observer);
/**
* 刪除一個觀察者
*
* @return 是否刪除成功
*/
boolean removeObserver(Observer observer);
/**
* 通知所有的觀察者
*
* @param id 對應觀察者的id
*/
void notifyObserver(int id,String param);
}
3、
public class TestObServernotice implements Subject {
private static Subject subject;
private List<Observer> observers = new ArrayList<>();
private TestObServernotice() {
// TODO Auto-generated constructor stub
}
public static Subject getInstance() {
if (null == subject) {
synchronized (TestObServernotice.class) {
if (null == subject)
subject = new TestObServernotice();
}
}
return subject;
}
@Override
public void addObserver(Observer observer) {
// TODO Auto-generated method stub
observers.add(observer);
}
@Override
public boolean removeObserver(Observer observer) {
// TODO Auto-generated method stub
return observers.remove(observer);
}
@Override
public void notifyObserver(int id,String param) {
// TODO Auto-generated method stub
for (Observer observer : observers) {
observer.update(id,param);
}
}
}
觀察者中註冊Observer,如MainActivity中註冊
private Observer deleteObserver = new Observer() {
@Override
public void update(int id,String param) {
tomapte.setText("sssssymd"+param);//dosomething
}
};
TestObServernotice.getInstance().addObserver(deleteObserver);
同時注意刪除觀察者
@Override
protected void onDestroy() {
super.onDestroy();
TestObServernotice.getInstance().removeObserver(deleteObserver);
deleteObserver = null;
Log.v("lgq", "......11..onDestroy....");
}
呼叫,通知觀察者,主要在receiver中呼叫,fragment,activity都可以呼叫,跨介面重新整理資料
TestObServernotice.getInstance().notifyObserver(0,bundle.getString("cn.jpush.android.ALERT"));
5、命令模式
實現步驟:
(1)建立命令接受者:
//命令接收者Receiver public class Tv { public int currentChannel = 0; public void turnOn() { System.out.println("lgqThe televisino is on."); } public void turnOff() { System.out.println("The television is off."); } public void changeChannel(int channel) { this.currentChannel = channel; System.out.println("Now TV channel is " + channel); } }
(2)定義命令的介面,宣告執行的方法。
//執行命令的介面 public interface Command { void execute(); }
(3)命令介面實現物件
//開機命令ConcreteCommand,獲取接受者類,實現命令 public class CommandOn implements Command { private Tv myTv; public CommandOn(Tv tv) { myTv = tv; } public void execute() { myTv.turnOn(); } }
(4)要求命令物件執行請求,通常會持有命令物件
//可以看作是遙控器Invoker public class Control { private Command onCommand, offCommand, changeChannel; public Control(Command on) { onCommand = on; } public void turnOn() { onCommand.execute(); } // public void turnOff() { // offCommand.execute(); // } // // public void changeChannel() { // changeChannel.execute(); // } }
(5)組裝命令物件和接收者
// 命令接收者Receiver Tv myTv = new Tv(); // 開機命令ConcreteCommond CommandOn on = new CommandOn(myTv); // on.execute(); // 命令控制物件Invoker,組裝接受者,命令物件 Control control = new Control(on); // 開機 control.turnOn();