Java design mode (2) Factory mode

Source: Internet
Author: User

1. Static Factory

The customer decides on the type of product to be produced without needing attention to detail.

Structure:

1) Abstract product roles generally use interfaces to abstract product commonality

2) Specific product role implementation interface

3) static factory role production of specific products

Example:

Interface Car//abstract product role{void run ();} Class Benz implements Car//specific product role  {@Override public void  Run () {//TODO Auto-gen    erated method Stub System.out.println ("Benz is Running" ); }}class Ford implements Car//specific product role  {@Override public void  Run () {//TODO auto-generated Me Thod stub System.out.println ("Ford is Running" );}} Class Carfactory//static Factory mode  {public static  car Produce (String name) {Try  {return  (car) Class.fornam E (name). newinstance (); } catch  (Instantiationexception e) {//TODO auto-generated catch block  e.printstacktrace ();} catch  (Il Legalaccessexception e) {//TODO auto-generated catch block  e.printstacktrace ();} catch  (Classnotfoundexcep tion e) {//TODO auto-generated catch block  e.printstacktrace ();} return null ;}}    
    public static void main (string[] args) {        Car car=carfactory.produce ("Benz");        Car.run ();        Car car2=carfactory.produce ("Ford");        Car2.run ();    }   Advantages:

Advantages:

By using the factory class, outsiders can get rid of the embarrassing situation of directly creating specific product objects, just need to be responsible for the "consumption" object. Without having to worry about how these objects are created and how they are organized. The respective responsibilities and rights are clarified. (Simply put, you go to KFC just to say you want chicken legs or chicken wings on the line, do not need to tube chicken legs and chicken wings is how to do it, the factory provides you with such an interface)

Insufficient:
Because the factory class centralizes the creation logic of all instances, violates the principle of cohesion responsibility allocation, the whole creation logic is concentrated in a factory class, and the classes it can create can only be considered in advance, if you need to add new
class, you will need to change the factory class. as the number of specific product classes in the system increases, there may be a need for factory classes to create different instances based on different conditions. This kind of judgment of the condition and the judgment of the specific product type staggered together, it is very difficult to avoid the spread of the module function, the maintenance and expansion of the system is very unfavorable ;

Usage scenarios:  

The factory class is responsible for creating fewer objects, and the customer knows only the parameters of the incoming factory class, and does not care about how to create the object (logic); Because simple factories can easily violate the high cohesion responsibility allocation principle, they are generally used only in very simple cases. As can be seen from the above example, factory classes often use reflection mechanisms to produce specific objects. (Because different classes are inherited from the same interface), so its extensibility is very limited, product categories must be known beforehand which kind of, when you want to add a non-public interface under the specific class will not be. In addition, if you do not use the reflection mechanism, nor the public interface, using other logic in the factory (such as judging incoming strings) to create objects based on user parameters is also OK, so the extensibility is also very bad, the logic and add only more.

2. Dynamic (abstract) factory

In contrast to the static factory model, the factory is transformed into an abstract plant and a specific plant, each producing a specific product, with the following structure:

1) Abstract Products

2) Specific Products

3) Abstract Factory

4) Specific Factory

Example:

InterfaceCar//abstract product {voidrun ();}classBenzImplementsCar//specific product {@Override Public voidrun () {//TODO auto-generated Method StubSystem.out.println ("Benz is running"); }}classFordImplementsCar//specific product {@Override Public voidrun () {//TODO auto-generated Method StubSystem.out.println ("Ford is running"); }}InterfaceFactory//abstract Factory {Car Produce (String name);}classBenzfactoryImplementsFactory//specific factory {@Override PublicCar Produce (String name) {//TODO auto-generated Method Stub        return NewBenz (); }}classFordfactoryImplementsFactory//specific factory {@Override PublicCar Produce (String name) {//TODO auto-generated Method Stub        return NewFord (); }}

Advantages: The function of the production product is separated from the previous factory and becomes the factory realization class of the independent production concrete product.

Java design mode (2) Factory 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.