• 大龄屌丝自学笔记Java零基础到菜鸟035


    常用设计模式:简单工厂模式、工厂方法模式、单例模式,Runtime类

    1、设计模式

    创建型:创建对象。

    结构型:对象的组成。

    行为型:对象的功能。

    2、简单工厂模式(创建型)

     1 public abstract class Animal{
     2     public abstract void eat();
     3 }
     4 
     5 public class Cat extends Animal{
     6     @Override
     7     public void eat(){
     8         System.out.println("喵");
     9     }
    10 }
    11 
    12 public class Dog extends Animal{
    13     @Override
    14     public void eat(){
    15         System.out.println("汪");
    16     }
    17 }
    18 
    19 piblic class AnimalFactory{
    20     private AnimalFactory(){
    21     }
    22 
    23     public static void createAnimal(String type){
    24         if("cat".equals(type)==true){
    25             return new Cat();
    26         }else if("dog".equals(type)==true){
    27             return new Dog();
    28         }else{
    29             return null;
    30         }
    31     }
    32 }
    33 
    34 public class Fin{
    35     public static void main(String[] args){
    36         Animal cat = AnimalFactory.createAnimal("cat");
    37         if(cat != null){
    38             cat.eat();
    39         }else{
    40             System.out.println("null");
    41         }
    42     }
    43 }
    44 
    45 //输出:喵

    3、工厂方法模式(创建型)

     1 public abstract class Animal{
     2     public abstract void eat();
     3 }
     4 
     5 public class Cat extends Animal{
     6     @Override
     7     public void eat(){
     8         System.out.println("喵");
     9     }
    10 }
    11 
    12 public interface IFactory{
    13     public static Animal createAnimal();
    14 }
    15 
    16 public class CatFactory implements IFactory{
    17     @Override
    18     public Animal createAimal(){
    19         return new Cat();
    20     }
    21 }
    22 
    23 public class Fin{
    24     public static void main(String[] args){
    25         IFactory factory = new CatFactory();
    26         Animal cat = factory.createAnimal();
    27         cat.eat();
    28     }
    29 }
    30 
    31 //输出:喵

    4、单例模式(创建型)

    (1)饿汉

     1 public class Cat{
     2     private Cat(){
     3     }
     4 
     5     private static Cat cat= new Cat();
     6 
     7     public static Cat getCat(){
     8         return cat;
     9     }
    10 }
    11 
    12 public class Fin{
    13     public static void main(String[] args){
    14         Cat cat1=Cat.getCat();
    15         Cat cat2=Cat.getCat();
    16         System.out.println(cat1==cat2);
    17     }
    18 }
    19 
    20 //输出:true

    (2)懒汉

     1 public class Cat{
     2     private Cat(){
     3     }
     4 
     5     private static Cat cat= null;
     6 
     7     public synchronized static Cat getCat(){
     8         if(cat == null){
     9             cat = new Cat();
    10         }
    11         return cat;
    12     }
    13 }
    14 
    15 public class Fin{
    16     public static void main(String[] args){
    17         Cat cat1=Cat.getCat();
    18         Cat cat2=Cat.getCat();
    19         System.out.println(cat1==cat2);
    20     }
    21 }
    22 
    23 //输出:true

    注意事项:单例模式体现类在内存中只有一个对象的思想。

         开发写(1)饿汉:类一加载就创建对象,开发不会出问题。

         面试写(2)懒汉:用的时候,才会创建对象,体现了延迟加载的思想,但存在安全问题(多线程环境下,可能会出现多条语句操作共享数据),所以必须加【synchronized】。

    5、Runtime类

    Runtime rt=Runtime.getRuntime();

    扫雷:rt.exec("winmine");

    记事本:rt.exec("notepad");

    关机:rt.exec("shutdown -s -t 10000");

  • 相关阅读:
    Kafka 文档用例
    Kafka 文档引言
    关于工作流核心选择
    IEEE浮点标准
    新项目的CQRS设计
    持续集成:CruiseControl.NET + VisualSVN.Server
    商城(一)
    Maven基础学习(一)—Maven入门
    Mybatis基础学习(五)—缓存
    Mybatis基础学习(四)—关系映射
  • 原文地址:https://www.cnblogs.com/liverpool/p/4899448.html
Copyright © 2020-2023  润新知