单例模式只在应用程序中保证一个实例的存在。
通常单例模式两种构建方式:
懒汉方式。指全局的单例实例在第一次被使用时构建。
饿汉方式。指全局的单例实例在类装载时构建。
饿汉模式
public class Singleton {
private final static Singleton INSTANCE = new Singleton();
// 默认的构造函数
private Singleton() {}
//公开获取的实例
public static Singleton getInstance() {
return INSTANCE;
}
}
懒汉式
public class Singleton {
private static volatile Singleton INSTANCE = null;
// 默认的构造函数
private Singleton() {}
//双重判断是为了确保更加安全
public static Singleton getInstance() {
if(INSTANCE == null){
synchronized(Singleton.class){
if(INSTANCE == null){
INSTANCE = new Singleton();
}
}
}
return INSTANCE;
}
}
工厂模式 (这三个工厂的具体形态和特点自己查询一下吧,要说起来特别长)
抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的
工厂
封装起来。在正常使用中,客户端程序需要创建抽象工厂的具体实现,然后使用抽象工厂作为
接口
来创建这一主题的具体对象。客户端程序不需要知道(或关心)它从这些内部的工厂方法中获得对象的具体类型,因为客户端程序仅使用这些对象的通用接口。抽象工厂模式将一组对象的实现细节与他们的一般使用分离开来。
工厂模式的几种形态:
(1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory Method
Pattern)。
(2)工厂方法(Factory Method)模式,又称多态性工厂(Polymorphic Factory)模式
或虚拟构造子(Virtual Constructor)模式;
(3)抽象工厂(Abstract Factory)模式,又称工具箱(Kit 或Toolkit)模式。
简单工厂模式的举例
例:
//抽象产品角色
public interface Car{
public void drive();
}
//具体产品角色
public class QQ implements Car{
public void drive() {
System.out.println("Driving QQ ");
}
}
public class HF implements Car{
public void drive() {
System.out.println("Driving HF ");
}
}
//工厂类角色
public class Driver{
//工厂方法.注意 返回类型为抽象产品角色
public static Car driverCar(String s)throws
Exception{
//判断逻辑,返回具体的产品角色给Client
if(s.equalsIgnoreCase("QQ "))
return new QQ ();
else
if(s.equalsIgnoreCase("HF "))
return new Bmw();
else throw new Exception();
}
}
工厂方法模式的举例
例:
//抽象产品
public abstract class PenCore{
String color;
public abstract void writeWord(String s);
}
//具体产品
public class RedPenCore extends PenCore {
RedPenCore() {
color = "红色";
}
public void writeWord(String s) {
System.out.println("写出" + color +
"的字" + s);
}
}
public class BluePenCore extends PenCore {
BluePenCore() {
color = "蓝色";
}
public void writeWord(String s) {
System.out.println("写出" + color +
"的字" + s);
}
}
public class BlackPenCore extends PenCore {
BlackPenCore() {
color = "黑色";
}
public void writeWord(String s) {
System.out.println("写出" + color +
"的字" + s);
}
}
//构造者
public abstract class BallPen {
BallPen(){
System.out.println("生产一只装有"+getPenCore().color+"笔芯的圆珠笔");
}
public abstract PenCore getPenCore();
}
//具体构造者
public class RedBallPen extends BallPen {
public PenCore getPenCore() {
return new RedPenCore();
}
}
public class BlueBallPen extends BallPen {
public PenCore getPenCore() {
return new BluePenCore();
}
}
public class BlackBallPen extends BallPen {
public PenCore getPenCore() {
return new BlackPenCore();
}
}
抽象工厂模式的举例
例:
//抽象产品 上衣
public abstract class UpperClothes {
public abstract int getChestSize();
public abstract int getHeight();
public abstract String getName();
}
//裤子
public abstract class Trousers {
public abstract int getWaistSize();
public abstract int getHeight();
public abstract String getName();
}
//具体产品 西方的裤子
public class WesternUpperClothes extends UpperClothes {
private int chestSize;
private int height;
private String name;
WesternUpperClothes(String name,int chestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public int getChestSize() {
return chestSize;
}
public int getHeight() {
return height;
}
public String getName() {
return name;
}
}
//品牌上衣
public class CowboyUpperClothes extends UpperClothes {
private int chestSize;
private int height;
private String name;
CowboyUpperClothes(String name,int chestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public int getChestSize() {
return chestSize;
}
public int getHeight() {
return height;
}
public String getName () {
return name;
}
}
//西方裤子
public class WesternTrousers extends Trousers {
private int waistSize;
private int height;
private String name;
WesternTrousers(String name,int waistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public int getHeight() {
return height;
}
public String getName() {
return name;
}
public int getWaistSize() {
return waistSize;
}
}
public class CowboyTrousers extends Trousers {
private int waistSize;
private int height;
private String name;
CowboyTrousers(String name,int waistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public int getHeight() {
return height;
}
public String getName() {
return name;
}
public int getWaistSize() {
return waistSize;
}
}
//抽象工厂 创造衣服的工厂
public abstract class ClothesFactory {
public abstract UpperClothes createUpperClothes(int
chestSize,int height);
public abstract Trousers createTrousers(int waistSize,int
height);
}
//具体工厂
public class NowCoderClothesFactory extends ClothesFactory {
public Trousers createTrousers(int waistSize, int height)
{
return new
WesternTrousers("牛客牌裤子",waistSize,height);
}
public UpperClothes createUpperClothes(int chestSize, int
height) {
return new
WesternUpperClothes("牛客牌上衣",chestSize,height);
}
}
public class KBClothesFactory extends ClothesFactory {
public Trousers createTrousers(int waistSize, int height)
{
return new
WesternTrousers("科比牌裤子",waistSize,height);
}
public UpperClothes createUpperClothes(int chestSize, int
height) {
return new
WesternUpperClothes("科比牌上衣",chestSize,height);
}
}
public class HuLuWaClothesFactory extends ClothesFactory {
public Trousers createTrousers(int waistSize, int height)
{
return new
WesternTrousers("葫芦牌裤子",waistSize,height);
}
public UpperClothes createUpperClothes(int chestSize, int
height) {
return new
WesternUpperClothes("葫芦牌上衣",chestSize,height);
}
}