The bitter force also chats the pattern--(1)--Simple factory

Source: Internet
Author: User

Since it is necessary to mention the factory model, we need to mention simple factories.

Simple Factory mode: is the creation mode of a class, or it can be called a static factory.

Can be understood so.

A factory can produce a category of products that must belong to the category. Just as the aircraft belongs to the aircraft and the eggs are not the same class.

Now take the aircraft as an example

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Consoleapplication1{class Program {static void Main (string[] args) {Aircraft A = Aircraf            Tfactory.create (1);            A.fly ();        A.inventor ();    }} public interface aircraft {void Fly ();//aircraft characteristics, to fly void Inventor ();//inventors, may have a brand or something will not write}        public class Airplane:aircraft {public void Fly () {Console.WriteLine ("I am flying in the sky");        } public void Inventor () {Console.WriteLine ("Earth Man");        }} public class Ufo:aircraft {public void Fly () {Console.WriteLine ("I am flying in space");        } public void Inventor () {Console.WriteLine ("Alien");  }} public class Aircraftfactory {public static aircraft create (int ") {if (what = 1) return new AiRplane ();            else if (what = = 2) return new UFO ();        else return null; }    }}

For example: The subtraction of a calculator can use this mode

The bitter force also chats the pattern--(1)--Simple factory

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.