Design Pattern Master--Abstract Factory mode

Source: Internet
Author: User

In other words, the last time Xiao Chen since the production line from small workshops to large factories, performance is rapid, Xiao Chen is a source of money rolling, orders is one after another, integrity NBA fiery, especially the NBA series of products sold quite hot, then there is a thing let Xiao Chen puzzled, because in this side, Xiao Chen's current production line is only for a single production, the Badge factory production of the crest, jerseys factory production of jerseys, shoes factory production of sneakers, while Xiao Chen in the performance of the increase in the performance of the Big data analysis, he found that the Knight's badge has bought 99.99999% of people will buy the knight uniforms, And the rider's sneakers, and the people who bought the Thunder badge will buy the Thunder team uniforms and the Thunder team's basketball.

This let Xiao Chen headache is, because the factory production of products single and scattered , each time the delivery, we have to wait for them from different places to the store, and then by the clerk to assemble , so, not only time-consuming, but also small Chen's human costs are rubbing against the upward, this does not, Chen's staff began to complain to Xiao Chen, "Chen General, you see us every day to assemble nearly 10,000 sets of products, employees have been tired desperate, many people are going to resign, you hurriedly to think of ways to do it." , Xiao Chen at this time anxious, quickly to the first-line staff to appreciate the increase, only temporarily appease their emotions.

At this time, Xiao Chen also contacted the factory supervisor, told him the current difficulties, asked whether there is a better way. At this time their supervisor to small Chen: "You this problem before we met, we must have already prepared the plan, like our company headquarters over a series of packaged products, such as, a product family of products are moved to the same factory production, and assembled, like sneakers, Jerseys, basketball belongs to a class of products, different product families correspond to different factories, come, I will show you, in our factory, for the product Family Factory architecture diagram. Then the factory supervisor gave Xiao Chen a picture of the production plant of the product family.

"You see, if you want to produce a series of product family products, you just have to put the product line together." "Now let's take a look at the template code for the specific product line: for example, we're going to produce a knight's product and the Thunder Team product, and the product includes a badge and sneakers for the time being.

1, an abstract interface, specifically for the production of logos and sneakers, but the specific implementation of the following sub-factory.

1  Public Interface abstractfactory {2      Public abstractproduct Createbadge (); 3     4      Public abstractproduct createshoes (); 5 }

2, the product production of drawings, here only contains the specific product information, if necessary to have a specific method, then add it yourself.

1  Public Interface abstractproduct {2      Public void ProductInfo (); 3 }

3. Cleveland and Thunder's product factory, specializing in the Cleveland Cavaliers crest and sneakers and thunder team crest and sneakers

1  Public classCavalierproductfactoryImplementsabstractfactory{2 @Override3      Publicabstractproduct Createbadge () {4         return NewCavalierbadge ();5     }6 7 @Override8      Publicabstractproduct createshoes () {9         return Newcavaliershones ();Ten     } One}
1  Public classThunderproductfactoryImplementsabstractfactory{2 3 @Override4      Publicabstractproduct Createbadge () {5         return NewThunderbadge ();6     }7 8 @Override9      Publicabstractproduct createshoes () {Ten         return Newthundershoes (); One     } A}

The crest and sneakers above are implemented from the Abstractproduct interface, taking one example.

1  Public class Implements abstractproduct{2    @Override3public     void ProductInfo () {4         System.out.println ("This is a cavalier badge!" ); 5     }6 }

Now two factories can start to implement their respective products, as described in the previous article, we can write the corresponding factory to the specific configuration file, and then according to the reflection to implement the specific factory.

1 @Test2      Public voidClient () {3Cavalierproductfactory cavalierfactory = (cavalierproductfactory) xmlutils.getfactory ("CavalierProductFactory");4Thunderproductfactory thunderfactory = (thunderproductfactory) xmlutils.getfactory ("ThunderProductFactory");5         6Abstractproduct Cavalierbadge =Cavalierfactory.createbadge ();7Abstractproduct cavaliershoes =cavalierfactory.createshoes ();8         9Abstractproduct Thunderbadge =Thunderfactory.createbadge ();TenAbstractproduct thundershoes =thunderfactory.createshoes (); One}

In this way, we can produce the corresponding product family for one by one.

Xiao Chen looked, very happy, so that can save a lot of their own labor costs. Products are divided into a class, but Xiao Chen thought, if the production of different teams alone need a factory, then the cost is too big, each time to create a new factory, the director silently nodded, said:

"Of course, if more subdivision, in fact, the knight's crest and shoes and the thunder of the badge and shoes are in the same big category, so that we can add branches inside the factory to choose, then, only need to use a factory product on it, so that our production line is not standardized, will become a small workshop of the work mode, but sometimes we will do for the cost, do not produce too many unnecessary factories to carry out this mode of work, so to see whether it is to expand the product for expansion, or to reduce the cost (the expansion of the Class), But sometimes when we start building a factory, we first consider the scale of production (the scale of the project) to merit, or to center the choice. ”

Xiao Chen listened, plunged into a meditation, it seems that enterprises to bigger and stronger is not easy ah, consider things to all aspects ah, but now that my daily product has reached 100,000/day of sales, then I first grasp the goods, the first to expand some factories, add products, began to engage. Xiao Chen in his own business road and took a solid step, the next little Chen will be in the business of the road encountered what twists and turns?

To know how the funeral, and listen to the following decomposition.

Design Pattern Master--Abstract 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.