Factory Method for design pattern Learning (create pattern) (2)

Source: Internet
Author: User
Tags pears

Next, let's talk about the simple factory in the previous lecture. If we need to collect new fruit pears, if we use the simple factory method, we will add a Pear class, then implement the Fruit class, modify the getFruitInstance method to obtain the instance in the FruitFactory class, and add the if else. This solves the problem but violates the "open and closed" principle, how can we solve this problem? Step 1: We can add an abstract factory FruitFactory interface, which has a getFruitInstance () method, and then add the AppleFactory class and BananaFactory class to implement the getFruitInstance () method, return the specific instance new Apple (), new Banana () Step 2: we can obtain the specific factory through the factory polymorphism during the call, then obtain the corresponding Fruit instance through the specific factory instance FruitFactory appleFactory = new AppleFactory (); Fruit fruitApple = appleFactory. getFruitInstance (); fruitApple. get (); Step 3: If we need to collect new Fruit pears, we only need to add a Pear class, implement the Fruit class, and add a PearFactory class to implement the FruitFact Ory interface, return new Pear (), the problem does not violate the "open closed" principle code is as follows: Apple. java copy code package com. designpattern. factorymethod; public class Apple implements Fruit {public void get () {System. out. println ("collect apple") ;}} copy the code Banana. java copy code package com. designpattern. factorymethod; public class Banana implements Fruit {public void get () {System. out. println ("collect bananas") ;}} copy the code Pear. java copy code package com. designpattern. factorymethod; public c Lass Pear implements Fruit {@ Override public void get () {System. out. println ("collection pears") ;}} copy the code Fruit. java package com. designpattern. factorymethod; public interface Fruit {public void get ();} AppleFactory. java copy code package com. designpattern. factorymethod; public class AppleFactory implements FruitFactory {@ Override public Fruit getFruitInstance () {return new Apple () ;}} copy the code BananaFactory. java copy code p Ackage com. designpattern. factorymethod; public class BananaFactory implements FruitFactory {@ Override public Fruit getFruitInstance () {return new Banana () ;}} copy the code PearFactory. java copy code package com. designpattern. factorymethod; public class PearFactory implements FruitFactory {@ Override public Fruit getFruitInstance () {return new Pear () ;}} copy the code FruitFactory. java package com. designpattern. factorym Ethod; public interface FruitFactory {public Fruit getFruitInstance ();} MainClass. java copy code package com. designpattern. factorymethod; public class MainClass {public static void main (String [] args) {FruitFactory appleFactory = new AppleFactory (); Fruit fruitApple = appleFactory. getFruitInstance (); fruitApple. get (); FruitFactory bananaFactory = new BananaFactory (); Fruit fruitBanana = bananaFactory. GetFruitInstance (); fruitBanana. get ();/** at this time, if I add a new fruit, I only need to add a fruit class and a factory class, without modifying other classes, you can implement */FruitFactory pearFactory = new PearFactory (); Fruit fruitPear = pearFactory. getFruitInstance (); fruitPear. get () ;}} copy Code 1. What is the factory method mode? The Factory method mode also belongs to the class creation mode and is also called the multi-state factory mode. The factory method mode defines a factory interface for creating product objects and delays the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role. 2. Compare the factory method mode with the simple factory mode. The difference between the factory method mode and the simple factory mode is not obvious in structure. The core of the factory method class is an abstract factory class, while the simple factory model places the core on a specific class. The factory method mode is called the polymorphism factory mode because each factory class has a common interface or abstract parent class. To add a new product object for system expansion, you only need to add a specific object and a specific factory object. The original factory object does not need to be modified or the client needs to be modified, it is in line with the "open-closed" principle. The simple factory mode has to modify the factory method after adding new product objects, and the scalability is poor. After the factory method model degrades, it can be transformed into a simple factory model.

Related Article

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.