Simple explanation of the C # factory model

Source: Internet
Author: User
First, simple Factory mode
The Simple factory pattern returns an instance of a class in several possible classes, based on the data supplied to it. Typically, the class it returns has a common parent class and a public method.

Simple Factory mode is not actually a member of the GOF 23 design patterns.
Second, simple factory model role and structure:
Factory class role Creator (Lightsimplefactory): The factory class creates a product object under direct control of the client (create method).
Abstract Product role Product (Light): Defines the parent class of objects created by simple factories or interfaces that they collectively own. Can be a class, abstract class, or interface.
Specific product role Concreteproduct (Bulblight, Tubelight): Defines the object that the factory specifically processes.
Iii. Examples of procedures:

Using System;   Public abstract class light{public abstract void TurnOn (); public abstract void Turnoff ();}   public class bulblight:light{public override void TurnOn () {Console.WriteLine ("Bulb Light was turned on");   } public override void Turnoff () {Console.WriteLine ("Bulb Light is turned off");    }} public class tubelight:light{public override void TurnOn () {Console.WriteLine ("Tube Light is turned on");   } public override void Turnoff () {Console.WriteLine ("Tube Light is turned off"); }} public class lightsimplefactory{Public light Create (string lighttype) {if (Lighttype = = "Bulb") Retu      RN new Bulblight ();      else if (Lighttype = = "Tube") return new Tubelight ();   else return null;       }} public class client{public static void Main () {lightsimplefactory LSF = new Lightsimplefactory (); Light L = LSF.      Create ("Bulb");      L.turnon ();       L.turnoff (); Console.WriteLine ("-----------------"); L = LSF.      Create ("Tube");      L.turnon ();   L.turnoff (); }}

Five advantages and disadvantages:
Advantages:
The factory class contains the necessary judgment logic, you can decide when to create an instance of which product class, the client can be exempted from the direct creation of product object responsibility, but only "consumption" products. The simple factory model achieves the separation of responsibilities through this approach.
Cons:
When a product has a complex multilayer hierarchy, the factory class only has its own, status quo, which is the disadvantage of the pattern. Because the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.
at the same time, the system is difficult to expand, once adding new products will have to modify the factory logic, may cause the factory logic is too complex.
In addition, the simple factory pattern typically uses a static factory approach, which makes it impossible to inherit from subclasses, resulting in a factory role that cannot form an inheritance-based hierarchy.

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.