Factory model of Design Pattern
The factory mode provides interfaces (Interface Programming) for object creation ). Factory models are classified into three types based on the concepts in Java and patterns:
Simple Factory Method Abstract Factory mode three modes are gradually abstracted from top to bottom, and more general. Another kind of classification is to look at the simple factory model as a special case of the factory method model. The two are classified as one type. The following two scenarios use the factory model:
You cannot predict the type of instance to be created during encoding. The system should not rely on the details of how product instances are created, combined, and expressed.
Xiaopang loves noodles. He really likes it. He can eat noodles for a week in a row and loves his mother's hand noodles! I am now taking an internship outside. I want to save some money and get rid of the problem. I eat noodles every night. Every time I eat noodles, I want to cook it myself. This is really troublesome. When I have money, get down every day! Eat Noodles every day!
Xiaopeng is an IT guru, and now he is learning new things during his internship. One day, he thought, "I need to cook noodles by myself, and I need to find materials for what I want to eat, great trouble!" Write the Code as follows:
/** The noodles are delicious, but they cannot be eaten every day (remember to add an egg) */public class instantnodid {public void doinstantnodid () {System. out. println ("make a bubble... ");}}
/** Favorite northwest ramen, which is expensive */public class handpullednovel {public void dohandpullednovel () {System. out. println ("Do Ramen ...");}}
/** Eat noodles in the summer, put more vinegar, and make it sour */public class coldnodid {public void docoldnodid () {System. out. println ("make noodles... ");}}
Public class eatno {public static void main (String [] args) {// instantno = new instantno (); instantno. doinstantno (); // you want to pull the HandPulledNoodle handpullno = new handpulledno (); handpullno. dohandpullednotify ();}}
These programs are instant noodles, ramen noodles, and cold noodles. The last Noodle Test class looks at the program running result and thinks that such code is always awkward, I want to eat all kinds of noodles. I don't want to cook it myself. How can I learn it! Although the Code can be run, it is highly dependent and cannot be maintained in the future!
Chubby scratched his head and changed the code,
/** */Public interface Noodles {public void DoNooles ();}
Public class ColdNoodel implements Noodles {@ Overridepublic void DoNooles () {System. out. println ("Noodles ");}}
Public class HandPulledNoodle implements Noodles {@ Overridepublic void DoNooles () {System. out. println ("Do Ramen ...");}}
Public class InstantNoodle implements Noodles {@ Overridepublic void DoNooles () {System. out. println ("instant Noodles ...");}}
/** Please Master Xie from the Chinese minor master */public class MasterXie {public Noodles doNoodles (String type) {if (type. equals ("bubble") {return new instantnotify ();} else if (type. equals ("ramen") {return new handpullednodel () ;}else {return new ColdNoodel ();}}}
Public class EatNoodle {public static void main (String [] args) {// Master Xie has come to MasterXie mx = new MasterXie (); // "what do you want to eat? "Noodles noodle = null; //" Thank you! I want to eat instant noodles !!! "Notify = mx. doNoodles (" instant noodles "); //" good "Notify. DoNooles (); //" Hello, I want to have a cold noodles! "Notify = mx. doNoodles (" noodles "); //" OK "Notify. DoNooles ();}}
After writing, I breathed a sigh of relief: "I have invited Master Xie. Now I can eat everything, and I don't have to do it myself. Hahaha"
Here, we abstract the noodles that act as products (interfaces) and implement this interface. Xie Shifu, acting as a factory class, is responsible for producing these products. In this way, the code coupling is reduced, the test class does not need to know what is in the code. You only need a factory class like Xie Shifu to exist. If you need anything, pass a parameter! However, if you continue to add a class, you need to open the factory class and modify it. The factory class does not conform to the principle of opening/closing, but does not need to modify the noodle code, xiaopang has been thinking for a long time and continues to change the code.
/** Let Master Xie split (abstracted) */public interface MasterXie {public Noodles DoNoodle ();}
/** Master Xie/public class ColdNoodleXie implements MasterXie {@ Overridepublic Noodles DoNoodle () {// TODO Auto-generated method stubreturn new coldnodle ();}}Instant Noodles and instant noodles
Public class eatnodid {public static void main (String [] args) {// Xie Shifu "xiaopang, what do you want to eat? "," Instant Noodles "//" I will separate the body and assign it to you as a secret. "MasterXie mx = new InstantNoodleXie (); Noodles Noles = mx. doNoodle (); noble. doNooles ();}}
The producers and products are abstracted here to allow the producers to correspond with the producers one by one, making maintenance easier.
Here, I think that even a bowl of noodles requires such a complicated program, so I will make more money in the future. I just need to pay for it !!!
(I hope you guys will leave a message to Tom. Tom will accept and modify it one by one !!!!)