Design Patterns factory models start with selling meat clips

Source: Internet
Author: User

 

Today, we will continue our design model journey and bring you the factory model. Let's briefly list the families of this model:

1. Static factory Mode

2. Simple factory Model

3. Factory method mode

4. Abstract Factory Model

Do you think I can list so many categories in this factory? Haha, I will start to break through each other.

1. Static factory Mode

This is the most common example: helper class in the project, TextUtil. isEmpty, class + static method. The following describes in detail.

2. Simple factory Model

Next, let's talk about selling meat clips. Recently, programmers are very popular selling meat clips. When the scalpers are sold, I will wait for them to rise. Haha.

First you have to have a store: RoujiaMoStore

 

Package com. zhy. pattern. factory. a; public class RoujiaMoStore {/*** sell different meat clips based on input types ** @ param type * @ return */public RouJiaMo sellRouJiaMo (String type) {RouJiaMo rouJiaMo = null; if (type. equals (Suan) {rouJiaMo = new SuanRouJiaMo ();} else if (type. equals (Tian) {rouJiaMo = new TianRouJiaMo ();} else if (type. equals (La) {rouJiaMo = new LaRouJiaMo ();} rouJiaMo. prepare (); rouJiaMo. fire (); rouJiaMo. pack (); return rouJiaMo ;}}

Then you have to have the following flavors:

 

 

Package com. zhy. pattern. factory. a; public abstract class RouJiaMo {protected String name;/*** preparations */public void prepare () {System. out. println (kneading meat-prepare);}/*** use your dedicated bag-pack */public void pack () {System. out. println (meat folder-dedicated bag-packaging);}/*** secret-Baking 2 minutes */public void fire () {System. out. println (meat folder-dedicated device-baking );}}

Package com. zhy. pattern. factory. a; import com. zhy. pattern. factory. a. rouJiaMo;/*** spicy meat folder ** @ author zhy **/public class LaRouJiaMo extends RouJiaMo {public LaRouJiaMo () {this. name = spicy meat sandwich ;}}

Package com. zhy. pattern. factory. a;/*** sour meat sandwich ** @ author zhy **/public class SuanRouJiaMo extends RouJiaMo {public SuanRouJiaMo () {this. name = sour meat sandwich ;}}

Package com. zhy. pattern. factory. a;/*** sour meat sandwich ** @ author zhy **/public class SuanRouJiaMo extends RouJiaMo {public SuanRouJiaMo () {this. name = sour meat sandwich ;}}

Although such a design can support selling meat clips, it is a bit difficult. The type of the production category is too high to your RoujiaMoStore. If you add several flavors, delete them, you have to modify the method in sellRouJiaMo all the time, so we need to make some changes, and the simple factory mode will be used in this case.

 

We started to write a simple factory to take out the pipeline generation process:

 

package com.zhy.pattern.factory.a;public class SimpleRouJiaMoFactroy{public RouJiaMo createRouJiaMo(String type){RouJiaMo rouJiaMo = null;if (type.equals(Suan)){rouJiaMo = new SuanRouJiaMo();} else if (type.equals(Tian)){rouJiaMo = new TianRouJiaMo();} else if (type.equals(La)){rouJiaMo = new LaRouJiaMo();}return rouJiaMo;}}

Then let the Store use the following methods in combination:

 

 

Package com. zhy. pattern. factory. a; public class RoujiaMoStore {private SimpleRouJiaMoFactroy factroy; public RoujiaMoStore (SimpleRouJiaMoFactroy factroy) {this. factroy = factroy;}/*** sell different meat clips based on input types ** @ param type * @ return */public RouJiaMo sellRouJiaMo (String type) {RouJiaMo rouJiaMo = factroy. createRouJiaMo (type); rouJiaMo. prepare (); rouJiaMo. fire (); rouJiaMo. pack (); return rouJiaMo ;}}

Now you can add shards of any type. Deleting shards of any type is irrelevant to the Store ~ Are people only responsible for selling sellers ~ This is a simple factory model. Of course, everyone is familiar with it.

 

3. Factory method mode

Definition:Defines an interface for object creation, but the subclass determines the class to be instantiated. The factory method mode delays the class instantiation process to the subclass.

Now, after reading the definition, let's use an example. As the simple factory model was used, the meat folder was doing well, so the download decided to open a branch in Xi'an and a branch in Beijing. Now that there is a branch, the main store is Abstract:

 

Package com. zhy. pattern. factory. b; public abstract class RoujiaMoStore {public abstract RouJiaMo createRouJiaMo (String type ); /*** selling different meat clips based on input types ** @ param type * @ return */public RouJiaMo sellRouJiaMo (String type) {RouJiaMo rouJiaMo = createRouJiaMo (type); rouJiaMo. prepare (); rouJiaMo. fire (); rouJiaMo. pack (); return rouJiaMo ;}}

Start two branches and use a code for demonstration. The rest are the same:

 

 

Package com. zhy. pattern. factory. b;/*** Xi'an meat folder store ** @ author zhy **/public class XianRouJiaMoStore extends RoujiaMoStore {@ Overridepublic RouJiaMo createRouJiaMo (String type) {RouJiaMo rouJiaMo = null; if (type. equals (Suan) {rouJiaMo = new XianSuanRouJiaMo ();} else if (type. equals (Tian) {rouJiaMo = new XianTianRouJiaMo ();} else if (type. equals (La) {rouJiaMo = new XianLaRouJiaMo () ;}return rouJiaMo ;}}

Then there are meat clips of different Xi'an tastes, and this Code will not be pasted. We can see that the process of making a meat folder is determined by the subclass in the form of an abstract method:

 

1. defines an interface for object creation: public abstract RouJiaMo createRouJiaMo (String type );

2. classes that are instantiated by subclass. We can see that our subtype is generated by subclass.

Some people may say that I can use the simple factory model, but if there are five flavors/cities in 10 cities, isn't it necessary to have more than 50 if in a simple factory, in addition, the Xi'an meat folder branch cannot have its own secrets. Of course, it is the best choice.

Now, the method factory model has been introduced.

4. Abstract Factory Model

Definition:Provides an interface for creating related or dependent object families without specifying specific classes.

This definition is a bit difficult. Forget it. Let's take an example. We continue to sell meat clips. If our business is so good, it is inevitable that some branches will start to shake their minds and start to use inferior meat to hit our brand. Therefore, we need to take the money to build our own raw materials field in each city to ensure the supply of high-quality raw materials.

So we create an interface to provide raw materials:

 

Package com. zhy. pattern. factory. b;/*** provide raw materials for the Meat folder * @ author zhy **/public interface RouJiaMoYLFactroy {/*** production Meat * @ return */public Meat createMeat (); /*** @ return */public YuanLiao createYuanliao ();}

Package com. zhy. pattern. factory. b;/*** provide the two materials according to the local characteristics of Xi'an * @ author zhy **/public class XianRouJiaMoYLFactroy implements RouJiaMoYLFactroy {@ Overridepublic Meat createMeat () {return new FreshMest () ;}@ Overridepublic YuanLiao createYuanliao () {return new XianTeSeYuanliao ();}}

With the principle factory, We Can slightly modify the RouJiaMo prepare method:

 

 

Package com. zhy. pattern. factory. b; public abstract class RouJiaMo {protected String name;/*** preparations */public final void prepare (RouJiaMoYLFactroy ylFactroy) {Meat meat = ylFactroy. createMeat (); YuanLiao yuanliao = ylFactroy. createYuanliao (); System. out. println (using official raw materials + meat +, + yuanliao + as raw materials to make meat clips);}/*** use your dedicated bag-pack */public final void pack () {System. out. println (meat folder-dedicated bag-packaging);}/*** secret-Baking 2 minutes */public final void fire () {System. out. println (meat folder-dedicated device-baking );}}

Now, we must use our official raw materials as raw materials.

 

Comparison definition:

1. provide an interface: public interface RouJiaMoYLFactroy

2. Create the related or dependent object family public Meat createMeat (); public YuanLiao createYuanliao (); our interface is used to create a series of raw materials.

Okay. Finally, I want to test it. I want to buy a sour taste at Xi'an taobaodian:

 

package com.zhy.pattern.factory.b;public class Test{public static void main(String[] args){RoujiaMoStore roujiaMoStore = new XianRouJiaMoStore();RouJiaMo suanRoujiaMo = roujiaMoStore.sellRouJiaMo(Suan);System.out.println(suanRoujiaMo.name);}}

Use the official raw material com. zhy. pattern. factory. b. freshMest @ e53108, com. zhy. pattern. factory. b. xianTeSeYuanliao @ f62373 as raw material for making meat clips-Special Equipment-baking meat clips-Special bags-packaging Sour Meat clips

 

Haha ~ The meat folder store has already been established ~ Remember to leave a comment and give a thumbs up ~

 

 

 

 

 

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.