1.单例模式
/** * 单例模式是一种常用的软件设计模式 * 它可以保证系统中一个类只有一个实例,即一个类只有一个实例对象 * 要满足上述条件1.构造方法要私有化,2.在自己内部创建私有的静态引用3.对外提供共有的静态方法 */ public class Singleton { //懒汉模式 private Singleton(){} private static Singleton singleton; public static Singleton getInstance(){ if (singleton==null){ //在高并发的情况下,如果采用的是懒汉模式,最好的选择是双重判断加同步的方式。 synchronized (Singleton.class){ //在多线程的情况下,不必每次获取对象时都进行同步,只有第一次才同步,提高了效率。 if (singleton==null){ singleton = new Singleton(); } } } return singleton; } }
public class Singleton { //饿汉模式 private Singleton(){} private static Singleton singleton = new Singleton(); public static Singleton getInstance(){ return singleton; } }
2.工厂模式
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。在实际项目中对数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时,使用工厂模式只需换方言和驱动就可以了。
/** * 形状接口,有一个画图方法 */ public interface Shape { void draw(); }
有三个形状的实现类
public class Circular implements Shape { public void draw() { System.out.println("画一个圆形"); } }
public class Square implements Shape{ public void draw() { System.out.println("画一个正方形"); } }
public class Triangle implements Shape { public void draw() { System.out.println("画一个三角形"); } }
/** *创建一个工厂,生成基于给定信息的实体类的对象 */ public class ShapeFactory { public Shape getShape(String shape){ if (shape==null){ return null; } if (shape.equalsIgnoreCase("CIRCULAR")){ return new Circular(); } if (shape.equalsIgnoreCase("SQUARE")){ return new Square(); } if (shape.equalsIgnoreCase("TRIANGLE")){ return new Triangle(); } return null; } }
测试类
public class Main { public static void main(String[] args){ ShapeFactory factory = new ShapeFactory(); Shape shape1 = factory.getShape("CIRCULAR"); Shape shape2 = factory.getShape("SQUARE"); Shape shape3 = factory.getShape("TRIANGLE"); shape1.draw(); shape2.draw(); shape3.draw(); } }
控制台打印结果:
3.抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。
有两个接口:形状和颜色
public interface Color { //填充颜色抽象方法 void fill(); }
public interface Shape { void draw(); }
他们的实现类
public class Circular implements Shape { public void draw() { System.out.println("画一个圆形"); } }
public class Square implements Shape{ public void draw() { System.out.println("画一个正方形"); } }
public class Triangle implements Shape { public void draw() { System.out.println("画一个三角形"); } }
public class Red implements Color { public void fill() { System.out.println("填充红色"); } }
public class Blue implements Color { public void fill() { System.out.println("填充蓝色"); } }
public class Yellow implements Color { public void fill() { System.out.println("填充黄色"); } }
抽象工厂类
public abstract class BaseFactory { public abstract Shape getShape(String shapeType); public abstract Color getColor(String colorType); }
工厂类的实现类
public class ShapeFactory extends BaseFactory{ public Shape getShape(String shapeType){ if (shapeType.equalsIgnoreCase("CIRCULAR")){ return new Circular(); } if (shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } if (shapeType.equalsIgnoreCase("TRIANGLE")){ return new Triangle(); } return null; } public Color getColor(String colorType) { return null; } }
public class ColorFactory extends BaseFactory{ public Shape getShape(String shapeType) { return null; } public Color getColor(String colorType) { if (colorType.equalsIgnoreCase("RED")){ return new Red(); } if (colorType.equalsIgnoreCase("YELLOW")){ return new Yellow(); } if (colorType.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; } }
创建工厂生成器,来获取具体工厂。
public class FactoryProducer { public static BaseFactory getFactory(String choice){ if(choice.equalsIgnoreCase("SHAPE")){ return new ShapeFactory(); } else if(choice.equalsIgnoreCase("COLOR")){ return new ColorFactory(); } return null; } }
测试
public class Main { public static void main(String[] args){ BaseFactory factory = FactoryProducer.getFactory("SHAPE"); BaseFactory factory1 = FactoryProducer.getFactory("COLOR"); Shape shape1 = factory.getShape("CIRCULAR"); Shape shape2 = factory.getShape("SQUARE"); Shape shape3 = factory.getShape("TRIANGLE"); Color color1 = factory1.getColor("RED"); Color color2 = factory1.getColor("BLUE"); Color color3 = factory1.getColor("YELLOW"); shape1.draw(); color1.fill(); shape2.draw(); color2.fill(); shape3.draw(); color3.fill(); } }
控制台打印结果: