Java开发常用设计模式
单例模式
定义
一个类只有一个实例,该类能自己创建该类的一个实例。
特点
- 单例类只有一个实例对象。
- 单例对象必须由该类自己创建。
- 单例类对外提供一个访问该单例的全局访问点。
单例模式的实现方式
- 懒汉模式:加载类的时候没有生成单例模式,调用生成实例对象的方法的时候创建单例。
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();
}
- 具体产品1实现产品接口
public class ProductImpl1 implements Product {
@Override
public void show() {
System.out.println("我生产了1");
}
}
- 具体产品2实现产品接口
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
。。。未完待续