Design Pattern-Factory-Design Pattern-Factory

Source: Internet
Author: User

Create All kinds of guns.

Design focus:

1 base class as interface

2. Inheritance classes are different classes.

3. Construct a factory class and create different classes as needed. You can input keywords or indexes.


# Pragma once # include
 
  
# Include
  
   
Using namespace std; // Base classclass Gun {public: virtual string description () {return "Generic Gun" ;}}; // Derived classclass MachineGun: public Gun {public: string description () {return "MachineGun-Gun for fast shoot" ;}}; class LaserGun: public Gun {public: string description () {return "LaserGun-Melt everything with heat" ;}}; class ShockwaveGun: public Gun {public: string description () {return "ShockwaveGun-shock down everything ";}}; class GofFactory_Gun {public: GofFactory_Gun () {} Gun * createGun (const string & type) {if ("MachineGun" = type) {return (new MachineGun );} else if ("LaserGun" = type) {return (new LaserGun);} else if ("ShockwaveGun" = type) {return (new ShockwaveGun );} else {return NULL ;}}; void GofFactory_Gun_Run () {GofFactory_Gun gofGun; Gun * g = gofGun. createGun ("MachineGun"); cout <
   
    
Description () <
    
     
Description () <
     
      
Description () <
      
       
Result:



Related Article

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.