java设计模式记录

///创建型模式/

1.抽象工厂模式:

对工厂类抽象一个接口出来,分别对每一个需要工厂类实例化的实现类进行实例化。

2.单例模式:

public class Singleton {    
 /* 私有构造方法,防止被实例化 */  
 private Singleton() {  
 } 
   /* 此处使用一个内部类来维护单例 */  
   private static class SingletonFactory {  
       private static Singleton instance = new Singleton();  
   }  
 
   /* 获取实例 */  
   public static Singleton getInstance() {  
       return SingletonFactory.instance;  
   }
   
   //本类的其他实现功能的方法
   
}

3.建造者模式

建造者模式是对抽象工厂模式的延伸,用于创建大量对象出来。

 public void produceMailSender(int count){  
       for(int i=0; i<count; i++){  
           list.add(new MailSender());  
       }  
   } 

这个抽象工厂接收创建实例个数的参数,进行大量的实例创建。

4.原型模式

实现原型模式,首先要得实现原型空接口(Cloneable),覆写clone()方法。基本上可以分为浅克隆和深克隆。

将一个对象复制后,基本数据类型的变量都会重新创建,而引用类型,指向的还是原对象所指向的
public class Prototype implements Cloneable {  
 
    public Object clone() throws CloneNotSupportedException {  
        Prototype proto = (Prototype) super.clone();  //调用native的clone()实现浅克隆就可以了
        return proto;  
    }  
}  

将一个对象复制后,不论是基本数据类型还有引用类型,都是重新创建的。简单来说,就是深复制进行了完全彻底的复制,而浅复制不彻底。

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 deepClone() throws IOException, ClassNotFoundException {  
  
        /* 写入当前对象的二进制流 */  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
        ObjectOutputStream oos = new ObjectOutputStream(bos);  
        oos.writeObject(this);  
  
        /* 读出二进制流产生的新对象 */  
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
        ObjectInputStream ois = new ObjectInputStream(bis);  
        return ois.readObject();  
    }  
}

将当前需要克隆的对象序列化,再读出来返回回去。

//结构型模式

5.适配器模式

a.类的适配器模式:c类继承a类实现b类,这时候c类同时有了a和b类的所有方法。

A类

public class Source {  
 
    public void method1() {  
        System.out.println("this is original method!");  
    }  
}

B类接口:

public interface Targetable {  
    /* 新类的方法 */  
    public void method2();  
}  

C类:

public class Adapter extends Source implements Targetable {  
 
    @Override  
    public void method2() {  
        System.out.println("this is the targetable method!");  
    }  

b.接口级别的适配器,c类实现b类,再用一个方法接收a类实例,把a类实例的方法暴露出来。

public class Wrapper implements Targetable {  
 
    private Source source;  
      
    public Wrapper(Source source){  
        super();  
        this.source = source;  
    }  
    @Override  
    public void method2() {  
        System.out.println("this is the targetable method!");  
    }  
  
    @Override  
    public void method1() {  
        source.method1();  
    }  
}   

测试:

public class AdapterTest {  
 
    public static void main(String[] args) {  
        Source source = new Source();  
        Targetable target = new Wrapper(source);  
        target.method1();  
        target.method2();  
    }  
}

c:接口的适配器

有接口a和接口b,一个抽象类d实现接口a的方法(空方法),然后用不同的类继承抽象类,重写抽象类中需要的方法。

抽象类d,实现空方法

public abstract class Wrapper2 implements Sourceable{  
    
    public void method1(){}  
    public void method2(){}  
}

public class SourceSub1 extends Wrapper2 {  
    public void method1(){  
        System.out.println("the sourceable interface's first Sub1!");  
    }  
}

public class SourceSub2 extends Wrapper2 {  
    public void method2(){  
        System.out.println("the sourceable interface's second Sub2!");  
    }  
}   



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值