更新时间:2021-08-09 21:24:42
工厂方法模式分为三种 :普通工厂模式,多个工厂方法模式,静态工厂方法模式
--- 发送邮件和短信
- 接口
public interface Sender{
public void Send();
}
- 实现类
public class MailSender implements Sender{
@Override
public void Send(){
System.out.println("MailSender Method");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class SmsSender implements Sender{
@Override
public void Send(){
System.out.println("SmsSender Method");
}
}
- 工厂类
public class SendFactory{
public Sender produce(String type){
if("mail".equals(type)){
return new MailSender();
}else if("sms".equals(type)){
return new SmsSender();
}else{
System.out.println("Please input right type!");
}
}
}
- 测试类
public class FactoryTest{
public static void main(String[] args){
SendFactory factory=new SendFactory();
Sender sender=factory.produce("sms");
sender.Send();
}
}
- SendFactory类
public class SendFactory{
public Sender produceMail(){
return new MailSender();
}
public Sender produceSms(){
return new SmsSender();
}
}
- 测试类
public class FactoryTest{
public static void main(String[] args){
SendFactory factory=new SendFactory();
Sender sender=factory.produceMail();
sender.Send();
}
}
- SendFactory
public class SendFactory{
public static Sender produceMail(){
return new MailSender();
}
public static Sender produceSms(){
return new SmsSender();
}
}
- FactoryTest
public class FActoryTest{
public static void main(String[] args){
Sender sender=SenderFactory.produceMail();
sender.Send();
}
}
工厂模式适合出现大量的产品需要创建,并且具有共同的接口,可以通过工厂方法模式创建:
- Sender
public interface Sender{
public void Sender();
}
- 两个实现类
- MailSender
public class MailSender implements Sender {
@Override
public void Send(){
System.out.println("This is MailSender!");
}
}
- SmsSender
public class SmsSender implements Sender{
@Override
public void Send(){
System.out.println("This is SmsSender!");
}
}
- 两个工厂类
- 工厂类接口:
public interface Provider{
public Sender produce();
}
- SendMailFactory
public class SendMailFactory implements Provider{
@Override
public Sender produce(){
return new MailSender();
}
}
- SendSmsFactory
public class SendSmsFactory implements Provider{
@Override
public Sender produce(){
return new SmsSender();
}
}
- Test
public class Test{
public static void main(String[] args){
Provider provider=new SendMailFactory();
Sender sender=provider.produce();
sender.Send();
}
}
抽象工厂模式的优点就是拓展性强:
如果需要增加一个功能,例如:发及时信息
单例模式的优点:
- 单例类
public class Singleton{
/* 私有静态实例,防止被引用,赋值为null,目的是实现延迟加载 */
private static Singleton instance=null;
/* 私有构造方法,防止被实例化 */
private Singleton(){
}
/* 静态工厂方法,创建实例 */
public static Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
/* 如果该对象被用于序列化,可以保证对象在序列化前后保持一致 */
public Object ReadResolve(){
return instance;
}
}
public static synchronized Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
由于synchronized锁住的是这个对象,这样的用法,每次调用getInstance(),都要对对象上锁,在性能上会有所下降.
public static Singleton getInstance(){
if(instance==null){
synchronized(instance){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
这样似乎解决了问题,将synchronized关键字加入内部,这样在调用的时候是不需要加锁的,只有在instance为null,并创建对象的时候才需要的加锁,性能得到了提升,但是这样的情况还是有问题的
存在这样的情况:
JVM并不保证这两个操作的先后顺序:
示例:
A,B两个线程
private static class SingletonFactory{
private static Singleton instance=new Singleton();
}
public static Singleton getInstance(){
return SingletonFactory.instance;
}
实际情况是:
public class Singleton{
/* 私有构造方法,防止被实例化 */
private Singleton(){}
/* 使用内部类维护单例 */
private static class SingletonFactory{
private static Singleton instance=new Singleton();
}
/* 获取实例 */
public static Singleton getInstance(){
return SingletonFactory.instance;
}
/* 如果该对象被序列化,可以保证对象在序列化前后保持一致 */
public Object readResolve(){
return getInstance();
}
}
这种方法,如果在构造函数中抛出异常,实例将永远不会创建,也会出错.
只能根据实际场景,选择最适合应用场景的实现方法
public class SingletonTest{
private static SingletonTest instance=null;
private SingletonTest(){}
private static synchronized void syncInit(){
if(instance==null){
instance=new SingletonTest();
}
}
public static SingletonTest getInstance(){
if(instance==null){
syncInit();
}
return instance;
}
}
public class SingletonTest{
private static SingletonTest instance=null;
private Vector properties=null;
public Vector getProperties(){
return properties;
}
private SingletonTest(){}
private static synchronized void syncInit(){
if(instance==null){
instance=new SingletonTest();
}
}
public static SingletonTest getInstance(){
if(intance==null){
syncInit();
}
return instance;
}
public void updateProperties(){
SingletonTest shadow=new SingletonTest();
properties=shadow.getProperties();
}
}
单例模式的特点:
单例模式理解起来简单,具体实现起来是有一定难度的\
synchronized关键字锁定的是对象,使用的时候要在恰当的地方使用:
采用类的静态方法,可以实现单例模式的效果
类的静态方法和单例模式的区别:
静态类不能实现接口:
单例可以被延迟启动:
单例可以被继承:
单例比较灵活:
建造者模式: 将各种产品集中起来进行管理,用来创建复合对象
- Builder
public class Builder{
private List<Sender> list=new ArrayList<Sender>();
public void produceMailSender(int count){
for(int i=0;i<count;i++){
list.add(new MailSender());
}
}
public void produceSmsSender(int count){
for(int i=0;i<count;i++){
list.add(new SmsSender());
}
}
}
- 测试类
public class Test{
public static void main(String[] args){
Builder builder=new Builder();
builder.produceMailSender(10);
}
}
建造者模式和工厂模式的区别:
- 原型类
public class Prototype implements Cloneable{
public Object clone() throws CloneNotSupportedException{
Prototype proto=(Prototype)super.clone();
return proto;
}
}
重点是super.clone():
对象的深复制和浅复制:
深复制:
浅复制:
public class Prototype implements Cloneable,Serializable{
private static final long serialVersionUID=1L;
private String string;
private SerializableObject obj;
/* 浅复制 */
public Object clone() throws CloneNotSupportedException{
Prototype proto=(Prototype)super.clone();
return proto;
}
/* 深复制 */
public Object clone() throws IOException,ClassNotFoundException{
/* 写出当前对象的二进制流 */
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(this);
/* 读入二进制流产生的新对象 */
ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
OnjectInputStream ois=new ObjectInputStream(bis);
return ois.readObject();
}
public String getString(){
return string;
}
public void setString(String string){
this.string=string;
}
public SerializableObject getObj(){
return obj;
}
public void setObj(SerializableObject obj){
this.obj=obj;
}
}
class SerializableObject implements Serializable{
private static final long serialVersionUID=1L;
}
要实现深复制: