Java笔记程序员Java学习笔记

Java基础学习第二十五天——设计模式

2016-04-03  本文已影响135人  lutianfei
文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.04.02 lutianfei none

[TOC]


面向对象思想设计原则

单一职责原则

开闭原则

里氏替换原则

依赖注入原则

接口分离原则

迪米特原则

设计模式

设计模式概述

设计模式的几个要素

设计模式的分类

简单工厂模式

简单工厂模式概述
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("对不起,暂时不提供这种动物");
        }
    }
}




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


public class Cat extends Animal {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

}


public class Dog extends Animal {

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

}


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) {
        // 需求:我要买只狗
        Factory f = new DogFactory();
        Animal a = f.createAnimal();
        a.eat();
        System.out.println("-------");
        
        //需求:我要买只猫
        f = new CatFactory();
        a = f.createAnimal();
        a.eat();
    }
}


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 {

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

}


public class Cat extends Animal {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

}


public class CatFactory implements Factory {

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

}

单例设计模式

单例设计模式概述
单例设计模式分类
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
    }
}



public class Student {
    // 构造私有,不让外界构造对象。
    private Student() {
    }

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

    // 提供公共的访问方式
    // 为了保证外界能够直接使用该方法,加静态
    public static Student getStudent() {
        return s;
    }
}
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
    }
}


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;
    }
}

Runtime类的概述和使用

/*
 * 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;
 *       }
 * }
 */
上一篇 下一篇

猜你喜欢

热点阅读