0%

工厂模式

前言:工厂模式

简单工厂模式

类图

image-20191226001459867

Product.java
1
2
3
public interface Product {
void display();
}
ProductA.java
1
2
3
4
5
6
7
8
9
10
/**
* @author shency
* @description: ProductA
*/
public class ProductA implements Product {
@Override
public void display() {
System.out.println("ProductA");
}
}
ProductB.java
1
2
3
4
5
6
7
8
9
10
11
/**
* @author shency
* @description: ProductB
*/
public class ProductB implements Product{

@Override
public void display() {
System.out.println("ProductB");
}
}
SimpleFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @author shency
* @description: 简单工厂
*/
public class SimpleFactory {
public Product GetProduct(String args){
if(args.equals("A")){
return new ProductA();
}else if(args.equals("B")){
return new ProductB();
}
return null;
}
}

工厂模式

image-20191226001625456

抽象工厂模式

image-20191226001633764

-------------本文结束感谢您的阅读-------------