Design Patterns factory models start with selling meat clips

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/lmj623565791/article/details/24460585

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 that I can list so many categories in this factory? Haha, I will try to break through them all below.

1. Static factory Mode

This is the most common example: helper class in the project, textutil. isempty, class + static method. The following is a brief introduction.

2. Simple factory Model

Let's start talking about selling meat clips. Recently, programmers are 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 = 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-prepare meat-prepare for work");}/*** use your dedicated bag-pack */Public void pack () {system. out. println ("Meat folder-dedicated bag-packed");}/*** 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 ";}}

Today's design, although it can support you to sell meat clips, is a bit of a problem, the type of production snapshot and your roujiamostore coupling is too high, suppose to add? You have to change the method in sellroujiamo to delete several flavors. Therefore, we need to make some changes, so the simple factory mode will work in handy.

We started to write a simple factory to take out the process of generating the license:

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 = factroy. createroujiamo (type); roujiamo. prepare (); roujiamo. fire (); roujiamo. pack (); Return roujiamo ;}}

Now you can join? The deletion of 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 the workshop.

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, we will use the example below to demonstrate it. 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 ); /*** sell different meat clips based on input types ** @ Param type * @ return */Public roujiamo sellroujiamo (string type) {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 = 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. The comparison definition is as follows:

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 a 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 mode is introduced.

4. Abstract Factory Model

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

This definition is a bit difficult. Forget it, let's take the example. We continue to sell meat clips. Our business is so good, it is inevitable that some branches are starting to shake their minds and start to use inferior meat and so on, hitting our brand. Therefore, we need to build our own raw materials field in every 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 slightly changed 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 = ylfactroy. createmeat (); yuanliao = ylfactroy. createyuanliao (); system. out. println ("using official raw materials" + meat + "," + yuanliao + "as raw materials for making meat clips ");} /*** use your dedicated bag-pack */public final void pack () {system. out. println ("Meat folder-dedicated bag-packed");}/*** 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.

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.

Now, let's try it out. I want to buy a sour taste at Xi'an Branch:

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 materials [email protected] and [email protected] as raw materials to create meat clips-special devices-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 ~






Design Patterns factory models start with selling meat clips

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.