Design mode--Analysis of factory model

Source: Internet
Author: User

Simple Factory is the creation mode of a class, also known as the Static Factory method (Factory) mode.

A simple factory model is a factory class that determines which product class to create based on the parameters passed in.

There are 4 characters

Factory class role: A direct caller of the specific product class role.

Abstract product role: an interface or abstract class that is responsible for defining specific product roles and interacting with clients.

Specific product roles: objects created by the factory class, and also the client's actual action object.

Client: Call the factory class to produce the instance and invoke the method of the instance to work accordingly.

public interface people{

public void say ();

}

Public class Chinese implements people{

public void Say () {

System.out.println ("Speak Chinese");

}

}

Public class American implements people{

public void Say () {

System.out.println ("Speak 中文版");

}

}

public class peoplefactory{

public static people create (int type) {

if (type==1) {

return new Chinese ();

}else if (type==2) {

return New American ();

}

}

}

public class test{

public static void Main (String []args) {

People p=peoplefactory.create (1);

P.say ();

P=peoplefactory.create (2);

P.say ();

}

}

The factory method model is a derivative of the simple factory model, which solves the problem of many simple factory models.

First, the ' open-closed principle ' is fully realized and extensible. Second, more complex hierarchy, can be applied to product results in complex situations.

The factory method model abstracts the Simple factory model. There is an abstract factory class (which can be abstract classes and interfaces), and this class will not be responsible for the production of specific products, but only to formulate some specifications, specific production work by its subclasses to complete. In this mode, the factory class and the product class can often correspond in turn. That is, an abstract factory corresponding to an abstract product, a specific factory corresponding to a specific product, this specific factory is responsible for the production of the corresponding products.

The factory method pattern (Factory mode) is the most typical template method pattern (templete) application.



Based on the principle of interface-oriented programming, the creation of a part becomes an abstract factory and an entity factory, creating objects that become abstract products and physical products. You may know why it solves the problem above: A new object, just add a set of corresponding factories and products, do not need to modify the original code, or only a small amount of modification.

Specific:

The entity factory realizes abstract factory, entity product realizes abstract product; Abstract Factory produces abstract products, physical factories produce physical products; entity factory a production entity product A, Entity Factory B production entity product B.

public interface icar{

public void Docar ();

}

public class BWM implements icar{

public void Docar () {

System.out.println ("I'm a BMW, don't touch Me");

}

}

Public class Buick implements icar{

public void Docar () {

System.out.println ("I'm a Buick, it's cool");

}

}

public interface icarfactory{

Public Icar Createcar ();

}

public class Bmwfactory implements icarfactory{

Public Icar Createcar () {

return new BWM ();

}

}

public class Buickfactory implements icarfactory{

Public Icar Createcar () {

return new Buick ();

}

}

public class test{

public static void Main (String []args) {

Icarfactory factory=new bmwfactory ();

Icar bwm= Factory.createcar ();

Bwm.docar ();

Factory=new buickfactory ();

Icar buick= Factory.createcar ();

Buick.docar ();

}

}

Abstract Factory mode is the most abstract and general form of the factory pattern in all forms.

Abstract Factory mode refers to a factory pattern that is used when there are multiple abstract roles. Abstract Factory mode provides the client with an interface that enables the client to create product objects in multiple product families without having to specify the product's specific circumstances. According to the LSP principle, any place that accepts a parent type should be able to accept subtypes. Therefore, what the system needs is only some instances of the same type as these abstract product roles, not instances of these abstract products. In other words, they are instances of specific subclasses of these abstract products. The factory class is responsible for creating instances of the specific subclasses of the abstract product.

Brief introduction

When each abstract product has more than one specific subclass, how does the factory role know which subclass to instantiate? For example, each abstract product role has two specific products. Abstract Factory mode provides two specific factory roles that correspond to these two specific product roles, each of which is responsible for instantiating only one product role. Each specific factory class is only responsible for creating an instance of a specific subclass of the abstract product.

Each model is a solution to a problem, and the factory approach model is for a product hierarchy, while the abstract factory model is for multiple product level results.

Design mode--Analysis of factory model

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.