Learn design patterns from Tensorflow source code

Cloud.tencent.com/developer/a…

conclusion

  • Simple Factory pattern: One factory class, one product abstract class.

The simple factory pattern has a unique factory class, which is created using if-else conditions based on the parameters passed in to determine what product objects are ultimately created.

  • Factory method pattern: Multiple factory classes, one product abstraction class.

The factory method pattern, in which multiple factory classes implement factory interfaces, utilizes polymorphism to create different product objects, thereby avoiding lengthy if-else condition judgments.

  • Abstract Factory pattern: Multiple factory classes, multiple product abstract classes.

The abstract factory pattern reduces the number of factory subclasses by grouping product subclasses into groups where different products in the same group are created by different methods of the same factory subclass.

The body of the

The core content is extracted from “Programmer Grey”

  • Q: How do you normally create objects

A: Just new an object

  • Q: If there are some initialization operations for creating an object, where do they happen?

A: Write it in the constructor

class Mask {
public:
    Mask() {// 5 lines of initialization code
        / /...}}Copy the code
  • Q: If the initialization operation has 100 lines, such as associating other objects, looking up the database, looking up the configuration file, are they still in the constructor?

A: It would be too long to write in the constructor. Instead, write A special class, factory class, for initialization

class MaskFactory {
public: 
    Mask createMask(a) {
        Mask mask = new Mask(a);// 100 lines of initialization code
        / /...
        returnmask; }}Copy the code
  • Q: How do I create an object if there is a subclass of Mask?

A: Pass the subclass type as A parameter to the factory, and the factory will go into different branches for creation based on the subclass type

This is the simple factory pattern

class HighEndMask::Mask {
public:HighEndMask() {
        printf("I'm a high-end mask."); }}class LowEndMask::Mask {
public:LowEndMask() {printf("My low-end mask."); }}Copy the code
class MaskFactory{
public:
    Mask createMask(String type) {
        Mask mask = null;
        if("High-end masks" == type){
            mask = new HighEndMask(a);/ /...
            // 100 lines of initialization code for HighEndMask
        }else if("Low-end mask" == type){
            mask =  new LowEndMask(a);/ /...
            // 100 lines of initialization code for LowEndMask
        }
        returnmask; }}Copy the code
int main(String[] args) {
        MaskFactory factory = new MaskFactory(a); Mask maskA = factory.createMask("High-end masks");
        Mask maskB = factory.createMask("Low-end mask"); }}Copy the code
  • Q: To add a new type of mask, it is necessary to add a branch in the factory, which does not conform to the object-oriented open-closed principle. How to improve it?

A: You can create A factory subclass for each mask subclass that implements an abstract factory interface using dynamic polymorphism

This is the factory method pattern

class HighEndFactory::MaskFactory{
public:
    Mask createMask(a) {
        Mask mask =  new HighEndMask(a);/ /...
        // 100 lines of initialization code for HighEndMask
        returnmask; }}class LowEndFactory::IMaskFactory{
public:
    Mask createMask(a) {
        Mask mask =  new LowEndMask(a);/ /...
        // 100 lines of initialization code for LowEndMask
        returnmask; }}Copy the code
int main(String[] args) {
        MaskFactory factoryA = new LowEndFactory(a); MaskFactory factoryB =new HighEndFactory(a); Mask maskA = factoryA.createMask(a); Mask maskB = factoryB.createMask(a); }Copy the code
  • Q: Creating a factory for each subclass is too cumbersome

A: In order to avoid the increasing number of factory subclasses, abstract factory pattern is introduced to abstract factory subclasses. Grouping product classes, in which different products correspond to different methods of the same factory class

This is the abstract factory pattern

// Product class code
class LowEndMask::Mask {
    void showMask(a){
        printf("My low-end mask."); }}class HighEndMask::Mask {
    void showMask(a) {
        printf("I'm a high-end mask."); }}class LowEndProtectiveSuit::ProtectiveSuit {
    void showSuit(a) {
        println("I'm a low-end suit."); }}class HighEndProtectiveSuit::ProtectiveSuit {
    void showSuit(a) {
        printf("I'm a high-end protective suit."); }}Copy the code
// Factory class code

class LowEndFactory::Factory {
    Mask createMask(a) {
        Mask mask =  new LowEndMask(a);/ /...
        // 100 lines of initialization code for LowEndMask
        return mask;
    }

    ProtectiveSuit createSuit(a) {
        ProtectiveSuit suit =  new LowEndProtectiveSuit(a);/ /...
        // LowEndProtectiveSuit 100 lines of initialization code
        returnsuit; }}class HighEndFactory::IFactory {
    Mask createMask(a) {
        Mask mask =  new HighEndMask(a);/ /...
        // 100 lines of initialization code for HighEndMask
        return mask;
    }

    ProtectiveSuit createSuit(a) {
        ProtectiveSuit suit =  new HighEndProtectiveSuit(a);/ /...
        // HighEndProtectiveSuit 100 lines of initialization code
        returnsuit; }}Copy the code
// Client code
int main(String[] args) {
        Factory factoryA = new LowEndFactory(a); Factory factoryB =new HighEndFactory(a);// Create a low-end mask
        Mask maskA = factoryA.createMask(a);// Create high-end masks
        Mask maskB = factoryB.createMask(a);// Create a low-end protective suit
        ProtectiveSuit suitA = factoryA.createSuit(a);// Create a high-end protective suit
        ProtectiveSuit suitB = factoryB.createSuit(a); maskA.showMask(a); maskB.showMask(a); suitA.showSuit(a); suitB.showSuit();
    }
}
Copy the code