定义:(拷贝)
用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
通过拷贝复制出一个新的对象。(拷贝)最简单的设计模式。又分为浅拷贝和深拷贝
浅拷贝: 浅拷贝,就是类的类对象实例,是没有被拷贝的,他们还是公用一份
- user代码
public class User implements Cloneable {
public String userName;
public int age;
public Address userAddress;//类的对象实例
@Override
protected User clone() throws CloneNotSupportedException {
return (User) super.clone();
}
public static class Address {
public String addressName;
public String city;
public Address(String addressName, String city) {
this.addressName = addressName;
this.city = city;
}
}
}
- 测试
public class Client {
public static void main(String[] args) {
User user=new User();
user.age=18;
user.userName="Peakmain";
user.userAddress=new User.Address("安徽合肥","合肥");
//浅拷贝
try {
User copyUser = user.clone();
//修改地址和名字
copyUser.userName="Treasure";
copyUser.userAddress.addressName="上海静安区";
// 浅拷贝,就是类的类对象实例,是没有被拷贝的,他们还是公用一份
System.out.print("姓名:"+user.userName+" 地址:"+user.userAddress.addressName+"\n");
System.out.print("姓名:"+copyUser.userName+" 地址:"+copyUser.userAddress.addressName);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
深克隆:将地址也进行克隆
- user代码
public class User implements Cloneable {
public String userName;
public int age;
public Address userAddress;
@Override
protected User clone() throws CloneNotSupportedException {
User user = (User) super.clone();
user.userAddress= userAddress.clone();
return user;
}
public static class Address implements Cloneable{
public String addressName;
public String city;
public Address(String addressName, String city) {
this.addressName = addressName;
this.city = city;
}
@Override
protected Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
}
}
测试代码没有变, 测试代码中 User copyUser = user.clone();调用的是User的克隆所以要在内部调用地址(Address)的克隆
android中运用模式的有
Intent
比如有三个activity,从activity1启动到activity2,在启动activity3,而数据需要从activity1中传递到activity3中activity1:
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("Params1","Params1");
intent.putExtra("Params2","Params2");
intent.putExtra("Params3","Params3");
startActivity(intent);
activity2:
//默认
/* String Params1 = getIntent().getStringExtra("Params1");
String Params2 = getIntent().getStringExtra("Params2");
String Params3 = getIntent().getStringExtra("Params3");
// 又 new 一个 intent
// 把参数传递
Intent intent = new Intent(this,Activity3.class);
intent.putExtra("Params1",Params1);
intent.putExtra("Params2",Params2);
intent.putExtra("Params3",Params3);*/
//原型设计模式拷贝
Intent intent = (Intent) getIntent().clone();
intent.setClass(this, Activity3.class);
startActivity(intent);
源码
@Override
public Object clone() {
return new Intent(this);
}
public Intent(Intent o) {
//赋值
this.mAction = o.mAction;
this.mData = o.mData;
this.mType = o.mType;
this.mPackage = o.mPackage;
this.mComponent = o.mComponent;
this.mFlags = o.mFlags;
this.mContentUserHint = o.mContentUserHint;
}
2 . ArrayList
ArrayList list=new ArrayList();
// 有很多数据
ArrayList copyList = (ArrayList) list.clone();
源码:
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
//数组拷贝
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;//重置修改次数
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
3 . OkHttp
OkHttpClient client = new OkHttpClient();
client.newBuilder();
源码:
Builder(OkHttpClient okHttpClient) {
this.dispatcher = okHttpClient.dispatcher;
this.proxy = okHttpClient.proxy;
this.protocols = okHttpClient.protocols;
this.connectionSpecs = okHttpClient.connectionSpecs;
this.interceptors.addAll(okHttpClient.interceptors);
this.networkInterceptors.addAll(okHttpClient.networkInterceptors);
this.eventListenerFactory = okHttpClient.eventListenerFactory;
this.proxySelector = okHttpClient.proxySelector;
this.cookieJar = okHttpClient.cookieJar;
this.internalCache = okHttpClient.internalCache;
this.cache = okHttpClient.cache;
this.socketFactory = okHttpClient.socketFactory;
this.sslSocketFactory = okHttpClient.sslSocketFactory;
this.certificateChainCleaner = okHttpClient.certificateChainCleaner;
this.hostnameVerifier = okHttpClient.hostnameVerifier;
this.certificatePinner = okHttpClient.certificatePinner;
this.proxyAuthenticator = okHttpClient.proxyAuthenticator;
this.authenticator = okHttpClient.authenticator;
this.connectionPool = okHttpClient.connectionPool;
this.dns = okHttpClient.dns;
this.followSslRedirects = okHttpClient.followSslRedirects;
this.followRedirects = okHttpClient.followRedirects;
this.retryOnConnectionFailure = okHttpClient.retryOnConnectionFailure;
this.connectTimeout = okHttpClient.connectTimeout;
this.readTimeout = okHttpClient.readTimeout;
this.writeTimeout = okHttpClient.writeTimeout;
this.pingInterval = okHttpClient.pingInterval;
}