博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单例模式 工厂模式
阅读量:7024 次
发布时间:2019-06-28

本文共 2392 字,大约阅读时间需要 7 分钟。

  hot3.png

 
单例
package com.enorth.user;
/**    
* 李晨        
* 创建时间:Jun 29, 2009 9:10:02 AM    
*/
public class Singleton {
   private static Singleton singleton = null;
   private Singleton() {
  }
   public static Singleton getInstance() {
     if (singleton == null) {
       //防止线程安全
       synchronized (Singleton. class) {
         if (singleton == null) {
          singleton = new Singleton();
           return singleton;
        }
      }
    }
     return singleton;
  }
}
 
简单工厂
// 产品接口                    
public interface Product {        
        
         public void getName();        
        
}        
        
// 具体产品A        
public class ProductA implements Product {        
        
         public void getName() {        
                System.out.println( "    I am ProductA    ");        
        }        
        
}        
        
// 具体产品B        
public class ProductB implements Product {        
        
         public void getName() {        
                System.out.println( "    I am ProductB    ");        
        }        
        
}        
        
// 工厂类        
public class ProductCreator {        
        
         public Product createProduct(String type) {        
                 if ( " A ".equals(type)) {        
                         return new ProductA();        
                }        
                 if ( " B ".equals(type)) {        
                         return new ProductB();        
                } else        
                         return null;        
        }        
        
         public static void main(String[] args) {        
                ProductCreator creator = new ProductCreator();        
                creator.createProduct( " A ").getName();        
                creator.createProduct( " B ").getName();        
        }        
}    
 
抽象工厂
//    产品 Plant接口                    
public interface Plant {        
}        
        
// 具体产品PlantA,PlantB        
public class PlantA implements Plant {        
        
         public PlantA() {        
                System.out.println( " create PlantA ! ");        
        }        
        
         public void doSomething() {        
                System.out.println( "    PlantA do something    ");        
        }        
}        
        
public class PlantB implements Plant {        
         public PlantB() {        
                System.out.println( " create PlantB ! ");        
        }        
        
         public void doSomething() {        
                System.out.println( "    PlantB do something    ");        
        }        
}        
        
// 产品 Fruit接口        
public interface Fruit {        
}        
        
// 具体产品FruitA,FruitB        
public class FruitA implements Fruit {        
         public FruitA() {        
                System.out.println( " create FruitA ! ");        
        }        
        
         public void doSomething() {        
                System.out.println( "    FruitA do something    ");        
        }        
}        
        
public class FruitB implements Fruit {        
         public FruitB() {        
                System.out.println( " create FruitB ! ");        
        }        
        
         public void doSomething() {        
                System.out.println( "    FruitB do something    ");        
        }        
}        
        
// 抽象工厂方法        
public interface AbstractFactory {        
         public Plant createPlant();        
        
         public Fruit createFruit();        
}        
        
// 具体工厂方法        
public class FactoryA implements AbstractFactory {        
         public Plant createPlant() {        
                 return new PlantA();        
        }        
        
         public Fruit createFruit() {        
                 return new FruitA();        
        }        
}        
        
public class FactoryB implements AbstractFactory {        
         public Plant createPlant() {        
                 return new PlantB();        
        }        
        
         public Fruit createFruit() {        
                 return new FruitB();        
        }        
}    
 
 

本文出自 “” 博客,请务必保留此出处

转载于:https://my.oschina.net/lichen/blog/264916

你可能感兴趣的文章
VMware Workstation 12-虚拟机-批量创建-快照-批量创建恢复
查看>>
Oracle 变量绑定与变量窥视合集系列一
查看>>
只要3分钟,Python生成器原理详解
查看>>
51CTO梦想的摇篮【我与51CTO一“七”成长】
查看>>
db2数据库的schema和user
查看>>
知乎引流实操:日吸200精准粉丝玩法分享
查看>>
KMS客户端出现0xC004F074错误的解决方法
查看>>
浅谈如何通过自媒体渠道实现赚钱的途径与方法?
查看>>
老男孩的学道,教道实践经!
查看>>
从ORA-00374错误看运维工作的规范性
查看>>
十分详细的DHCP服务工作原理剖析
查看>>
大数据实战之环境搭建(六)
查看>>
RAID技术穷途末路了吗?
查看>>
windows 2008 64位oracle11g部署问题(1)之警告-启动database control时出错
查看>>
SFB 项目经验-30-SFB与SFB联盟-IM-正常-状态-不正常
查看>>
Linux如何在系统运行过程中修改内核参数
查看>>
配套自测连载(八)
查看>>
日期格式化大全
查看>>
Windows 8产品重置次数
查看>>
《IT人生需要指引》读后感(学生作业分享)
查看>>