Hello everyone, let's introduce the factory model today. First of all, we know the disadvantage of the simple factory model, that is, it is written to death. Let's take the previous example as a simple factory.
If you want to scale it out, you need to modify the source code. In this way, if there are many Phone types generated in the future, you need to keep changing the code, and the code maintenance cost is high (what if you have written a wrong type)
So there is a factory model.
The factory model is simply a factory that corresponds to a product. For example, IPhone factory generates IPhone phones and XiaoMiFactory generates XiaoMi phones.
Let's look at the UML diagram.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + ytfPyMrHUGhvbmW907/aPC9wPgo8cD48L3A + signature "brush: java;"> package edu. fjnu. cs. hwb. factory; public interface Phone {void call ();}Then the specific implementation class
First, iPhone 4
package edu.fjnu.cs.hwb.factory;import edu.fjnu.cs.hwb.factory.*;public class IPhone4 implements Phone {@Overridepublic void call() {// TODO Auto-generated method stubSystem.out.println("i am iphone4");}}
And then IPhone4s
package edu.fjnu.cs.hwb.factory;import edu.fjnu.cs.hwb.factory.*;public class IPhone4s implements Phone {@Overridepublic void call() {// TODO Auto-generated method stubSystem.out.println("i am iphone4s");}}
Followed by XiaoMi2
package edu.fjnu.cs.hwb.factory;import edu.fjnu.cs.hwb.factory.*;public class XiaoMi2 implements Phone {@Overridepublic void call() {// TODO Auto-generated method stubSystem.out.println("i am xiaomi2");}}
Followed by XiaoMi2s
package edu.fjnu.cs.hwb.factory;import edu.fjnu.cs.hwb.factory.*;public class XiaoMi2s implements Phone {@Overridepublic void call() {// TODO Auto-generated method stubSystem.out.println("i am xiaomi2s");}}
Now, let's look at the factory.
First, the factory Interface
Package edu. fjnu. cs. hwb. factory; // define the factory interface public interface PhoneFactory {Phone getPhone (String name );}
Look at the corresponding factory
First, the IPhone factory
Package edu. fjnu. cs. hwb. factory; public class IPhoneFactory implements PhoneFactory {public Phone getPhone (String name) {Phone phone = null; if (name. inclusignorecase ("iphone4") {phone = new IPhone4 ();} else if (name. equalsIgnoreCase ("iphone4s") {phone = new IPhone4s () ;}else {throw new RuntimeException ("no mobile phone of this type");} return phone ;}}
Followed by XiaoMi Factory
Package edu. fjnu. cs. hwb. factory; public class XiaoMiFactory implements PhoneFactory {@ Overridepublic Phone getPhone (String name) {Phone phone = null; if (name. inclusignorecase ("xiaomi2") {phone = new XiaoMi2 ();} else if (name. inclusignorecase ("xiaomi2s") {phone = new XiaoMi2s ();} else {throw new RuntimeException ("no mobile phone of this type");} return phone ;}}
Let's see how the client works.
Package edu. fjnu. cs. hwb. factory;/*** factory mode: * Abstracts a factory, and a mobile phone class * is used to create a factory for each mobile phone, this factory is dedicated to a mobile Phone service */public class Client {public static void main (String [] args) {PhoneFactory phoneFactory = new IPhoneFactory (); phone Phone = phoneFactory. getPhone ("Iphone4s"); phone. call (); phone = phoneFactory. getPhone ("iphone4"); phone. call (); phoneFactory = new XiaoMiFactory (); phone = phoneFactory. getPhone ("xiaomi2"); phone. call (); phone = phoneFactory. getPhone ("xiaomi2s"); phone. call (); try {phoneFactory = new XiaoMiFactory (); phone = phoneFactory. getPhone ("xiaomi2w"); phone. call (); phone = phoneFactory. getPhone ("xiaomi2s"); phone. call ();} catch (Exception ex) {ex. printStackTrace ();}}}
Result
To sum up, the factory model is better than a simple factory, at least it is easy to expand. But if there are multiple products, doesn't it mean that multiple factories are required? (If we need a Computer now, it means we need to write multiple factories.) So there will be an abstract factory design pattern. Abstract factories are designed for product families!
Let's talk about the abstract factory next time.
If there is something wrong, please point it out.