设计模式(Design pattern)是一套被反复使用的代码设计经验的总结。使用设计模式的目的是为了可重用代码让代码更容易被他人理解
设计模式的运用,有助于代码的维护与拓展。任何模式的出现,都是为了解决一些特定的场景的耦合问题,以达到对修改封闭,对扩展开放的效果。
1、实现Build步骤
1、创建实体类2、创建实体内部build类3、实体内部类返回实体类4、实体类创建内部类构造函数/*** 作者:created by meixi* 邮箱:15913707499@163.com* 日期: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;}@Overridepublic 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;
}
}
activity管理类
public class ActivityManager { private static volatile ActivityManager instance; private Stack<Activity> mActivityStack = new Stack<Activity>(); private ActivityManager(){ } public static ActivityManager getInstance(){ if (instance == null) { synchronized (ActivityManager.class) { if (instance == null) { instance = new ActivityManager(); } } return instance; } public void addActicity(Activity act){ mActivityStack.push(act); } public void removeActivity(Activity act){ mActivityStack.remove(act); } public void killMyProcess(){ int nCount = mActivityStack.size(); for (int i = nCount - 1; i >= 0; i--) { Activity activity = mActivityStack.get(i); activity.finish(); } mActivityStack.clear(); android.os.Process.killProcess(android.os.Process.myPid()); }
}
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)组装命令对象和接收者
// 命令接收者ReceiverTv myTv = new Tv();// 开机命令ConcreteCommondCommandOn on = new CommandOn(myTv);// on.execute();// 命令控制对象Invoker,组装接受者,命令对象Control control = new Control(on);// 开机control.turnOn();