The difference between a simple factory and a single row of design patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.