设计模式

一、概念

设计模式是一套被反复使用、多数人知晓的、经过分类编目、代码设计经验的总结。

使用设计模式是为了可重用代码、让代码更容易被他人理解。保证代码的可靠性

 

二、设计模式的分类

  • 创建型模式   对象的创建
    简单工厂模式、工厂方法模式、单例模式、抽象工厂模式、建造者模式、原型模式
  • 结构性模式   对象的组成(结构)
  • 行为型模式   对象的行为

 

三、创建型模式

1、简单工厂模式

又叫静态工厂方法模式,它定义了一个具体的工厂类负责创建一些类的实例

  • 优点:客户端不需要再负责对象的创建,从而明确了各个类的职责
  • 缺点:这个静态工厂类负责所有对象的创建,如果有新的对象增加,或者某些对象的创建方式不同,就需要不断的修改工厂类,不利于后期的维护

举例:一个动物工厂类,负责创建狗和猫

抽象动物类:

public abstract class Animal {
    public abstract void eat();
}

狗类:

public class Dog extends Animal {
    public void eat() {
        System.out.println("狗吃肉");
    }
}

猫类:

public class Cat extends Animal {
    public void eat() {
        System.out.println("猫吃鱼");
    }

}

动物工厂类:

public class AnimalFactory {
    private AnimalFactory() {
    }
    public static Animal createAnimal(String type){
        if("dog".equals(type))
            return new Dog();
        if("cat".equals(type))
            return new Cat();
        else
            return null;
    }
}

测试类:

public class AnimalDemo {
    public static void main(String[] args) {
        Animal a = AnimalFactory.createAnimal("dog");
        a.eat();
        a = AnimalFactory.createAnimal("cat");
        if (a != null) {
            a.eat();
        }else{
            System.out.println("暂时不提供这种动物");
        }
    }
}

 

2、工厂方法模式

工厂方法模式中抽象工厂类负责定义创建对象的接口,具体对象的创建工作由继承抽象工厂的具体类实现缺点

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

举例:创建动物工厂,下面有各个动物类的子工厂

抽象动物类:

public abstract class Animal {
    public abstract void eat();
}

工厂类(接口):

public interface Factory {
    public abstract Animal createAnimal();
}

 

狗类:

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }

}

狗工厂类:

public class DogFactory implements Factory {

    public Animal createAnimal() {
        return new Dog();
    }
} 

猫类:

public class Cat extends Animal {
    public void eat() {
        System.out.println("猫吃鱼");

    }

}

猫工厂类:

public class CatFactory implements Factory {

    public Animal createAnimal() {
        return new Cat();
    }

}

测试类:

public class AnimalDemo {
    public static void main(String[] args) {
        //需求:我要买只狗
        Factory f = new DogFactory();
        Animal a = f.createAnimal();
        a.eat();
        //需求:我要买只猫
        f=new CatFactory();
        a = f.createAnimal();
        a.eat();
    }
}

 

3、单例模式

(1)单例模式之饿汉式:类一加载就创建对象

概述:单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供

优点:在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象单例模式无疑可以提高系统的性能

缺点:没有抽象层,因此扩展很难

职责过重,在一定程度上违背了单一职责

举例:创建学生对象

public class Student {
    //构造私有
    private Student() {
    }

    //自己造一个对象
    //静态方法只能访问静态成员变量
    //为了不让外界直接访问修改这个值,加private
    private static Student s = new Student();

    //提供公共的访问方式
    public static Student getStudent(){
        return s;
    }
}

测试类:

/*
 * 单例模式:保证类在内存中只有一个对象
 * 如何保证类在内存中只有一个对象呢?
 *   A:把构造方法私有
 *   B:在成员位置自己创建一个对象
 *   C:通过一个公共的方法提供访问
 *   
 * */
public class StudentDemo {
    public static void main(String[] args) {
        Student s1 = Student.getStudent();
        Student s2 = Student.getStudent();
        System.out.println(s1 == s2);  // true
    }
}

 

(2)单例模式之懒汉式:用的时候才创建对象

老师类:

public class Teacher {
    private Teacher(){}
    private static Teacher t = null;
    public static Teacher getTeacher(){
        if(t==null)
             t = new Teacher();
        return t;
    }
}

测试类:

public class TeacherDemo {
    public static void main(String[] args) {
        Teacher t1 = Teacher.getTeacher();
        Teacher t2 = Teacher.getTeacher();
        System.out.println(t1=t2);  c
    }
}

 

面试题:单例模式的思想?请写一个代码体现

开发:饿汉式(不会出问题的单例模式)

面试:懒汉式(可能会出问题的单例模式)

A:懒加载(延迟加载)

B:线程安全问题:

  • 是否是多线程环境     是
  • 是否有共享数据          是(t 对象)
  • 是否有多条语句操作共享数据      是

改进后的代码:加上sysnchronized关键字

public class Teacher {
    private Teacher(){}
    private static Teacher t = null;
    
    public synchronized static Teacher getTeacher(){
        if(t==null)
             t = new Teacher();
        return t;
    }
}

 

举例:Runtime类 单例模式实现

public class Runtime {
    private static Runtime currentRuntime = new Runtime();
    public static Runtime getRuntime() {
        return currentRuntime;
    }
    private Runtime() {}
}

举例:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接

exec方法:在单独的进程中执行指定的字符串命令。

import java.io.IOException;
public class RuntimeDemo {
    public static void main(String[] args) throws IOException {
        Runtime r = Runtime.getRuntime();
        r.exec("calc");
    }
}

 

posted @ 2018-09-14 16:59  谁叫土豆豆豆  阅读(156)  评论(0编辑  收藏  举报