A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

编程尽量面像接口,抽象类。
单一职责原则: 其实就是开发人员经常说的”高内聚,低耦合
开闭原则 核心思想是:一个对象对扩展开放,对修改关闭。
依赖注入原则 核心思想:要依赖于抽象,不要依赖于具体实现。
接口分离原则 核心思想:不应该强迫程序依赖它们不需要使用的方法。
迪米特原则 核心思想:一个对象应当对其他对象尽可能少的了解 其实就是说:性降低各个对象之间的耦合,提高系统的可维护性。
提高代码的维护性,扩展性。
设计模式:经验的总结。Design pattern
是一种方法,不是方法和技术
创建型模式 对象的创建
结构型模式 对象的组成(结构)
行为型模式 对象的行为
创建型模式:
    1:简单工厂模式
            简单工厂模式概述 又叫静态工厂方法模式,它定义一个具体的工厂类负责创建一些类的实例 优点 客户端不需要在负责对象的创建,从而明确了各个类的职责 缺点 这个静态工厂类负责所有对象的创建,如果有新的对象增加,或者某些对象的创建方式不同,就需要不断的修改工厂类,不利于后期的维护
public class AnimalFactory {

    private AnimalFactory() {
    }

    // public static Dog createDog() {
    // return new Dog();
    // }
    //
    // public static Cat createCat() {
    // return new Cat();
    // }

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


public class AnimalDemo {
    public static void main(String[] args) {
        // 具体类调用
        Dog d = new Dog();
        d.eat();
        Cat c = new Cat();
        c.eat();
        System.out.println("------------");

        // 工厂有了后,通过工厂给造
        // Dog dd = AnimalFactory.createDog();
        // Cat cc = AnimalFactory.createCat();
        // dd.eat();
        // cc.eat();
        // System.out.println("------------");

        // 工厂改进后
        Animal a = AnimalFactory.createAnimal("dog");
        a.eat();
        a = AnimalFactory.createAnimal("cat");
        a.eat();

        // NullPointerException
        a = AnimalFactory.createAnimal("pig");
        if (a != null) {
            a.eat();
        } else {
            System.out.println("对不起,暂时不提供这种动物");
        }
    }
}
    2:工厂方法模式
       这个工厂,面向接口编程。抽象类。
     有大工厂,许多小工厂。需要什么工厂,造什么工厂。需要猫,就造猫,需要狗,就造狗。

工厂方法模式中抽象工厂类负责定义创建对象的接口,具体对象的创建工作由继承抽象工厂的具体类实现。
优点: 客户端不需要在负责对象的创建,从而明确了各个类的职责,如果有新的对象增加,只需要增加一个具体的类和具体的工厂类即可,不影响已有的代码,后期维护容易,增强了系统的扩展性
缺点 :需要额外的编写代码,增加了工作
   public interface Factory {
    public abstract Animal createAnimal();
}
public class DogFactory implements Factory {

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

}


public class CatFactory implements Factory {

    @Override
    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();
        System.out.println("-------");

        //需求:我要买只猫
        f = new CatFactory();
        a = f.createAnimal();
        a.eat();
    }
}

3:单例设计模式
单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供。
优点:在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象单例模式无疑可以提高系统的性能。            缺点:没有抽象层,因此扩展很难。 职责过重,在一定程序上违背了单一职责

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

    // 自己造一个
    // 静态方法只能访问静态成员变量,加静态
    // 为了不让外界直接访问修改这个值,加private
//一new试试恶汉式
    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 = new Student();
        // Student s2 = new Student();
        // System.out.println(s1 == s2); // false

        // 通过单例如何得到对象呢?

        // Student.s = null;

        Student s1 = Student.getStudent();
        Student s2 = Student.getStudent();
        System.out.println(s1 == s2);

        System.out.println(s1); // null,cn.itcast_03.Student@175078b
        System.out.println(s2);// null,cn.itcast_03.Student@175078b
    }
}

类已加载进来,一进来就造对象,这个就是饿汉式。一回来就吃馒头。
懒汉式
/*
* 单例模式:
*         饿汉式:类一加载就创建对象
*         懒汉式:用的时候,才去创建对象
*
* 面试题:单例模式的思想是什么?请写一个代码体现。
*
*         开发:饿汉式(是不会出问题的单例模式)
*         面试:懒汉式(可能会出问题的单例模式)
*             A:懒加载(延迟加载)   
*             B:线程安全问题
*                 a:是否多线程环境    是
*                 b:是否有共享数据    是
*                 c:是否有多条语句操作共享数据     是
*/
public class Teacher {
    private Teacher() {
    }

    private static Teacher t = null;

    public synchronized static Teacher getTeacher() {
        // t1,t2,t3
        if (t == null) {
            //t1,t2,t3
            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);
        System.out.println(t1); // cn.itcast_03.Teacher@175078b
        System.out.println(t2);// cn.itcast_03.Teacher@175078b
    }
}
/*
* Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
* exec(String command)
*/
public class RuntimeDemo {
    public static void main(String[] args) throws IOException {
        Runtime r = Runtime.getRuntime();
//        r.exec("winmine");
        // r.exec("notepad");
        // r.exec("calc");
//        r.exec("shutdown -s -t 10000");
        r.exec("shutdown -a");
    }
}

/*
* class Runtime {
*         private Runtime() {}
*         private static Runtime currentRuntime = new Runtime();
*         public static Runtime getRuntime() {
*           return currentRuntime;
*       }
* }
*/
模板设计模式是用抽象类实现的。
需求:计算一段代码的运行时间。
对扩展开放,对修改关闭
定义一个算法的骨架,用子类实现动态的代码。


装饰设计模式:
手机
定义手机规则
interface
                           新加彩铃                            听音乐
                         给手机进行装饰                  给手机进行装饰      
具体的手机
用那给就做那个的装饰类。
装饰可以任意组合。在io流中的使用。
InputStream is = System.in
InputStreamReader isr = new InputStreamReader(is):
BufferedReader br = new BufferedReader(isr);
new BufferedReader(new InputStramReader(System.in))
//==-=====================TEST

/*
* 我给你ArrayList<Integer>的一个对象,我想在这个集合中添加一个字符串数据,如何实现呢?
*/
public class ArrayListDemo {
    public static void main(String[] args) throws NoSuchMethodException,
            SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        // 创建集合对象
        ArrayList<Integer> array = new ArrayList<Integer>();

        // array.add("hello");
        // array.add(10);

        Class c = array.getClass(); // 集合ArrayList的class文件对象
        Method m = c.getMethod("add", Object.class);

        m.invoke(array, "hello"); // 调用array的add方法,传入的值是hello
        m.invoke(array, "world");
        m.invoke(array, "java");

        System.out.println(array);
    }
}
/*
* 通过配置文件运行类中的方法
*
* 反射:
*         需要有配置文件配合使用。
*         用class.txt代替。
*         并且你知道有两个键。
*             className
*             methodName
*/

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马