Use of the factory method pattern for Java design pattern programming _java

Source: Internet
Author: User
Tags abstract

Definition: Defines an interface for creating objects, letting subclasses decide which class to instantiate, and the factory method delays the instantiation of a class to its subclasses.
Type: Creating class Mode
Class Diagram:

Factory Method mode Code

Interface IProduct {public 
  void Productmethod (); 
} 
 
Class Product implements IProduct {public 
  void Productmethod () { 
    System.out.println ("Product"); 
  } 
} 
 
Interface Ifactory {public 
  iproduct createproduct (); 
} 
 
Class Factory implements Ifactory {public 
  iproduct createproduct () {return 
    new Product (); 
  } 
} 
 
public class Client {public 
  static void Main (string[] args) { 
    Ifactory factory = new Factory (); 
    IProduct prodect = Factory.createproduct (); 
    Prodect.productmethod (); 
  } 
 

Factory mode:
The first thing to say is the factory model. The factory model is divided into three types based on the degree of abstraction: the Simple Factory pattern (also known as the Static Factory pattern), the factory method pattern described in this article, and the abstract factory pattern. Factory mode is a pattern that is often used in programming. Its main advantages are:
You can make the code structure clear and effectively encapsulate the changes. In programming, the instantiation of the product class is sometimes more complex and changeable, through the factory pattern, the product instantiation is encapsulated, making the caller need not care about the product instantiation process, just rely on the factory to get the product they want.
Masks the specific product class for the caller. If the Factory mode is used, the caller only cares about the interface of the product, and as for the specific implementation, the caller does not need to care at all. Even if a specific implementation is changed, it has no effect on the caller.
Reduce the coupling degree. The instantiation of a product class is often complex, and it relies on a lot of classes that don't need to be known to the caller, and if the factory method is used, all we need to do is instantiate the product class and give it to the caller. For the caller, the class on which the product depends is transparent.

Factory Method Mode:
as you can see from the class diagram of the factory method pattern, the factory method pattern has four elements:
Factory interface. The factory interface is the core of the factory method pattern, which interacts directly with the caller to provide the product. In actual programming, an abstract class is sometimes used as an interface to interact with the caller, which is essentially the same.
Factory implementation. In programming, the factory implementation decides how to instantiate the product, is the way to achieve the extension, how many products are needed, and how many specific factory implementations are required.
Product interface. The main purpose of the product interface is to define the product specification, and all product implementations must conform to the specification of the product interface definition. The product interface is the most concerned by the caller, and the quality of the product interface definition directly determines the stability of the caller's code. Similarly, product interfaces can be replaced with abstract classes, but it is best to be careful not to violate the Richter substitution principle.
Product implementation. The realization of the specific class of product interface determines the specific behavior of the product in the client.

Advantages of the factory method model:

    1. Good encapsulation, reduce coupling between modules;
    2. Product interface, shielding product class.
    3. A typical decoupling framework. The high-level module only needs to know the abstract class of the product.
    4. Comply with the Dimitri principle, relying on inverted principle, the Richter replacement principles.
&NBSP
applicable scenario:
        Whether it's a simple factory model, a factory method pattern, or an abstract factory pattern, They have similar characteristics, so their application scenarios are similar.
        First, as a pattern of creating classes, you can use the factory method pattern in any place where you need to build complex objects. One thing to note is that complex objects are suitable for Factory mode, and simple objects, especially those that need to be created with new, do not use Factory mode. If you use the factory model, you need to introduce a factory class that increases the complexity of the system.
       Secondly, the factory model is a typical decoupling mode, and the Dimitri rule is particularly evident in the factory model. Consider using Factory mode if the caller assembles the product themselves and needs to add dependencies. Will greatly reduce the degree of coupling between objects.
       again, because the factory model relies on an abstract architecture, it puts the task of instantiating the product into the implementation class, which is more scalable. In other words, when the system needs to have a better scalability, you can consider the factory model, different products with different implementation of the factory assembly.
     
Typical application
       to illustrate the advantages of the factory model, There may not be a more appropriate example than assembling a car. The scenario is this: the car is made up of engines, wheels, and chassis and now needs to assemble a car to the caller. If you do not use Factory mode, the code is as follows:

Class Engine {public 
  void GetStyle () { 
    System.out.println ("This is the engine of the car"); 
  } 
Class Underpan {public 
  void GetStyle () { 
    System.out.println ("This is the chassis of the car"); 
  } 
Class Wheel {public 
  void GetStyle () { 
    System.out.println ("This is the tyre of the car"); 
  } 
public class Client {public 
  static void Main (string[] args) { 
    Engine Engine = new Engine (); 
    Underpan Underpan = new Underpan (); 
    Wheel Wheel = new Wheel (); 
    ICar car = new Car (Underpan, wheel, engine); 
    Car.show (); 
  } 
 

As you can see, the caller also needs to instantiate the engine, chassis, and tires in order to assemble the car, and the components of these cars are unrelated to the caller, seriously violating the Dimitri law, and the coupling is too high. And very detrimental to expansion. In addition, in this case the engine, chassis and tires are more specific, in practical applications, may be the components of these products are also abstract, the caller simply do not know how to assemble the product. If the factory method is used, the whole structure becomes much clearer.

Interface Ifactory {public 
  ICar createcar (); 
} 
Class Factory implements Ifactory {public 
  ICar Createcar () {Engine 
    Engine = new Engine (); 
    Underpan Underpan = new Underpan (); 
    Wheel Wheel = new Wheel (); 
    ICar car = new Car (Underpan, wheel, engine); 
    return car; 
  } 
public class Client {public 
  static void Main (string[] args) { 
    Ifactory factory = new Factory (); 
    ICar car = Factory.createcar (); 
    Car.show (); 
  } 
 

After using the factory method, the coupling of the caller is greatly reduced. And for the factory, it can be expanded, if you want to assemble other cars, you just need to add a factory class implementation. Both flexibility and stability have been greatly improved.

PS: Factory method model and simple Factory mode

The simple factory model mentioned above is very similar to the factory method pattern, the core of the Factory method class is an abstract factory class, and the simple factory pattern puts the core on a specific class. The difference between the factory method model and the simple factory model structure is not obvious.

    The
    1.         factory method pattern has an alias called the Polymorphism factory pattern because the specific factory class has a common interface, or there is a common abstract parent class.
    2. When a system extension needs to add a new product object, just add a specific object and a specific factory object, the original factory object does not need to make any changes, and do not need to modify the client, well in line with the "open-closed" principle. The simple factory model has to modify the factory method after adding the new product object, and the extensibility is not good. The
    3. factory method pattern is degraded and can evolve into a simple factory pattern.

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.