zoukankan      html  css  js  c++  java
  • Java基础5——设计模式

    简单工厂模式

    //创建型模式:简单工厂模式
    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 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();
            Animal a = AnimalFactory.createAnimal("dog");
            a.eat();//调用的是狗的
            Animal b = AnimalFactory.createAnimal("cat");
            b.eat();//调用的是猫的
        }
    }

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

    工厂方法模式

    public interface Factory{
        public abstract Animal createAnimal();
    }
    
    public class DogFactory implements Factory{
        public Animal createAnimal(){
            return new Dog();
        }
    }
    
    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();
        }
    }

    工厂方法模式改变了简单工厂模式的弊端,但是工厂方法模式写起来比较繁琐

    还有一种工厂方式叫抽象工厂方式,可以解决工厂方法模式的弊端 

    单例模式——饿汉式

    特点:对象在内存中只有一份

    如何保证类在内存中只有一个对象
    1 把构造方法私有
    2 在成员位置自己创建一个对象
    3 通过一个公共的方法访问

    public class Student{
        private Student(){
            
        }
        //静态方法只能访问静态资源
        //外界也不能直接改变Student类里面的成员s,所以需要加上private
        private static Student s=new Student();
        
        //构造函数已经私有化了,所以外界只能通过静态的方式来访问getStudent方法
        public static Student getStudent(){
            return s;
        }
    }
    public class StudentDemo{
        public static void main(String[] args){
            Student s1=Student.getStudent();
            Student s2=Student.getStudent();
            System.out.println(s1==s2);
        }
    }

    单例模式——懒汉式

    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.getStudent();
            Teacher t2=Teacher.getStudent();
            System.out.println(t1==t2);
        }
    }

    总之,单例模式的思想就是保证只有一个对象,饿汉式是不会出问题的单例模式,所以开发的时候经常用,而懒汉式是会出问题的,所以面试时经常问

    懒汉式有延迟加载的特点

    懒汉式可能会带来线程安全问题,如果执行环境是多线程,我们有共享数据t, 也有多条语句操作t,如果在t1执行if(t==null)的时候t2也进来了就会创建两个对象所以比较保险的做法是给getTeacher方法加上synchronized关键字

    而饿汉式里面对象初始化就是一个原子性的操作,所以不会出现线程安全问题

    Runtime类的概述和应用

    public class RuntimeDemo{
        public static void main(String[] args) throws IOException{
            //每个Java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接,可以通过getRuntime方法获取当前运行时
            Runtime r=Runtime.getRuntime();
            //exec方法可以执行dos指令
            r.exec("calc");
        }
    }
    
    //在Runtime类源码内部,可以发现以下源码
    class Runtime{
        private RunTime(){}
        private static Runtime currentRuntime=new Runtime();
        public static Runtime getRuntime(){
            return currentRuntime;
        }
    }
  • 相关阅读:
    使用活字格搭建企业级web应用--办公用品管理系统
    怪兽级性能,用代码玩转Excel!葡萄城强势发布Spread表格组件
    无需编码,轻松搭建企业采购管理软件
    NHibernate变的简单
    Easyui + jQuery表单提交 给 Controller patr1
    Easyui datagrid 批量编辑和提交
    C# 网络编程 Part.1
    提高你开发效率的十五个Visual Studio 2010使用技巧
    System.IO
    C#路径,文件,目录,I/O常见操作
  • 原文地址:https://www.cnblogs.com/zhaohuiziwo901/p/5252373.html
Copyright © 2011-2022 走看看