23 design modes (2): Factory method mode

Source: Internet
Author: User

Turn: http://blog.csdn.net/zhengzhb/article/details/7348707

--------------------------------------

Definition:Defines an interface used to create objects so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to its subclass.

Type:Create Class Mode

Class diagram:

Factory method mode code

[Java]View plaincopyprint?
  1. Interface iproduct {
  2. Public void productmethod ();
  3. }
  4. Class product implements iproduct {
  5. Public void productmethod (){
  6. System. Out. println ("product ");
  7. }
  8. }
  9. Interface ifactory {
  10. Public iproduct createproduct ();
  11. }
  12. Class factory implements ifacloud {
  13. Public iproduct createproduct (){
  14. Return New Product ();
  15. }
  16. }
  17. Public class client {
  18. Public static void main (string [] ARGs ){
  19. Ifactory factory = new factory ();
  20. Iproduct prodect = factory. createproduct ();
  21. Prodect. productmethod ();
  22. }
  23. }
Interface iproduct {public void productmethod ();} class product implements iproduct {public void productmethod () {system. out. println ("product") ;}} interface ifacloud {public iproduct createproduct ();} class factory implements ifacloud {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 model:

First, let's talk about the factory model. The factory mode is divided into three types based on the degree of Abstraction: simple factory mode (also called static factory mode ),Factory method modeAnd Abstract Factory mode. Factory mode is a commonly used mode in programming. Its main advantages include:

  • The code structure can be clear and the changes can be effectively encapsulated. In programming, product class instantiation is sometimes complicated and changeable. Through the factory model, product instantiation is encapsulated, so that callers do not need to care about the product instantiation process at all, you only need to rely on the factory to get the desired product.
  • Shield callers from specific product categories. If the factory mode is used, the caller only cares about the product interface. As for the specific implementation, the caller does not need to care about it. Even if the specific implementation is changed, there is no impact on the caller.
  • Reduce coupling. The instantiation of product classes is usually very complex, and it depends on a lot of classes, and these classes do not need to be known to the caller. If the factory method is used, all we need to do is instantiate the product class and hand it over to the caller. For callers, the classes on which the product depends are transparent.

 

Factory method mode:

The class diagram of the factory method mode shows that the factory method mode has four elements:

  • Factory interface. The factory interface is the core of the factory method mode and interacts directly with callers to provide products. In actual programming, sometimes an abstract class is used as the interface for interaction with the caller, which is essentially the same.
  • Factory implementation. In the compilation process, factory implementation determines how to instantiate a product. It is a way to achieve expansion. How many products are required? How many specific factory implementations are required.
  • Product interface. The main purpose of a product interface is to define product specifications. All product implementations must follow the product interface definition specifications. Product interfaces are the most important to callers. The advantages and disadvantages of product interface definitions directly determine the stability of the caller code. Likewise, product interfaces can be replaced by abstract classes, but it is best not to violate the Lee's replacement principle.
  • Product implementation. The specific class that implements the product interface determines the specific behavior of the product on the client.

The simple factory model mentioned above is very similar to the factory method model. The difference is that a simple factory has only three elements. It does not have a factory interface and the product method is generally static. Because there is no factory interface, the scalability of the factory implementation is a little weak. You can calculate the simplified version of the factory method mode. The simple factory mode is included here.

Applicable scenarios:

Whether it's a simple factory model, a factory method model, or an abstract factory model, they have similar features, so their application scenarios are similar.

First, as a class creation modeComplex objectsYou can use the factory method. One thing to note is that complex objects are suitable for using the factory mode, while simple objects, especially objects that can be created through new, do not need to use the factory mode. If the factory mode is used, a factory class needs to be introduced, which will increase the complexity of the system.

Second, the factory model is a typical decoupling mode, which is particularly evident in the factory model. If the caller needs to add dependency when assembling the product, the factory mode can be used. This will greatly reduce the coupling between objects.

Thirdly, because the factory model relies on the abstract architecture, it submits the task of instantiating the product to the implementation class for better scalability. That is to say, when the system requires better scalability, we can consider the factory model. Different products are assembled with different implementation factories.

Typical applications

To illustrate the advantages of the factory model, there may be no more suitable example than assembling a car. The scenario is as follows: a car consists of an engine, a wheel, and a chassis. Now a car needs to be assembled and handed over to the caller. If the factory mode is not used, the Code is as follows:

[Java]View plaincopyprint?
  1. Class engine {
  2. Public void getstyle (){
  3. System. Out. println ("this is the car's engine ");
  4. }
  5. }
  6. Class underpan {
  7. Public void getstyle (){
  8. System. Out. println ("this is the chassis of a car ");
  9. }
  10. }
  11. Class Wheel {
  12. Public void getstyle (){
  13. System. Out. println ("this is a car tire ");
  14. }
  15. }
  16. Public class client {
  17. Public static void main (string [] ARGs ){
  18. Engine engine = new engine ();
  19. Underpan = new underpan ();
  20. Wheel wheel = new wheel ();
  21. ICAR car = new car (underpan, wheel, engine );
  22. Car. Show ();
  23. }
  24. }
Class engine {public void getstyle () {system. out. println ("this is the car's engine") ;}} class underpan {public void getstyle () {system. out. println ("this is the car chassis") ;}} Class Wheel {public void getstyle () {system. out. println ("this is a car tire") ;}} public class client {public static void main (string [] ARGs) {engine = new engine (); underpan = new underpan (); wheel = new wheel (); ICAR car = new car (underpan, wheel, engine); car. show ();}}


We can see that the caller needs to instantiate the engine, chassis, and tires in order to assemble the car. The components of these cars are irrelevant to the caller, seriously violating the Demeter Law, and the coupling is too high. And is not conducive to expansion. In addition, the engine, chassis, and tires in this example are more specific. in actual application, the components of these products may also be abstract, and the caller does not know how to assemble the product. If the factory method is used, the entire architecture is much clearer.

[Java]View plaincopyprint?
  1. Interface ifactory {
  2. Public ICAR createcar ();
  3. }
  4. Class factory implements ifacloud {
  5. Public ICAR createcar (){
  6. Engine engine = new engine ();
  7. Underpan = new underpan ();
  8. Wheel wheel = new wheel ();
  9. ICAR car = new car (underpan, wheel, engine );
  10. Return car;
  11. }
  12. }
  13. Public class client {
  14. Public static void main (string [] ARGs ){
  15. Ifactory factory = new factory ();
  16. ICAR car = factory. createcar ();
  17. Car. Show ();
  18. }
  19. }
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 the factory method is used, the coupling degree of the call end is greatly reduced. In addition, the factory can be expanded. If you want to assemble other cars in the future, you only need to add a factory implementation. Both flexibility and stability have been greatly improved.

23 design modes (2): Factory method mode

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.