设计模式

1.单例模式:

  饿汉式:

    实例化好了直接调用:

    

 1 public class Car{
 2 
 3        private  static Car  car = new Car(); 
 4 
 5        public static Car getCar(){
 6 
 7          return car        
 8 
 9        }
10 
11     }

懒汉式:

  什么时候用是么时候实例化;

  

 1 public class Car{
 2 
 3        private  static Car  car; 
 4 
 5        public static Car getCar(){
 6 
 7         if(car == null){
 8 
 9           car = new Car()
10 
11         }
12 
13          return car        
14 
15        }
16 
17     }

懒汉式:在静态方法中初始化。时间换空间。(不推荐,时间很重要)

饿汉式:在声明对象就初始化。空间换时间。(推荐,空间不是问题)

 

2.工厂模式:

  

程序在接口和子类之间加入了一个过渡端,通过此过渡端可以动态取得实现了共同接口的子类实例化对象。

      示例代码如下:

 

 1 public interface Student {
 2     //定义一个接口
 3     public void stady();//定义一个公共的方法
 4 }
 5 
 6 
 7 public class Xiaohong implements Student {
 8 //定义一个子类Xiaohong 继承接口
 9     @Override
10     public void stady() {
11         System.out.println("小红在学习");
12     }//重写方法
13 
14 }
15 
16 
17 public class Xiaoming implements Student {
18 //定义一个子类Xiaoming 继承接口
19     @Override
20     public void stady() {
21         System.out.println("小明在学习");
22     }//重写方法
23     
24 }
25 
26 
27 public class Fatory {//定义工厂类
28     public static Student getinterface(String Name) {
29         Student s = null;//定义接口
30         
31         if("Xiaoming".equals(Name)) {
32             s = new Xiaoming();
33         }
34         if("Xiaohong".equals(Name)) {
35             s = new Xiaohong();
36         }
37         return s;
38               //判断并实例化
39     }
40 }    
41 
42 
43 public class FatoryDamo {
44     public static void main(String[] args) {
45         Student s = null;//定义接口对象
46         s = Fatory.getinterface(args[0]);//调用工厂方法
47         if(s != null) {
48             s.stady();//调用方法
49         }
50     }
51 }

3.适配器设计模式:

  

适配器模式的一般定义:某类继承这个适配器,从而实现我们需要实现的方法。

实现思路:通过写一个适配器类,里面写了所有的抽象方法,但是这些方法是空的,并且适配器类要定义成抽象的,如果适配器类可以自己实现就没有意义了。适配器的作用,继承适配器,重新需要重新的方法就可以了,简化操作。

 优点
    * 客户端不需要在负责对象的创建,从而明确了各个类的职责,如果有新的对象增加,只需要增加一个具体的类和具体的工厂类即可,不影响已有的代码,后期维护容易,增强了系统的扩展性。
缺点
    * 需要额外的编写代码,增加了工作量;
public interface Student {

    void math();
    void English();
    
}//写一个接口


ublic class Techer implements Student {
//写一个类继承Student接口,但不重写方法
    @Override
    public void math() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void English() {
        // TODO Auto-generated method stub
        
    }
    
}



public class Xiaoming extends Techer {
    //写个类继承Techer类
    public void math() {
        System.out.println("小明老师教数学");
        //重写math方法
    } 
}


public class Inclass {
    public static void main(String[] args) {
        Xiaoming x = new Xiaoming();
        x.math();//实例化对象调用重写的方法
    }
}

 

4.模板设计模式(常用):
 

模板设计模式的一般定义:定义一个算法骨架将具体实现交给子类去实现。

实现方法:在类中定义一个抽象方法,距离实现交由子类去实现

 

public interface Student {
    
    void English();
    
}


public class ClassName implements Student {

    @Override
    public void English() {
        System.out.println("班在上英语");
        
    }
    
}

 

 

 

 

 

 

 

 

 

posted @ 2018-08-31 23:22  k丶灵  阅读(150)  评论(0编辑  收藏  举报