单例模式 工厂模式

 

单例
package com.enorth.user;

/**
 * @author 李晨
 * @version 创建时间: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();         
        }         
}    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值