Simple factory Mode
1. Purpose
The factory mode is specifically responsible for instantiating a large number of classes with common interfaces, and you do not have to know which class to instantiate each time. It defines an interface used to create objects, and the subclass determines which class to instantiate.
2. Structure of the simple factory Model
3. A simple example of Java code
- // Product Interface
- Public interface product {
- Public void getname ();
- }
- // Product
- Public class producta implements product {
- Public void getname (){
- System. Out. println ("I am producta ");
- }
- }
- // Product B
- Public class productb implements product {
- Public void getname (){
- System. Out. println ("I am productb ");
- }
- }
- // Factory type
- Public class productcreator {
- Public Product createproduct (string type ){
- If ("A". Equals (type )){
- Return new producta ();
- }
- If ("B". Equals (type )){
- Return new productb ();
- } Else
- Return NULL;
- }
- Public static void main (string [] ARGs ){
- Productcreator creator = new productcreator ();
- Creator. createproduct ("A"). getname ();
- Creator. createproduct ("B"). getname ();
- }
- }
4. Summary applicability of the factory Model
? You cannot predict the type of instance to be created during encoding.
? A class uses its subclass to create objects.
? Developers do not want to create an instance of any class or expose the information about how to create an instance to external programs.
Abstract Factory Model
1. abstract factory models can be said to be extensions of simple factory models. The main difference between them is the complexity of object creation.
In the abstract factory model, abstract products may be one or more to form one or more product families. In the case of only one product family, the abstract factory mode actually degrades to the factory method mode.
2. Abstract The structure of the factory Model
3. A simple example
Java code
- // Product plant interface
- Public interface plant {
- }
- // Specific products: planta and plantb
- Public class planta implements plant {
- Public planta (){
- System. Out. println ("create planta! ");
- }
- Public void dosomething (){
- System. Out. println ("planta do something ");
- }
- }
- Public class plantb implements plant {
- Public plantb (){
- System. Out. println ("create plantb! ");
- }
- Public void dosomething (){
- System. Out. println ("plantb do something ");
- }
- }
- // Product fruit Interface
- Public interface fruit {
- }
- // Specific products: Fruita and fruitb
- Public class Fruita implements fruit {
- Public Fruita (){
- System. Out. println ("create Fruita! ");
- }
- Public void dosomething (){
- System. Out. println ("Fruita do something ");
- }
- }
- Public class fruitb implements fruit {
- Public fruitb (){
- System. Out. println ("create fruitb! ");
- }
- Public void dosomething (){
- System. Out. println ("fruitb do something ");
- }
- }
- // Abstract Factory method
- Public interface abstractfactory {
- Public plant createplant ();
- Public fruit createfruit ();
- }
- // Specific factory Method
- Public class factorya implements actfactory {
- Public plant createplant (){
- Return new planta ();
- }
- Public fruit createfruit (){
- Return new Fruita ();
- }
- }
- Public class factoryb implements actfactory {
- Public plant createplant (){
- Return new plantb ();
- }
- Public fruit createfruit (){
- Return new fruitb ();
- }
- }
4. Summary
The abstract factory mode should be considered in the following cases.
First, a system should not depend on the details of product instances being created, formed, and expressed. This is important for all forms of factory models.
Second, there are more than one product family in this system.
Third, products belonging to the same product family are designed to be used together. This constraint must be reflected in the system design.
Finally, different products appear with a series of interfaces, so that the system does not depend on the Interface implementation details.
Among them, the second and third conditions are the key conditions for choosing the abstract factory model rather than other factory models.
Design Model (1)-factory Model