[Toxic] simple design patterns factory pattern

Copyright: OO welcome to reprint, if I have the ability to write the original article, then ~ https://blog.csdn.net/qq_16468937/article/details/50958532

The Description // : talking nonsense

Well, I had wanted to write things that are not lazy design patterns, but since last week asked the feces Alibaba phone face I decided to take it review the design mode. . .

There is to be a Markdown editor, but that the old typesetting I feel very anxious to grasp, ranked Debu beautiful or ordinary bar ............... Orz 


// part Source:

1.C ++ realization of a simple factory pattern example Source: http: //www.jellythink.com/archives/42

2. The operation of exemplary sources: Jie Cheng - Lying Model Design


//text:

Applications simple factory design pattern - (taken from sources that some of the information 1 URLs)

1. Many of the objects in the program, the need to create, leading to multiple operations and heteroaryl new object, we need to use simple factory pattern;

2. Since the creation of objects that we do not need to care about, and we pay attention to the actual operation of the object, so we need to create separate objects and operations of two parts, so to facilitate the latter part of the program expansion and maintenance.



The following piece of code is implemented in C ++:

#include <iostream>
#include <vector>
using namespace std;

typedef enum ProductTypeTag
{
	TypeA,
	TypeB,
	TypeC
}PRODUCTTYPE;

// Here is the product class
class Product
{
public:
	virtual void Show() = 0;
};

class ProductA : public Product
{
public:
	void Show()
	{
		cout<<"I'm ProductA"<<endl;
	}
};

class ProductB : public Product
{
public:
	void Show()
	{
		cout<<"I'm ProductB"<<endl;
	}
};

class ProductC : public Product
{
public:
	void Show()
	{
		cout<<"I'm ProductC"<<endl;
	}
};

// Here is the Factory class
class Factory
{
public:
	Product* CreateProduct(PRODUCTTYPE type)
	{
		switch (type)
		{
		case TypeA:
			return new ProductA();

		case TypeB:
			return new ProductB();

		case TypeC:
			return new ProductC();

		default:
			return NULL;
		}
	}
};

int main(int argc, char *argv[])
{
	// First, create a factory object
	Factory *ProductFactory = new Factory();
	Product *productObjA = ProductFactory->CreateProduct(TypeA);
	if (productObjA != NULL)
		productObjA->Show();

	Product *productObjB = ProductFactory->CreateProduct(TypeB);
	if (productObjB != NULL)
		productObjB->Show();

	Product *productObjC = ProductFactory->CreateProduct(TypeC);
	if (productObjC != NULL)
		productObjC->Show();

	delete ProductFactory;
	ProductFactory = NULL;

	delete productObjA;
	productObjA = NULL;

	delete productObjB;
	productObjB = NULL;        

	delete productObjC;
	productObjC = NULL;

	return 0;
}

Well, that's the link to this article, you need to create an object when a lot, so in fact, the use of the plant is: use a separate class to do this to create instances of the process (from Westward design patterns)

So, in my understanding, this is a workshop ProductFactory, then there are three machines, respectively, is the production ProductA, ProductB, ProductC, we have this facility, you can go with the output of these products to the factory class (factory class method call ), in fact, doing that is new a new Product. Then you can ask the factory to pick up these products without the need for you to go directly to the factory inside the machine take these products, and carefully think about is there are so useful. (Not used in the actual, not yet know what was good, being so understanding, remember to add later ..)


This simple factory pattern then it involves a very important issue, that is, the three major characteristics of the implementation class: encapsulation, inheritance, polymorphism.

Package: Packaging Product from Product class has three subclasses

Inheritance: ProductA, ProductB, ProductC inherited from Product

Polymorphism: Subclasses override method

// better to take the opportunity to review the concept of polymorphism it:

Polymorphism: the same operation applied to different objects can have different interpretations, produce different execution result. At runtime, by the base class pointer to invoke the method in a derived class implementation.

In C ++, multi-state following methods: virtual function, an abstract class, covering, templates (regardless of overloading and polymorphism).




// then my own question here:

1.

Q: Why packaging? Implement different types create directly inside the main product is not a little more code more convenient it?

A: Package is to "separation of interface from implementation," indeed, increase the amount of code, but the program easy to maintain, easy to expand, and easy reuse of, in simple terms: a look at the main function is that you know he did What, do not look carefully in the end what is achieved which is the interface.


2.

Q: Why is the switch that selects productA or B or C? Why not three if?

A: branch judgment means that each condition must be determined, equal to three times the computer to do useful work.


3.

Q: Why are not all implementations are written in the Product inside, then it does not derive three subclasses, ah?

A: According to Cheng Jie big thoughts, your boss told you that ProductC production machine to re-install, then you directly to production machines C wants, do not you ask your boss to give you the whole machine shop go over do you go to get the machine C .. In other words, when you want to add or modify a Product subclass, then go directly to "derive a subclass" or "modify the corresponding sub-class" on the line ..

This is called: "Reducing the degree of coupling (loose coupling tight coupling &&)"

So to expand knowledge about slightly: "What is coupling"  

Coupling As the name suggests, is the closeness of the relationship between the two, it can be understood as the degree of interdependence.


4.

Q: So when this simple model ah?

A: In fact, the front has been said, but I understand that is often similar to those of new objects, such as addition and subtraction, multiplication and division four class object. (Remember to come back later have a better understanding of change .. I always feel so understanding not fly!)

4


Guess you like

Origin blog.csdn.net/qq_16468937/article/details/50958532