1. What is design mode
* Resolution: propose specific solutions for specific issues
A simple Factory is not one of the four design patterns--just an abstract factory
2. What is a simple factory?
01. The concept of a simple factory
An ordinary class that defines a method in the class that is decorated with a static (static) value that returns the type of the parent class, usually with a parameter.
02. Code Examples
Public classPizzafactory {//A method that defines a static and returns a value type that is a parent class carries a parameter Public StaticPizza getinstance (stringtype) { //01. Define a container for the parent class type to hold the corresponding subclass objectPizza pizza=NULL; Switch(type) { Case "Cheese Pizza": Pizza=NewCheesepizza (); Break; Case "Bacon Pizza": Pizza=NewPgpizza (); Break; } returnPizza; } }
03. Code for parent and child classes
--Parent Class Public Abstract classPizza//abstract keyword to be modified as an abstraction class { Public Abstract stringInfo ();//abstract methods--left to subclasses to rewrite }--sub-class Public classPgpizza:pizza//Inheritance { Public Override stringInfo ()//overridden The method { return "Hello, bacon pizza order complete! "; }--another sub-class Public classCheesepizza:pizza { Public Override stringInfo () {return "Hello, Cheese pizza successfully ordered"; }
3. Single case
01. Brief Description
Similar to a simple factory, only a single case can only think of memory to request a space, equivalent to only new once.
In a normal class, the class is defined by a static variable,and then the non-parametric constructed modifier of the class (public) is changed to private.
A static method is defined, the return value type is the class, and the class is finally judged whether it has new, that is, to the memory to request space, if not new, otherwise return
Variables of this class can be.
02. The code is shown below
Public classPerson { Public StaticPerson p;//Static Variables PrivatePerson ()//Private Constructors { } Public StaticPerson getinstance ()//static method return value type person { if(p==NULL)//Judging{p=NewPerson ();//Ask for Space } returnP//returns the variable of the class } }
If you feel any shortcomings, please leave your point of view, I hope we can learn the knowledge 、、、
The difference between a simple factory and a single row of design patterns