`
lichen0921
  • 浏览: 79498 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

单例模式 工厂模式

    博客分类:
  • Java
阅读更多

 

单例
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();         
        }         
}    
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics