介紹
? ? ? ? 在簡單工廠模式中,當系統(tǒng)中需要引入新的產(chǎn)品時,由于靜態(tài)工廠方法通過所傳入的參數(shù)的不同來創(chuàng)建不同的產(chǎn)品,這必定要修改工廠類的源代碼,將違背開閉原則。因此,工廠方法模式應運而生。工廠方法模式是定義一個用于創(chuàng)建對象的接口,讓子類決定將哪一個類實例化
實現(xiàn)
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Product {
public:
void methodSame();
virtual void methodDiff() = 0; // 抽象方法的聲明,由具體產(chǎn)品實現(xiàn)
};
class ConcreteProductA : public Product {
public:
void methodDiff() override;
};
class ConcreteProductB : public Product {
public:
void methodDiff() override;
};
class Factory {
public:
virtual Product *createProduct() = 0;
};
class ProductAFactory : public Factory {
public:
Product *createProduct() override;
};
class ProductBFactory : public Factory {
public:
Product *createProduct() override;
};
#endif //DESIGNPATTERNS_MYCLASS_H
myclass.cpp
//
// Created by yuwp on 2024/1/12.
//
#include "myclass.h"
#include "myclass.h"
void Product::methodSame() { // 公共方法的實現(xiàn)
std::cout << "methodSame" << std::endl;
}
void ConcreteProductA::methodDiff() {
std::cout << "ConcreteProductA" << std::endl;
}
void ConcreteProductB::methodDiff() {
std::cout << "ConcreteProductB" << std::endl;
}
Product *ProductAFactory::createProduct() {
return new ConcreteProductA();
}
Product *ProductBFactory::createProduct() {
return new ConcreteProductB();
}
main.cpp
#include <iostream>
#include <mutex>
#include "myclass.h"
int main() {
Factory *factory;
Product *product;
factory = new ProductAFactory();
product = factory->createProduct();
product->methodSame();
product->methodDiff();
delete factory;
delete product;
factory = new ProductBFactory();
product = factory->createProduct();
product->methodSame();
product->methodDiff();
delete factory;
delete product;
return 0;
}
重載工廠類方法
class Factory {
public:
virtual Product *createProduct() = 0;
virtual Product *createProduct(std::string opt); // 新增
};
隱藏工廠類創(chuàng)建產(chǎn)品實例方法
class Factory {
public:
virtual Product *createProduct() = 0;
void methodDiff() {
Product *product = this->createProduct();
product->methodDiff();
delete product;
}
};
總結
????????優(yōu)點:
? ? ? ? 1.?在工廠方法模式中,工廠方法用來創(chuàng)建客戶所需要的產(chǎn)品,同時還向客戶隱藏了哪種具體產(chǎn)品類將被實例化這一細節(jié)。用戶只需要關心所需產(chǎn)品對應的工廠,無須關心創(chuàng)建細節(jié),甚至無須知道具體產(chǎn)品類的類名。
? ? ? ? 2.?基于工廠角色和產(chǎn)品角色的多態(tài)性設計是工廠方法模式的關鍵。它能夠讓工廠可以自主確定創(chuàng)建何種產(chǎn)品對象,而如何創(chuàng)建這個對象的細節(jié)則完全封裝在具體工廠內部。工廠方法模式之所以又被稱為多態(tài)工廠模式,正是因為所有的具體工廠類都具有同一抽象父類。
? ? ? ? 3.?使用工廠方法模式的另一個優(yōu)點是在系統(tǒng)中加入新產(chǎn)品時,無須修改抽象工廠和抽象產(chǎn)品提供的接口,無須修改客戶端,也無須修改其他的具體工廠和具體產(chǎn)品,而只要添加一個具體工廠和具體產(chǎn)品就可以了。這樣,系統(tǒng)的可擴展性也就變得非常好,完全符合開閉原則。
? ? ? ? 缺點:
? ? ? ? 1.?在添加新產(chǎn)品時,需要編寫新的具體產(chǎn)品類,而且還要提供與之對應的具體工廠類,系統(tǒng)中類的個數(shù)將成對增加,在一定程度上增加了系統(tǒng)的復雜度,有更多的類需要編譯和運行,會給系統(tǒng)帶來一些額外的開銷。
? ? ? ? 2.?由于考慮到系統(tǒng)的可擴展性,需要引入抽象層,在客戶端代碼中均使用抽象層進行定義,增加了系統(tǒng)的抽象性和理解難度,且在實現(xiàn)時可能需要用到DOM、反射等技術,增加了系統(tǒng)的實現(xiàn)難度。
練習
????????使用工廠方法模式設計一個程序來讀取各種不同類型的圖片格式,針對每種圖片格式都設計一個圖片讀取器。例如,GIF圖片讀取器用于讀取GIF格式的圖片,JPG圖片讀取器用于讀取JPG格式的圖片。需充分考慮系統(tǒng)的靈活性和可擴展性。
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Reader {
public:
virtual void read(const std::string &file) = 0;
};
class GIFReader : public Reader {
public:
void read(const std::string &file) override;
};
class JPGReader : public Reader {
public:
void read(const std::string &file) override;
};
class Factory {
private:
virtual Reader *createReader() = 0;
public:
void read(const std::string &file) {
Reader *reader = this->createReader();
reader->read(file);
delete reader;
}
};
class GIFFactory : public Factory {
private:
Reader *createReader() override;
};
class JPGFactory : public Factory {
private:
Reader *createReader() override;
};
#endif //DESIGNPATTERNS_MYCLASS_H
myclass.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-816690.html
//
// Created by yuwp on 2024/1/12.
//
#include "myclass.h"
#include "myclass.h"
void GIFReader::read(const std::string &file) {
std::cout << "read GIF file: " << file << std::endl;
}
void JPGReader::read(const std::string &file) {
std::cout << "read JPG file: " << file << std::endl;
}
Reader* GIFFactory::createReader() {
return new GIFReader();
}
Reader* JPGFactory::createReader() {
return new JPGReader();
}
main.cpp文章來源地址http://www.zghlxwxcb.cn/news/detail-816690.html
#include <iostream>
#include <mutex>
#include "myclass.h"
int main() {
Factory *factory;
factory = new GIFFactory();
factory->read("file.gif");
delete factory;
factory = new JPGFactory();
factory->read("file.jpg");
delete factory;
return 0;
}
到了這里,關于《設計模式的藝術》筆記 - 工廠方法模式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!