//20210117
寫在前面:剛期末考試完,考了面向對象,里邊兒有23個設計模式,我尋思着考完挨個兒實現一下,本文先實現三個工廠模式————簡單工廠、工廠模式、抽象工廠模式
一、簡單工廠模式
- 簡單的僅有一個的大工廠,工廠里根據分支條件來判斷要生產的產品
- 源碼如下
//大工廠接口
public interface Factory {
Production product(String type);
}
//產品接口
public interface Production {
void des();
}
//工廠實現類
public class ConcreteFactory implements Factory{
public Production product(String type){
if(type.equals("email")){
return new ConcreteProduction_1();
}else if(type.equals("sms")){
return new ConcreteProduction_2();
}else{
System.out.println("請選擇正確的類型");
return null;
}
}
}
//產品實現類1
public class ConcreteProduction_1 implements Production{
public void des(){
System.out.println("使用短信發送");
}
}
//產品實現類2
public class ConcreteProduction_2 implements Production{
public void des(){
System.out.println("使用郵件發送");
}
}
//測試主方法
public static void main(String[] args) {
Factory factory = new ConcreteFactory();
Production email = factory.product("email");
Production sms = factory.product("sms");
email.des();
sms.des();
}
- 輸出如下
二、工廠模式
- 小工廠,將生成不同產品的工廠分別抽象出來,再根據不同的工廠生產產品
- 源碼如下
//工廠接口
public interface Factory {
Production product();
}
//產品接口
public interface Production {
void des();
}
//工廠實現類1
public class ConcreteFactory_1 implements Factory{
public Production product(){
return new ConcreteProduction_1();
}
}
//工廠實現類2
public class ConcreteFactory_2 implements Factory{
public Production product(){
return new ConcreteProduction_2();
}
}
//產品實現類1
public class ConcreteProduction_1 implements Production {
public void des(){
System.out.println("生產芒果飲料。。。");
}
}
//產品實現類2
public class ConcreteProduction_2 implements Production {
public void des(){
System.out.println("生成蘋果飲料。。。");
}
}
//測試主方法
public static void main(String[] args) {
Factory mangoFactory = new ConcreteFactory_1();
Factory appleFactory = new ConcreteFactory_2();
Production mango = mangoFactory.product();
Production apple = appleFactory.product();
mango.des();
apple.des();
}
- 輸出如下
三、抽象工廠模式
- 抽象工廠,在工廠的概念上再抽象一層,即生成工廠的工廠,被生成的工廠再生成相應的產品
- 源碼如下
//工廠抽象類
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
//顏色接口
public interface Color {
void fill();
}
//形狀接口
public interface Shape {
void draw();
}
//工廠制造者
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}else{
System.out.println("::請輸入正確工廠名稱");
return null;
}
}
}
//實現工廠之顏色工廠
public class ColorFactory extends AbstractFactory{
@Override
public Shape getShape(String shape) {
return null;
}
@Override
public Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("Red")){
return new Red();
}else if (color.equalsIgnoreCase("Green")){
return new Green();
}
return null;
}
}
//實現工廠之形狀工廠
public class ShapeFactory extends AbstractFactory{
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
}else if (shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
public Color getColor(String color){
return null;
}
}
//具體形狀之圓形實現類
public class Circle implements Shape{
public void draw(){
System.out.println("Inside Circle::draw() method.");
}
}
//具體形狀之正方形實現類
public class Square implements Shape{
public void draw() {
System.out.println("Inside Square::draw() method");
}
}
//具體形狀之長方形實現類
public class Rectangle implements Shape{
public void draw(){
System.out.println("Inside Rectangle::draw() method...");
}
}
//具體顏色之紅色實現類
public class Red implements Color{
public void fill(){
System.out.println("Inside Red:fill() method.");
}
}
//具體顏色之綠色實現類
public class Green implements Color{
public void fill(){
System.out.println("Inside Green::fill() method");
}
}
//測試主方法
public static void main(String[] args) {
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
Shape circle = shapeFactory.getShape("Circle");
Color red = colorFactory.getColor("red");
circle.draw();
red.fill();
}
- 輸出如下
總結
- 三者的關系是,簡單工廠最簡單,工廠將簡單工廠的分支抽象成具體的工廠,抽象工廠將實現的小工廠集合起來;當然,可以繼續往上抽象————生產出生產不同類別工廠的工廠(可以有多個,依據統一接口或者抽象類實現)
以上
希望對大家有所幫助