Factory pattern (C++)

definition

Define an interface for creating objects and let subclasses decide which class to instantiate. Factory Method makes the instantiation of a class defer (purpose: decoupling, means: virtual function) to subclasses.

Application Scenario

  • In software systems, we are often faced with the task of creating objects; due to changes in requirements, the specific types of objects that need to be created often change.
  • How to deal with this change? How to bypass the conventional object creation method (new) and provide an "encapsulation mechanism" to avoid the tight coupling between the client program and this specific object creation work?

structure

insert image description here

code example

//Factory.h
/****************************************************/
#ifndef FACTORY_H
#define FACTORY_H
#include<iostream>
using namespace std;

class Shape
{
    
    
public:
	Shape() {
    
    };
	virtual ~Shape() {
    
    };
	virtual void draw() = 0;
 
	enum Shape_type{
    
     Rectangle , Square, Circle};
};
 
class Rectangle :Shape
{
    
    
public:
	Rectangle() {
    
    };
	~Rectangle() {
    
    };
	void draw() {
    
     cout << "Inside Rectangle::draw() method." << endl; };
};
 
class Square :Shape
{
    
    
public:
	Square() {
    
    };
	~Square() {
    
    };
	void draw() {
    
     cout << "Inside Square::draw() method." << endl; };
};
 
class Circle :Shape
{
    
    
public:
	Circle() {
    
    };
	~Circle() {
    
    };
	void draw() {
    
     cout << "Inside Circle::draw() method." << endl; };
};
 
 
class ShapeFactory
{
    
    
public:
	ShapeFactory() {
    
    };
	~ShapeFactory() {
    
    };
 
	Shape* getShapeFactory(Shape::Shape_type type);
};
 
 
Shape* ShapeFactory::getShapeFactory(Shape::Shape_type type)
{
    
    
	switch (type)
	{
    
    
	case Shape::Rectangle:
		return (Shape*)new Rectangle();
	case Shape::Square:
		return (Shape*)new Square();
	case Shape::Circle:
		return (Shape*)new Circle();
	default:
		return NULL;
	}
}

#endif

//test.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Factory.h"
int main()
{
    
    
	ShapeFactory shapefac;
	Shape *t1 =(Shape*) shapefac.getShapeFactory(Shape::Circle);
	Shape *t2 = (Shape*)shapefac.getShapeFactory(Shape::Circle);
	Shape *t3 = (Shape*)shapefac.getShapeFactory(Shape::Circle);
 
	t1->draw();
	t2->draw();
	t3->draw();
 
	delete t1;
	t1 = NULL;
 
	delete t2;
	t2 = NULL;
 
	delete t3;
	t3= NULL;
	
	return 0;
}

operation result
insert image description here

Summary

  • The Factory Method pattern is used to isolate the coupling relationship between the user of the class object and the concrete type. In the face of a frequently changing concrete type, the tightly coupled relationship (new) can lead to software brittleness.
  • The Factory Method pattern delays the work of specific objects to be created to subclasses through object-oriented methods, so as to implement an expansion (rather than change) strategy, which better solves this tight coupling relationship.
  • The Factory Method pattern solves the requirement change of "single object". The disadvantage is that the creation method/parameters are required to be the same.

Guess you like

Origin blog.csdn.net/weixin_47424753/article/details/132125519