"Simple factory and Singleton" in object-oriented programming mode, object-oriented programming Factory

Source: Internet
Author: User

"Simple factory and Singleton" in object-oriented programming mode, object-oriented programming Factory

Simple Factory Pattern ):

Defines a factory class. It can return instances of different classes according to different parameters. The created instances usually share the same parent class.

 

In simple Factory mode, the Method used to create an instance is usually the static Method. Therefore, it is also called the Static Factory Method mode.

Key Point: If you need anything, you only need to pass in a correct parameter to obtain the desired object without knowing the creation details.

 

The Code is as follows:

The simple factory model contains the following three roles:

Factory (Factory role)

Product (Abstract Product role)

ConcreteProduct (specific product role)

// Typical abstract Product Code:
Abstract class Product {// public business methods of all Product classes public void MethodSame () {// implementation of public methods} // declare abstract Business Methods public abstract void MethodDiff ();}

 

// Typical Product code: class ConcreteProductA: Product {// implement the business method public override void MethodDiff () {// implement the business method }}

 

// Typical Factory Code: class Factory {// static Factory method public static Product GetProduct (string arg) {Product product = null; if (arg. equals ("A") {product = new ConcreteProductA (); // initialize the set product} else if (arg. equals ("B") {product = new ConcreteProductB (); // initialization settings product} return product ;}}

 

// Client code: class Program {static void Main (string [] args) {Product product; product = Factory. getProduct ("A"); // create the product object through the factory class. methodSame (); product. methodDiff ();}}
// The relationship between two classes A and B should be that A creates B or A uses B, but not both. Separating object creation and use makes the system more compliant with the single responsibility principle and facilitates function reuse and system maintenance.
 

 

 

Singleton pattern ):

That is, a class has only one instantiated object and provides a global access point to access this instance.

1. This class can only have one instance.

2. You must create this instance on your own.

3. You must provide this instance to the entire system.

Structure of Singleton mode:

 

Implementation of Singleton mode: 1. private constructor 2. Static private member variable (its own type) 3. Factory method of static public

 

// Implementation Method of Singleton Mode
Class Singleton {private static Singleton instance = null; // static private member variable // private constructor private Singleton () {}// static public factory method, return public static Singleton GetInstance () {if (instance = null) instance = new Singleton (); return instance ;}}

 

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.