Profile
 
Design pattern is an art, if you really understand the art, you will find that the world will become more beautiful.
 
Defined
 
Defines an interface for creating an object, letting its subclasses decide which class to instantiate
 
Working with scenes
 
You can use Factory mode for any place where you use complex objects
 
Uml
 
 
1. Abstract Factory
 
Abstract Factory: We all know that factories, generally have only one function, that production, for example, Geely Automobile Factory, then make Geely Car, the iphone manufacturer made the iphone and so on
So can be summed up in a simple way, is create ();
2. Specific Automobile factory
 
Specific car manufacturers: the realization of the abstract factory, with the actual production of automotive processes and methods, etc.
 
3. Abstract car
 
Abstract Cars: Cars we generally know, can run, can play music, can navigate, can turn these are the characteristics of the car
 
4. Specific car
 
Abstract cars: Specific cars realize the abstract car, with all its functions, of course, different cars may have the same function
 
The simple introduction of the above, in general, the specific car manufacturers to make the corresponding cars, such as: Audi's factory, manufacturing Audi cars, Volkswagen's factories, manufacturing Volkswagen's car, etc.
 
Code Demo
 
We have clearly understood the relationship between them, so let's use the code to show our thoughts and logic in code:
 
(1) Abstract automobile Factory
 
 
  
  
Package com.zengtao.demo.factory;
/**
 * Abstract Factory * * Public abstract
class Carfactory {public
  abstract <t extends car> T Createcar (class& Lt T> CLA);
}
 
   
  
(2) Specific automobile factory
 
 
  
  
Package com.zengtao.demo.factory;
/**
 * Audi factory
/public class Audifactory extends Carfactory {
  @SuppressWarnings ("unchecked")
  @Override public
  <t extends car> T createcar (class<t> cla) {car car
    = null;
    try {car
      = (car) class.forname (Cla.getname ()). newinstance ();
    } catch (Exception e) {
      e.printstacktrace ();
    }
    Return (T) car;
  }
 
   
  
(3) Definition of the abstract car
 
 
  
  
Package com.zengtao.demo.factory;
Public abstract class Car {public
  abstract void Drive ();
  public abstract void selfnagive ();
  public abstract void Playmusic ();
}
 
   
  
(4) Specific car
 
(Audi Q3)
 
 
  
  
Package com.zengtao.demo.factory;
public class Audiq3car extends Audicar {
  @Override public
  Void Drive () {
    System.out.println ("Audiq3car has been made successful ");
    System.out.println ("Audiq3car Drive");
  }
  @Override public
  void Selfnagive () {
    System.out.println ("Audiq3car selfnagive");
  }
  @Override public
  void Playmusic () {
    System.out.println ("Audiq3car playmusic");
    System.out.println ("");
  }
}
 
   
  
(Audi Q5)
 
 
  
  
Package com.zengtao.demo.factory;
public class Audiq5car extends Audicar {
  @Override public
  Void Drive () {
    System.out.println ("Audiq5car has been made successful ");
    System.out.println ("Audiq5car Drive");
  }
  @Override public
  void Selfnagive () {
    System.out.println ("Audiq5car selfnagive");
  }
  @Override public
  void Playmusic () {
    System.out.println ("Audiq5car playmusic");
    System.out.println ("");
  }
}
 
   
  
(5) Call
 
 
 
  
  
Package Com.zengtao.demo;
Import com.zengtao.demo.factory.AudiFactory;
Import Com.zengtao.demo.factory.AudiQ3Car;
Import Com.zengtao.demo.factory.AudiQ5Car;
public class Main {public
  static void Main (string[] str) {
    //create factory
    audifactory audifactory = new Audifactory ( );
    The use of factory-made Q3 car
    audiq3car Audiq3car = Audifactory.createcar (audiq3car.class);
     The use of factory-made Q5 car
    audiq5car Audiq5car = Audifactory.createcar (audiq5car.class);
    Start driving music
    audiq3car.drive ();
    Audiq3car.playmusic ();
    Audiq5car.drive ();
    Audiq5car.playmusic ();
  }
 
   
  
(6) Results
 
 
Above all, the use of factory method model, the realization of the simple process of manufacturing automobiles
 
1th:
 
In the abstract factory we see that Createcar (Class cla) method, why do we define it, use the way of reflection to create a specific car, so that our different types of cars, only the corresponding conditions, not all can produce, so it is good to write, like many, Audi has Q3,Q5,A4,A6,A7,A8 and other series.
 
2nd:
 
If we have other cars, such as BMW, BMW x1,320,x5 and so on, we can also be very simple to achieve, only to implement the abstract factory, and then the specific car to implement the abstract cars, the expansion is also very convenient
 
Summarize
 
Abstract factory Here is almost the same, the factory method model is a relatively simple and very good design pattern
 
But there are also disadvantages, such as: if there are new types of cars, only to implement abstract factories and abstract cars, there are two new classes
 
But if it's not a car, it's a cell phone, so write it all over again, which will cause a lot of classes to appear, whether to adopt a factory approach or to decide on demand.
 
Note: We do not use the design model for the design, of course, the factory method model is also commonly used, such as: activity of the OnCreate method, we are based on their own definition of the XML layout, loading the corresponding interface and so on
 
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.