单例
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();
}
}
分享到:
相关推荐
单例模式和工厂模式结合应用,实现了产品的生产,适合用做课程设计,包含详细文档和代码。Java语言。喜欢的可以下载来看看那
NULL 博文链接:https://xieyan30.iteye.com/blog/1695483
php单例模式php单例模式php单例模式php单例模式
1、单例模式 结论:单例模式可以把数据封装在对象里,操作对象;而静态类是把数据封装在类里,操作类。单例模式中,数据只有在对象存在的时候才能操作;而静态类中,数据可以直接通过静态类点出来操作。 2、工厂模式...
模式就像是OOP开发人员的配方,每种模式都提供了所需的成分。模式对OOP开发人员尤其有用,因为它有助于创建稳定的API,...本章将介绍两种常用的模式:单例模式和工厂模式。前者被称为类的职责,后者被称为类的多态性。
java 设计模式 mvc模式 单例模式 代理 工厂 简单工厂
细心整合和单例模式和工厂模式的几种模型,懒汉式,饿汉式,如何并发操作模式,等都有详细讲解
简单工厂模式、抽象工厂模式、工厂方法模式、饿汉单例模式、懒汉单例模式
java 设计模式 mvc模式 单例模式 代理 工厂 简单工厂 第二部分
javaweb项目+设计模式(单例模式,工厂模式,动态代理,适配器)
很简单的东西,适合没入门的人看,两个类,一个类是单例,一个类是工厂,一看就知道他们的区别在哪里了。
基于Java实现的23种设计模式Demo,抽象工厂模式、建造模式、工厂方法模式、原型模式、单例模式、外观模式、适配器模式等 Java设计模式共有23种,分别为:抽象工厂模式、建造模式、工厂方法模式、原型模式、单例模式...
NULL 博文链接:https://zhaohong.iteye.com/blog/483396
NULL 博文链接:https://51even.iteye.com/blog/768097
24设计模式之禅 代理模式、单例模式、工厂模式实例代码 DEMO
主要是对javascript的面向对象思想的一个理解,有简单的单例模式,工厂模式,观察者模式含代码。
java 三种设计模式Demo 其中对单例模式做了详细的讲解,及性能和线程安全方面的优化其中Singleton.java为经典单例模式。
23种设计模式之工厂方法模式对工厂方法模式进行了简单的介绍,并附带了例子程序,和大家分享
自己用VC2010写的工厂模式和单例模式的实现
java 单例模式和工厂模式实例详解.docx