您的位置:首页 > 编程语言 > Java开发

java设计模式记录

2017-08-02 11:43 330 查看
///////////////////////////创建型模式/////////////////////////////////////

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!");  

    }  

}   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java设计模式