Factory mode of design mode 3

Source: Internet
Author: User

A, Introduction
In the software system, often faced with the creation of "an object", due to the change in demand, the object's implementation is often faced with drastic changes, but it has a relatively stable interface. How to deal with this change? Provides a encapsulation mechanism to isolate changes to "this volatile object" so that the "other objects that depend on the object" in the system do not change as the requirements change? This is the factory method mode to say.
As the name implies, to create an interface (abstract factory Class), inherit the sub-class of this interface (specific factory Class), you can produce a variety of different product class instances (specific products), that is, the interface does not directly produce specific product classes, but let inherit its subclasses to decide which class to instantiate, so that the instantiation of a class to its subclasses

B, meaning
Define a factory interface that creates product objects and defer actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles. The disadvantage is that when the product is modified, the factory class should also make corresponding changes. such as: How to create and how to provide to the client.
When you add a specific product class, you don't need to change it.
Bottom line: Create a set of classes that can "produce a product class." such as: To create a "red four-wheel" car category, "Blue three-wheeled" car category, "Pink two-wheel" car, just create a "color, number of wheels" parameter of the factory, respectively, new out of three cars, three cars and then new out three car instances.
Purpose: Used to generate "multiple" objects of different types.
Difference:
1. Different from the builder model: the builder pattern is the process of building multiple classes that require "a series of complex" packages into an "integrated" class that separates the construction of a complex object from its representation, focusing on "consolidating multiple operations".
2. Different from the façade mode: The façade mode is the integration of multiple interfaces into one interface, focusing on "consolidating multiple interfaces".

C, structure
Factory mode is primarily an interface for creating objects. The factory model is divided into three categories:
Simple Factory : A factory produces multiple products
Factory Method : The base plant has a number of sub-factories, each sub-factory can produce a product
Abstract Method : The base plant has many sub-factories, each sub-factory can produce multiple products

I. Simple Factory mode (easy Factory)
Simplefactory
| real
| example
|
Production:productiona, Productionb 、...

Production Productiona = simplefactory.generateproduction ("Productiona");
Production productionb = simplefactory.generateproduction ("productionb");
1. Factory Floor: Factory class
2. Product Layer:
2.1 Abstract Product Layer: Abstract Product interface
2.2 Specific product layers: specific Product Categories
3. Call Layer: Product a My product = Static factory class. Generateproduction ("Product A")

II. Factory method Mode (Factory methods)
Ifactory:factorya, Factoryb 、...
| real
| example
|
Production:productiona, Productionb 、...

Ifactory Factorya = new Factorya ();
Ifactory Factoryb = new Factoryb ();
Production Productiona = Factorya.generateproduction ();
Production productionb = Factoryb.generateproduction ();

1. Factory Floor:
1.1 Abstract Factory Layer: Abstract Factory interface
1.2 Specific factory Floor: Specific factory class
2. Product Layer:
2.1 Abstract Product Layer: Abstract Product interface
2.2 Specific product layers: specific Product Categories
3. Call Layer: Factory interface
Factory Interface Factory = new Factory sub-category (Product Class);
Product Category = Factory. Production ();

Iii. Abstract Factory mode (Factory)
Ifactory:factorya, Factoryb 、...
| real
| example
|
PRODUCTION:PRODA1, ProdA2, ProdA3, ProdB1, ProdB2 、...
With the "factory method" mode, except that each plant in the factory floor is comprised of multiple product production methods.

D. Examples
The main line of this example: Driver (Factory)->car (product)
I, Simple Factory (easy Factory)
If you want to add products need to change product class
--Product A
Product Class--|
--Product B
Cases:

//Product Layer//Abstract Product Interface Public Interfaceicar{voidlaunch ();}//Specific Product Categories PublicBenz implements icar{ Public voidlaunch () {System. out. println ("Driving Benz"); }} PublicBMW implements icar{ Public voidlaunch () {System. out. println ("Driving BMW"); }}//Factory Floor//factory Class (core part) Public classdriver{ Public StaticICar Distributecar (String s) throws exception{if(S.equalsignorecase ("Benz"))            return NewBenz (); Else if(S.equalsignorecase ("BMW"))            return NewBMW (); ...        Else Throw NewException (); }}//Call Layer//Call class Public classprogram{ Public Static voidMain (string[] args) {ICar car= Driver.distributecar ("Benz");    Car.launch (); }}

Cons: Requires an all-rounder (or God-like), in this case driver.

II, Factory Methods (Factory method)
Adding a product does not require changing the factory class, just adding an implementation when defining a new factory class
A factory-based class is implemented by multiple factory classes, and a factory class can produce only one product
--factory a--product A
Factory base class--|
--Factory b--Product B
Cases:

//Product Layer//Abstract Product InterfaceIbid .//Specific Product CategoriesIbid .//Factory Floor//Abstract Factory Interface (core part) Public Interfacedriver{ICar Distributecar ();}//Specific factory class Public classBenzdriver implements driver{ PublicICar Distributecar () {return NewBenz (); }} Public classBmwdriver implements driver{ PublicICar Distributecar () {return NewBMW (); }}//Call Layer//Call class Public classprogram{ Public Static voidMain (string[] args) {Driver _benzdriver=NewBenzdriver ();//Factory classICar car = _benzdriver.distributecar ();//(through factory Class) Get product classCar.launch (); }}

III, Abstract Factory (Factory)
Also a factory-based class is implemented by multiple factory classes, but a factory class can produce "multiple products in one series", such as a mobile phone factory that can produce screens, batteries, cameras, and memory.
--Product A1
--Factory a--|
| --Product A2
Factory base class--|
| --Product B1
--Factory b--|
--Product B2

Cases:

//Product Layer//Defining the Product 1 abstract interfaceInterfaceICar {...}//Define product 1 specific classesclassbenz:icar {...}classbmw:icar {...}//Defining the Product 2 abstract interfaceInterfaceiwheel {...}//Define product specific 2 categoriesclassbenzwheel:iwheel {...}classbmwwheel:iwheel {...}//Factory Floor//Abstract Factory Interface (core part) Public Interfacedriver{ICar Distributecar (); Iwheel Distributewheel ();}//Specific factory class Public classBenzdriver implements driver{ PublicICar Distributecar () {return NewBenz (); }     PublicIwheel Distributewheel () {return NewBenzwheel (); }} Public classBmwdriver implements driver{ PublicICar Distributecar () {return NewBMW (); }     PublicIwheel Distributewheel () {return NewBmwwheel (); }}//Call Layer//Call classPublic classprogram{ Public StaticMain (string[] args) {//Production BenzwheelDriver Benzdriver =NewBenzdriver (); ICar Benzcar=Benzdriver.distributecar (); Iwheel Benzwheel=Benzdriver.distributewheel (); }}

Factory mode of design mode 3

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.