Simple Factory for learning design patterns (1)

Source: Internet
Author: User

Simple Factory: for example, if we want to collect apples and bananas, we need to create an Apple class and a Banana class, each of them has a collection method get (), which is then called through the main method. However, we find that both Apple and Banana have a collection get method, so we use the Fruit interface to abstract it, there is a get method in the interface, which is then implemented by the Apple class and Banana class. We can use polymorphism when calling the method. Step 2: we found that every time we had to create a new Apple and Banana, we felt uncomfortable. So we went to create a new FruitFactory class to encapsulate this new process, two static getAppleInstance () and getBananaInstance () methods are written respectively, and then the third step is called through FruitFactory in the main method: we find that both methods of FruitFactory obtain the corresponding instance, so we will The method is encapsulated as a method. getFruitInstance (String className) gets the corresponding instance call. If else is used to determine the className to obtain the instance Step 4: We will improve the getFruitInstance (String className) method and use the reflection mechanism to obtain the instance based on the full name of the input class, the disadvantage is that the caller must know the full name of the class. The above four steps are the basic code implementation of the simple factory: Apple. java copy code package com. designpattern. simplefactory; public class Apple implements Fruit {public void get () {System. out. println ("collect apple") ;}} copy the code Banana. java copy code package com. designpattern. simplefactory; public class Banana implements Fruit {public void get () {System. out. println ("collect bananas") ;}} copy the code Fruit. java package com. designpattern. simplefactory; public interface Fruit {publi C void get ();} FruitFactory. java copy code package com. designpattern. simplefactory; public class FruitFactory {// public static Fruit getAppleInstance () {// return new Apple (); //} // public static Fruit getBananaInstance () {// return new Banana (); //} public static Fruit getFruitInstance (String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {// if (className. equal SIgnoreCase ("Apple") {// return new Apple (); //} // else if (className. inclusignorecase ("Banana") {// return new Banana (); //} // else {// System. out. println ("no instance of this type"); // return null; //} // use the reflection mechanism, you must know the full name of the Class class1 = Class. forName (className); return (Fruit) class1.newInstance () ;}} copy the code MainClass. java copy code package com. designpattern. simplefactory; public class MainClass {public static void main (St Ring [] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {/** use polymorphism, because a new instance object is required here, so the factory Encapsulation Method * // Fruit apple = new Apple (); // apple. get (); // Fruit banana = new Banana (); // banana. get ();/** obtain the instance by calling the corresponding method through the factory * // Fruit apple = FruitFactory. getAppleInstance (); // apple. get (); // Fruit banana = FruitFactory. getBananaInstance (); // banana. get ();/** further encapsulate instance method usage */ Fruit apple = FruitFactory. getFruitInstance ("com. designpattern. simplefactory. apple "); apple. get (); Fruit banana = FruitFactory. getFruitInstance ("com. designpattern. simplefactory. banana "); banana. get () ;}} copy Code 1. What is simple factory mode is a class creation mode, also called static factory method mode. A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class. Ii. Advantages and disadvantages of the simple factory model in which the factory class is the key to the entire model. It contains the necessary judgment logic to determine the specific class object to be created based on the information given by the outside world. You can directly create required instances based on the factory class during use without understanding how these objects are created and organized. It is conducive to the optimization of the entire software architecture. It is not difficult to find that the shortcomings of the simple factory model are also reflected in the factory class. Because the factory class concentrates the creation logic of all instances, it is not easy to do "high cohesion. In addition, when the number of product categories in the system increases, the factory category may be required to be modified accordingly, and the scalability is not very good.

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.