Fun Factory Model

Source: Internet
Author: User

I recently read "headfirst design mode" and found that the difference between simple factory, factory mode, and abstract factory mode is not very easy to understand. After research, I wrote several examples to help me better understand the differences. The previous article introduced the simple factory model and the simple factory model. below is another example of the factory model.

Package go. derek; // This is an abstract factory class public abstract class CarFactory {abstract void brand (); abstract Car create (String type) ;}// BMW factory, inherits the abstract factory class BmwFactory extends CarFactory {public void brand () {System. out. println ("this is the BMW factory, using the BMW designated accessories and manufacturing process");} // BMW's factory method public Car create (String type) {if (type. equals ("745Li") {return new Bmw_745Li ();} else if (type. equals ("765Li") {return new Bmw_765Li ();} else return null ;}// Audi factory, inherits the abstract factory class AudiFactory extends CarFactory {public void brand () {System. out. println ("this is the Audi factory, using Audi designated accessories and manufacturing process");} // The Factory method of Audi public Car create (String type) {if (type. equals ("A8L") {return new Audi_A8L ();} else if (type. equals ("A6L") {return new Audi_A6L ();} else return null ;}// abstract class Car of a Car abstract {abstract void intro ();} // BMW 745Li class Bmw_745Li extends Car {private String type = "Bmw_745Li"; public void intro () {System. out. println ("this BMW model is" + type + "") ;}// BMW 765Li class Bmw_765Li extends Car {private String type = "Bmw_765Li"; public void intro () {System. out. println ("this BMW model is" + type + "") ;}}// Audi A8L class Audi_A8L extends Car {private String type = "A8L"; public void intro () {System. out. println ("This Audi Model is" + type + "") ;}}// Audi A6L class Audi_A6L extends Car {private String type = "A6L"; public void intro () {System. out. println ("This Audi Model is" + type + "") ;}} [java] view plaincopypackage go. derek; // test class. The simulated client calls public class FactoryTest {public static void main (String [] args) {// creates the bmw factory object CarFactory bmw = new BmwFactory (); // execute the bmw factory process. brand (); // get the Car car1 = bmw of the specified model. create ("745Li"); car1.intro (); Car car2 = bmw. create ("765Li"); car2.intro (); // create the audi factory object CarFactory audi = new AudiFactory (); // execute the audi Engineering Process audi. brand (); // get the Car car3 = audi of the specified model. create ("A6L"); car3.intro (); Car car4 = audi. create ("A8L"); car4.intro ();}}

 

The operation result is as follows: This is the BMW factory, which adopts the designated parts and manufacturing process of BMW. The model of this BMW is Bmw_745Li. The model of this BMW is Bmw_765Li. This is the Audi factory, the model of This Audi is A6L. The model of This Audi is A8L. Compared with the previous simple factory design model, we can see that the Car products are basically the same, there is only a difference in the factory category. A simple factory is a factory that produces all cars, while a factory model is a factory that produces only one brand of cars. Obviously, if there are many brands and many models of vehicles, the dependency is much simpler than the simple model, and it is more convenient to expand.

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.