Java simple factory Mode

Source: Internet
Author: User

Simple factory mode SimpleFactory, also known as StaticFactory Method, has the advantage that the factory class is the key to the entire mode. it contains the necessary logical judgment to determine the specific class object to be created based on the information given by the outside world. by using the factory class, the outside world can get rid of the embarrassing situation of directly creating specific product objects, and only need to be responsible for "consuming" objects. Without having to worry about how these objects are created and organized, they clearly define their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture. Disadvantages: Because the factory class integrates the creation logic of all instances, it violates the High Cohesion responsibility allocation principle and integrates all creation logic into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class. When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion; these shortcomings have been overcome in the factory method model. Applicable scenarios: 1. The factory class is responsible for creating fewer objects. 2. The customer only knows how to import factory classes and how to create objects (logic) I don't care about it. 3. Because the simple factory model is easy to violate the High Cohesion responsibility allocation principle, the simple factory model is generally used only in simple cases, including: abstract Product role, specific product role, and factory role (some articles refer to them as "God class "); the code below contains an abstract product class ICar, three specific product role classes (Benz, Ferrari, and Jaguar), a factory role class SimpleFactory, and a test class SimpleFatoryTest. ~~ ICar. java ~~ Abstract Product role interface [java]/*** @ author VerpHen * @ date 9:36:19 on January 1, September 10, 2013 */package org. verphen. simpleFactory;/* abstract Product role interface: Car */public interface ICar {/* Car start */public void run (); /* car stop */public void stop ();}~~ Benz. java ~~ The specific product role category [java]/*** @ author VerpHen * @ date 9:38:08 on January 1, September 10, 2013 */package org. verphen. simpleFactory;/* specific product angle: Car-Benz */public class Benz implements ICar {@ Override public void run () {System. out. println ("") ;}@ Override public void stop () {System. out. println (" ");}}~~ Ferrari. java ~~ The specific product role category [java]/*** @ author VerpHen * @ date 9:40:18 on January 1, September 10, 2013 */package org. verphen. simpleFactory;/* specific product angle: automobile-Ferrari */public class Ferrari implements ICar {@ Override public void run () {System. out. println ("Ferrari Boot") ;}@ Override public void stop () {System. out. println ("Ferrari stopped ");}}~~ Jaruar. java ~~ The specific product role category [java]/*** @ author VerpHen * @ date 9:42:21 on January 1, September 10, 2013 */package org. verphen. simpleFactory;/* specific product angle type: automobile-Jaguar-jaruar */public class jaruar implements ICar {@ Override public void run () {System. out. println ("Jaguar startup") ;}@ Override public void stop () {System. out. println ("Jaguar stopped ");}}~~ SimpleFactory. java ~~ Factory corner class [java] view plaincopyprint? /*** @ Author VerpHen * @ date 9:29:02 AM on April 9 */package org. verphen. simpleFactory;/* factory corner class */public class SimpleFactory {/* factory method, return the abstract Product role */public static ICar driveCar (String carType) {/* define signorecase () ignore carType case sensitivity */if ("Benz ". inclusignorecase (carType) {return new Benz ();} else if ("Ferrari ". inclusignorecase (carType) {return new Ferrari ();} else if ("Jaguar ". equalsIgnoreCa Se (carType) {return new jaruar () ;}else {throw new RuntimeException ("this car type is not available ");}}}~~ SimpleFactoryTest. java ~~ Test class [java]/*** @ author VerpHen * @ date 9:48:32 on January 1, September 10, 2013 */package org. verphen. simpleFactory;/* test the simple factory method class */public class SimpleFactoryTest {public static void main (String [] args) {// ICar car = SimpleFactory. driveCar ("Benz"); // ICar car = SimpleFactory. driveCar ("Ferrari"); ICar car = SimpleFactory. driveCar ("Jaguar"); car. run (); car. stop ();}}

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.