一个类只有一个实例,该类能自己创建该类的一个实例。
public class LazySingleton {
//保证lazySingleton在线程中同步
private static volatile LazySingleton lazySingleton = null;
//保证类不在别的地方被实例化
private LazySingleton() {
}
//synchronize保证线程安全
public static synchronized LazySingleton getInstance() {
if (lazySingleton == null) {
lazySingleton = new LazySingleton();
}
return lazySingleton;
}
}
public class HungrySingleton {
private static final HungrySingleton hungrySingleton = new HungrySingleton();
private HungrySingleton() {
}
public static HungrySingleton getInstance() {
return hungrySingleton;
}
}
对象的实际创建工作推迟到子类当中。
public interface Product {
public void show();
}
public class ProductImpl1 implements Product {
@Override
public void show() {
System.out.println("我生产了1");
}
}
public class ProductImpl2 implements Product {
@Override
public void show() {
System.out.println("我生产了2");
}
}
public interface AbstractFactory {
public Product getInstance();
}
public class FactoryTest {
public static void main(String[] args) {
CreateFactory1 createFactory1 = new CreateFactory1();
createFactory1.getInstance();
CreateFactory2 createFactory2 = new CreateFactory2();
createFactory2.getInstance();
}
static class CreateFactory1 implements AbstractFactory {
@Override
public Product getInstance() {
System.out.println("工厂1生产产品");
ProductImpl1 productImpl1 = new ProductImpl1();
productImpl1.show();
return productImpl1;
}
}
static class CreateFactory2 implements AbstractFactory {
@Override
public Product getInstance() {
System.out.println("工厂2生产产品");
ProductImpl2 productImpl2 = new ProductImpl2();
productImpl2.show();
return productImpl2;
}
}
}
是一种为访问类提供一个创建一组相关或相互依赖对象的接口,且访问类无须指定所要产品的具体类就能得到同族的不同等级的产品的模式结构。抽象工厂模式是工厂方法模式的升级版本,工厂方法模式只生产一个等级的产品,而抽象工厂模式可生产多个等级的产品。
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("我是长方形");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("我是矩形");
}
}
public interface Color {
void fill();
}
public class Red implements Color {
@Override
public void fill() {
System.out.println("填充红色");
}
}
public class Green implements Color {
@Override
public void fill() {
System.out.println("填充绿色");
}
}
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
public class ColorFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
if(color==null){
return null;
}else if(color.equalsIgnoreCase("red")){
return new Red();
}else if(color.equalsIgnoreCase("green")){
return new Green();
}
return null;
}
@Override
public Shape getShape(String shape) {
return null;
}
}
public class ShapeFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
return null;
}
@Override
public Shape getShape(String shape) {
if(shape==null){
return null;
}
if(shape.equalsIgnoreCase("rectangle")){
return new Rectangle();
}else if(shape.equalsIgnoreCase("square")){
return new Square();
}
return null;
}
}
public class FactoryProducer {
public static AbstractFactory getFactoryproduce(String choice){
if(choice.equalsIgnoreCase("shape")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("color")){
return new ColorFactory();
}
return null;
}
}
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//获取形状工厂
AbstractFactory shape = FactoryProducer.getFactoryproduce("shape");
Shape rectangle = shape.getShape("rectangle");
rectangle.draw();
Shape square = shape.getShape("square");
square.draw();
//获取颜色工厂
AbstractFactory colorFactory = FactoryProducer.getFactoryproduce("COLOR");
Color color1 = colorFactory.getColor("RED");
color1.fill();
}
}
代码地址https://gitee.com/Marlon_Brando/JavaTest/tree/master/src/main/java/designpatterns
。。。未完待续