Design Pattern Master-Factory mode

Source: Internet
Author: User

A few days ago wrote a singleton mode, the response is flat, probably because the online design model is really rotten street, can not get to the reader's point, but also is their own knowledge of the summary, today we change the angle to say this Factory mode, Factory mode, the main three kinds of, simple factory, ordinary factory, Abstract Factory mode, today we do not talk about the abstract factory model, we say simple factory and ordinary factory design patterns. Today we are going to introduce another protagonist, his name is--Xiao Chen.

Xiao Chen is a designer, he is very fond of doing small things, those gadgets are very beautiful, one day, his good friends to play at home, see the little Chen do those small thing, like very much, have to please small Chen do little thing to him, Xiao Chen peacetime Gudaorechang, a promise, wait until friends go, his head is a bit big , Xiao Chen is currently good at doing gadgets are, glass, knight badge, ancestral phone shell ..., and his few good friends want to get these three, and there are ten good friends want, and promised others, small Chen head very big, but since promised others, will do, and then Xiao Chen began to do. The code is roughly as follows:

 1  public  void   Xiaochenmake () { 2  for  (int  i=0;i<10;i++) { Span style= "color: #008080;" >3   Xiaochen.doglass ();  4   Xiaochen.dophoneshell ();  5   Xiaochen.dochopsticks ();  6   7
     } 

This looks like the small Chen spent a week to finish the supplies of 10 people, this can be tired of Xiao Chen, but friends get are very happy, although know spread like wildfire hundred, because Xiao Chen do the Knight team crest .... Ancestral Phone shell .... Too exquisite, resulting in more and more people come to visit please Xiao Chen do, and even some people at high prices hope to get the ancestral phone shell technology, because just with their ancestral mobile phone foil secret recipe combination ... This can Huanghui small Chen, at this time someone suggested, let Xiao Chen will own technology bigger and stronger, mass production, open shop, do micro-business, as CEO, marry mating, said little Chen a little excited when, Xiao Chen thought, this to mass production, must find a factory to produce Ah, At this time Xiao Chen went out to contact the manufacturers, but because Xiao Chen is self-employed, big factory too little Chen do things too little, refused to take over his project, forced ..... Xiao Chen had to find a small factory to do, small Chen painstakingly found a small factory willing to take over their production needs, so Xiao Chen put his own drawings to the small factory to produce, two days later, has produced 300 sets of people part of the product, Chen curious, how they are so quickly made out.

Xiao Chen at this time into the factory inside a look, originally they put the drawings into the machine, and then the machine will be based on different drawings to produce different products. The specific code is as follows:

Because the phone case, the Cup, the Knight crest are all from the design drawing, we pick a view, the drawing is equivalent to a product interface, everything needs from this interface (drawing),

1  Public Interface Product {2      Public void Make (); 3 }

Knight Crest is also a product, need to have the drawings of the product, this drawing is the interface of products,

1  Public class Implements product{2    @Override3public     void make () {  4         System.out.println ("Knight Crest was made!") ); 5     }6 }

And the code for this little workshop is as follows:

1  Public classSimplefactory {2 3     Privatesimplefactory () {}4     5      Public StaticProduct getproduct (String productName) {6         if("Glass". Equals (ProductName)) {7             return NewGlass ();8}Else if("Phoneshell". Equals (ProductName)) {9             return NewPhoneshell ();Ten}Else if("CAVALIERSBADG". Equals (ProductName)) { One             return NewPhoneshell (); A         } -         return NULL; -     } the}

Because the factory is not everyone can have, must not be custom production, so must be private, at this time we only need to pass in the name of the drawing, this small factory will be based on the drawings to get the product, of course, no drawings of the product, of course, is not production, so the return of course is a null product.

The approximate class diagram is as follows

(Image from Baidu Encyclopedia)

Xiao Chen looked at this all of a sudden, the three kinds of products suddenly out, Xiao Chen very happy, hurriedly want to expand the product line, too hectic and design a lot of product design drawings, when he put 20 kinds of product design drawings to this small factory, small factory blankly shook his head, Originally their factory can not support the production of so many products, because every time if you want to add drawings to the factory, it will be the entire production line to be demolished again (in violation of the software design of the opening and closing principle), which makes the small Chen very big, this time let 20 kinds of product design drawings no authorizing, But at present there are three kinds of products, Xiao Chen hurriedly put the three kinds of products on the shelves, first of all to see how the sales to further plan.

To know what the next next, and listen to the following decomposition.

  

  

  

Design Pattern Master-Factory mode

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.