[Design mode] -- factory method factorymethod

Source: Internet
Author: User

[Mode overview] ---------- by xingoo

Pattern intent

The factory method is widely used in MVC.

The factory method is intended to separate the two layers of products and the creation, so that users can select the products they want in a factory pool and ignore the creation process.

Simply put, it is like a large factory. for consumers, they only need to know what factory products are produced, rather than how the factory produces products. For factories, the manufacturing methods of all products must be known.

Mode Structure

Creator: interface for creating a factory

The specific product creator of concretecreator

Product Interface

Concreteproduct

Applicable scenarios

  1. When a class does not know the class of the object it must create.

2. When a class wants its subclass to define the object it creates

3. When the role of the class to create an object is delegated to one of multiple help sub-classes and you want to perform partial initialization of some information.

Code structure

The factory method requires a factory interface and provides the basic class mode and a product interface to provide the basic method.

1 interface Creator{2     public Product factory();3 }4 interface Product{5     public void Say();6 }

The next step is the specific factory method. You can create different products.

 1 class ConcreteCreator1 implements Creator{ 2     public Product factory() { 3         return new ConcreteProduct1(); 4     } 5 } 6 class ConcreteCreator2 implements Creator{ 7     public Product factory() { 8         return new ConcreteProduct2(); 9     }10 }

Next, we need different products.

 1 class ConcreteProduct1 implements Product{ 2     public void Say() { 3         System.out.println("ConcreteProduct1"); 4     } 5 } 6 class ConcreteProduct2 implements Product{ 7     public void Say() { 8         System.out.println("ConcreteProduct2"); 9     }10 }

The usage is roughly as follows:

 1 public class test { 2     public static Creator creator1,creator2; 3     public static Product product1,product2; 4     public static void main(String[] args){ 5         creator1 = new ConcreteCreator1(); 6         creator2 = new ConcreteCreator2(); 7          8         product1 = creator1.factory(); 9         product2 = creator2.factory();10         11         product1.Say();12         product2.Say();13     }14 }
Design Patterns in life

  

At present, life is a fast food life. Sometimes I don't want to cook, and I can't wait to go to a restaurant. KFC, McDonald's, and dicos have become a very convenient way.

When we go to KFC, we usually eat hamburgers and cola. We don't need to know how these items are made. We just need to order them and get them for convenience. This is the fast food factory in our life.

With a makechoice method, you can make a unified selection.

Interface KFC {public client working ();} interface client {public void eating ();} class bread_menu implements KFC {public client working () {return New client_bread ();}} class cola_menu implements KFC {public client working () {return New client_cola () ;}} class client_bread implements client {public void eating () {system. out. println ("customer eats bread") ;}} class client_cola implements client {public void eating () {system. out. println ("customers drink Cola") ;}} public class factorymethodtest {public static KFC waiter; public static client Client1, Client2; public static KFC makechoice (KFC maker) {If (maker instanceof bread_menu) {return New bread_menu () ;}else {return New cola_menu () ;}} public static void main (string [] ARGs) {system. out. println ("------------- want to eat bread -----------------"); Waiter = makechoice (New bread_menu (); Client1 = waiter. working (); client1.eating (); system. out. println ("------------- want to drink Cola ---------------"); Waiter = makechoice (New cola_menu (); Client2 = waiter. working (); client2.eating ();}}

Run the following command:

------------- Want to eat bread --------------- customers want to eat bread ------------- want to drink Cola --------------- customers drink cola

 

[Design mode] -- factory method factorymethod

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.