[设计模式]创建型设计模式
Posted mrdragonma
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[设计模式]创建型设计模式相关的知识,希望对你有一定的参考价值。
简介
创建型设计模式:创建对象的同时,隐藏对象创建的细节
代码
- 简单工厂模式
```java
package me.maxiaolong.designpattern;
/**
* @author maxiaolong
* created at 2020/7/2
* 简单工厂模式,工厂类只需要知道参数便可以获得相应对象
* 优点:
* 缺点:1) 如果增加产品,必须要修改工厂类(违背开闭原则)
* 例子:
* 冰箱厂 生产大冰箱,中冰箱,小冰箱
*/
public class SimpleFactoryClient {
public static void main(String[] args) {
// 创建工厂的过程可以使用简单工厂进一步隐藏创建细节
ShapeFactory shapeFactory = new ShapeFactory();
Shape circle = shapeFactory.getShape("Circle");
circle.draw();
}
}
class ShapeFactory{
ShapeFactory(){
}
public Shape getShape(String shapeDescription){
switch (shapeDescription){
case "Triangle":
return new Triangle();
case "Rectangle":
return new Rectangle();
case "Circle":
return new Circle();
default:
return null;
}
}
}
interface Shape{
/**
* 绘制图形
*/
void draw();
}
class Triangle implements Shape{
@Override
public void draw() {
System.out.println("draw a triangle shape");
}
}
class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("draw a rectangle shape");
}
}
class Circle implements Shape{
@Override
public void draw() {
System.out.println("draw a circle shape");
}
}
```
- 工厂方法模式
```java
package me.maxiaolong.designpattern;
/**
* @author maxiaolong
* created at 2020/7/3
* 相比于简单工厂,增加了抽象工厂类(抽象工厂类仅用于创建高层接口) 产品依然是单一种类产品
* 例子:增加具体产品不需要修改原有工厂代码,只需要在增加具体产品的基础上增加具体工厂(开闭原则) 即增加产品等级更加满足开闭原则
*/
public class FactoryMethodClient {
public static void main(String[] args) {
LogFactory logFactory = new FileLogFactory();
Log log = logFactory.createLog();
log.writeLog();
}
}
interface LogFactory{
/**
* 创建Log
* @return
*/
Log createLog();
}
class FileLogFactory implements LogFactory{
@Override
public Log createLog() {
return new FileLog();
}
}
class DatabaseLogFactory implements LogFactory{
@Override
public Log createLog() {
return new DatabaseLog();
}
}
interface Log{
/**
* 写日志
*/
void writeLog();
}
class FileLog implements Log{
@Override
public void writeLog() {
System.out.println("write file log");
}
}
class DatabaseLog implements Log{
@Override
public void writeLog() {
System.out.println("write database log");
}
}
```
- 抽象工厂模式
```java
package me.maxiaolong.designpattern;
/**
* @author maxiaolong
* created at 2020/7/3
* 增加产品族
*
*/
public class AbstractFactoryClient {
public static void main(String[] args) {
// 工厂new的操作可以使用简单工厂代替
AbstractFactory factory = new BigFactory();
AbstractIceBox iceBox = factory.createIceBox();
iceBox.describeIceBox();
}
}
interface AbstractFactory{
/**
* 生产冰箱
* @return
*/
AbstractIceBox createIceBox();
/**
* 生产空调
* @return
*/
AbstractAirCondition createAirCondition();
}
class BigFactory implements AbstractFactory{
@Override
public AbstractIceBox createIceBox() {
return new BigIceBox();
}
@Override
public AbstractAirCondition createAirCondition() {
return new BigAirCondition();
}
}
class MediumFactory implements AbstractFactory{
@Override
public AbstractIceBox createIceBox() {
return new MediumIceBox();
}
@Override
public AbstractAirCondition createAirCondition() {
return new MediumAirCondition();
}
}
class SmallFactory implements AbstractFactory{
@Override
public AbstractIceBox createIceBox() {
return new SmallIceBox();
}
@Override
public AbstractAirCondition createAirCondition() {
return new SmallAirCondition();
}
}
interface AbstractIceBox {
/**
* 生产冰箱接口
*/
void describeIceBox();
}
class BigIceBox implements AbstractIceBox {
@Override
public void describeIceBox() {
System.out.println("describe big icebox");
}
}
class MediumIceBox implements AbstractIceBox {
@Override
public void describeIceBox() {
System.out.println("describe medium icebox");
}
}
class SmallIceBox implements AbstractIceBox {
@Override
public void describeIceBox() {
System.out.println("describe small icebox");
}
}
interface AbstractAirCondition {
/**
* 生产空调接口
*/
void describeAirCondition();
}
class BigAirCondition implements AbstractAirCondition {
@Override
public void describeAirCondition() {
System.out.println("describe big air condition");
}
}
class MediumAirCondition implements AbstractAirCondition {
@Override
public void describeAirCondition() {
System.out.println("describe medium air condition");
}
}
class SmallAirCondition implements AbstractAirCondition {
@Override
public void describeAirCondition() {
System.out.println("describe small air condition");
}
}
```
- 建造者模式
```java
package me.maxiaolong.designpattern;
/**
* @author maxiaolong
* created at 2020/7/3
* 定义:创建一个包含多个组成部分的复杂对象,可以返回一个完整的产品对象给用户
*/
public class BuilderClient {
public static void main(String[] args) {
Builder builder = new ConcreteBuilder();
Director director = new Director(builder);
Product product = director.construct();
}
}
/**
* 负责安排复杂对象的建造次序
*/
class Director{
private Builder builder;
Director(Builder builder){
this.builder = builder;
}
public Builder getBuilder() {
return builder;
}
public void setBuilder(Builder builder) {
this.builder = builder;
}
/**
* 控制对象构建顺序
*/
public Product construct(){
this.builder.buildPartA();
this.builder.buildPartB();
this.builder.buildPartC();
return this.builder.getResult();
}
}
/**
* 产品以及部分
*/
class Product{
private String partA;
private String partB;
private String partC;
public String getPartA() {
return partA;
}
public void setPartA(String partA) {
this.partA = partA;
}
public String getPartB() {
return partB;
}
public void setPartB(String partB) {
this.partB = partB;
}
public String getPartC() {
return partC;
}
public void setPartC(String partC) {
this.partC = partC;
}
}
abstract class Builder{
/**
* 申明为受保护的,便于子类使用
*/
protected Product product = new Product();
abstract void buildPartA();
abstract void buildPartB();
abstract void buildPartC();
public Product getResult(){
return this.product;
}
}
class ConcreteBuilder extends Builder{
@Override
public void buildPartA(){
this.product.setPartA("partA");
}
@Override
public void buildPartB(){
this.product.setPartB("partB");
}
@Override
public void buildPartC(){
this.product.setPartC("partC");
}
}
```
- 单例模式
```java
package me.maxiaolong.designpattern;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
/**
* @author maxiaolong
* created at 2020/7/3
*/
public class SingletonClient {
}
/**
* 饿汉模式
*/
class SingletonV1{
private static SingletonV1 singletonV1 = new SingletonV1();
private SingletonV1(){}
public static SingletonV1 getInstance(){
return singletonV1;
}
}
/**
* 懒汉模式
*/
class SingletonV2{
private static SingletonV2 singletonV2;
private SingletonV2(){}
/**
* 懒汉模式线程不安全
* @return
*/
public static SingletonV2 getInstance(){
if(singletonV2==null){
//没有设置为临界区,可自由访问
singletonV2 = new SingletonV2();
}
return singletonV2;
}
}
class SingletonV3{
/**
* volatile关键字可以使指令重排序失效
*/
private static volatile SingletonV3 singletonV3;
private SingletonV3(){}
public static SingletonV3 getInstance(){
if(singletonV3==null){
// 如果对整个方法加synchronized,同步块太大,不利于并发
synchronized (Object.class){
if(singletonV3==null){
// 过程 1. 内存空间开辟 2. 指针指向 3. 对象初始化 其中2,3是进行指令重排序优化的
singletonV3 = new SingletonV3();
}
}
}
return singletonV3;
}
}
/**
* 延迟初始化占位类模式
* 实现延迟初始化的同时,保证线程安全
* 原理:1. 外部类加载时不要求内部类加载,即SingletonV4加载时,不要求内部类InstanceHolder被加载;只有当外部类调用getInstance静态方法时内部类才加载并完成初始化操作;
* 2. 多线程进行类的初始化时,JVM保证只有一条线程进行类的初始化,其它线程则会被阻塞。
*/
class SingletonV4{
public static class InstanceHolder{
public static SingletonV4 singletonV4 = new SingletonV4();
}
private SingletonV4(){}
public static SingletonV4 getInstance(){
return InstanceHolder.singletonV4;
}
}
```
以上是关于[设计模式]创建型设计模式的主要内容,如果未能解决你的问题,请参考以下文章