"Factory method Model" __java 23 Design Patterns

Source: Internet
Author: User
Definition:

Defines an interface for creating objects, allowing subclasses to decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses. Explanation:

The factory approach model abstracts The Simple factory model, which, depending on the reversal principle, abstracts factories and products, abstracting abstract factories (abstract classes or interfaces) and abstract products (abstract classes or interfaces). The instantiation of the product class is realized by the concrete factory that implements the abstract factory. class Diagram:


role:

Abstract Factory Class (Creator): An abstract class or interface that defines an abstract factory object and provides a way to create a product object.

The Concrete factory class (Concretecreator): Implements the class, implements the abstract factory class, realizes the method which creates the concrete product.

Abstract Product Class (product): An abstract class or interface that defines an abstract product object.

Specific product class (CONCRETEPRODUCT): Implement class, implement abstract product class, define specific Product object. Code:

Factory interface
interface Ifactory {public
	ICar createcar ();
}

Product Interface
interface ICar {public
	void run ();
}

Small car factory class
Smallcarfactory implements Ifactory {public
	ICar Createcar () {return
		new Smallcar ();
	}

//Large auto factory class
Bigcarfactory implements Ifactory {public
	ICar Createcar () {return
		new Bigcar ();
	}

Small car Products Class
Smallcar implements ICar {public
	void run () {
		System.out.println ("Car Startup");
	}

//Large Auto Products class
Bigcar implements ICar {public
	void run () {
		System.out.println ("Big car Startup");
	}
}

The client public
class Client {
	private static ifactory factory1, Factory2;
	private static ICar car1, car2;

	public static void Main (string[] args) {
		factory1 = new Smallcarfactory ();
		Car1 = Factory1.createcar ();
		Car1.run ()//car start

		Factory2 = new Bigcarfactory ();
		CAR2 = Factory2.createcar ();
		Car2.run ()//Big car start
	}
}
Benefits:Good encapsulation, the structure of the code is clear. The factory class encapsulates the product creation process and provides only the means to obtain the product. Good extensibility. When the system needs to add new products, only need to add a product class and a corresponding factory class. Shielding factory class and product class. Clients rely only on abstract factory classes and abstract product classes, not directly on specific factory classes and specific product classes. Reduce coupling. Because the client does not depend on the specific factory class and the specific product class, the client's code is not affected when the process of the specific factory class instantiation of the product changes. Usage Scenarios:The factory method pattern is used as the creation pattern and can be substituted for new to create the object. Objects should have a public behavior method, and the creation of objects is relatively complex. If the object creation process is too simple, you do not have to use the factory method pattern and you can create it directly using new.

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.