Home> Java> javaTutorial> body text

JAVA design pattern factory pattern (simple factory pattern + factory method pattern)

高洛峰
Release: 2016-12-12 14:04:57
Original
1273 people have browsed it

In object-oriented programming, the most common method is to use a new operator to generate an object instance. The new operator is used to construct an object instance. But in some cases, the new operator directly generating objects will cause some problems. For example, the creation of many types of objects requires a series of steps: you may need to calculate or obtain the object's initial settings; choose which sub-object instance to generate; or you must generate some helper objects before generating the object you need. In these cases, the creation of a new object is a "process", not just an operation, like a gear transmission in a larger machine.


The problem with the pattern: How can you construct an object instance easily and conveniently without having to worry about the details and complicated process of constructing an object instance?

Solution: Build a factory to create objects

Implementation:

1. Introduction
1) There is no factory era yet: If there is no industrial revolution, if a customer wants a BMW car, the general approach is for the customer to create it A BMW car and use it.
2) Simple factory model: Later, the industrial revolution appeared. Users don't have to create BMW cars. Because the customer has a factory to help him create a BMW. This factory can build whatever car he wants. For example, I want a 320i series car. The factory creates this series of cars. That is, the factory can create products.
3) Factory method model era: In order to satisfy customers, there are more and more BMW car series, such as 320i, 523i, 30li and other series. One factory cannot create all BMW series. As a result, multiple specific factories were separated. Each specific plant creates a kind of series. That is, a specific factory class can only create one specific product. But the BMW factory is still an abstraction. You need to specify a specific factory to produce the car.

4) The era of abstract factory model: As customer requirements become higher and higher, BMW cars must be equipped with air conditioners. So the factory began to produce BMW cars and the required air conditioners.

In the end, the customer only needs to say to the BMW salesperson: I want a 523i air-conditioned car, and the salesperson will directly give him a 523i air-conditioned car. Instead of creating a 523i air-conditioned BMW car yourself.

This is the factory mode.

2. Classification
The factory pattern mainly provides a transition interface for creating objects, so as to shield and isolate the specific process of creating objects to achieve the purpose of improving flexibility.
Factory pattern can be divided into three categories:

1) Simple Factory Pattern (Simple Factory)
2) Factory Method Pattern (Factory Method)
3) Abstract Factory Pattern (Abstract Factory)

These three patterns are from top to bottom Progressively abstract and more general.
GOF dividing the factory pattern into two categories in the book "Design Patterns": Factory Method Pattern (Factory Method) and Abstract Factory Pattern (Abstract Factory).

Think of the Simple Factory pattern (Simple Factory) as a special case of the Factory Method pattern, and the two are classified into the same category.

3. Differences
Factory method pattern:
An abstract product class can derive multiple specific product classes.
An abstract factory class can derive multiple concrete factory classes.
Each specific factory class can only create one instance of a specific product class.
Abstract factory pattern:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class can derive multiple concrete factory classes.
Each specific factory class can create multiple instances of specific product classes.
Difference:
The factory method pattern has only one abstract product class, while the abstract factory pattern has multiple.
The concrete factory class of the factory method pattern can only create one instance of the specific product class, while the abstract factory pattern can create multiple instances.
Both are available.

4. Simple Factory Pattern
Create a factory (a function or a class method) to create new objects.
Introduction to distribution description: from scratch. Customers create their own BMW cars and then use them.

JAVA design pattern factory pattern (simple factory pattern + factory method pattern)

public class BMW320 { public BMW320(){ System.out.println("制造-->BMW320"); } } public class BMW523 { public BMW523(){ System.out.println("制造-->BMW523"); } } public class Customer { public static void main(String[] args) { BMW320 bmw320 = new BMW320(); BMW523 bmw523 = new BMW523(); } }
Copy after login

Customers need to know how to create a car, and the customer and the car are closely coupled together. In order to reduce the coupling, the factory class appeared, and all the operational details of creating a BMW were placed in the factory, and the customer Directly use the factory's create factory method and pass in the desired BMW model without having to know the details of the creation. This is the industrial revolution: the simple factory pattern

means that we create a factory class method to create new objects . As shown in the picture:

JAVA design pattern factory pattern (simple factory pattern + factory method pattern)

Product category:

abstract class BMW { public BMW(){ } } public class BMW320 extends BMW { public BMW320() { System.out.println("制造-->BMW320"); } } public class BMW523 extends BMW{ public BMW523(){ System.out.println("制造-->BMW523"); } }
Copy after login

Factory category:

public class Factory { public BMW createBMW(int type) { switch (type) { case 320: return new BMW320(); case 523: return new BMW523(); default: break; } return null; } }
Copy after login

Customer category:

public class Customer { public static void main(String[] args) { Factory factory = new Factory(); BMW bmw320 = factory.createBMW(320); BMW bmw523 = factory.createBMW(523); } }
Copy after login

简单工厂模式又称静态工厂方法模式。重命名上就可以看出这个模式一定很简单。它存在的目的很简单:定义一个用于创建对象的接口。
先来看看它的组成:
1) 工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑,用来创建产品
2) 抽象产品角色:它一般是具体产品继承的父类或者实现的接口。
3) 具体产品角色:工厂类所创建的对象就是此角色的实例。在Java中由一个具体类实现。

下面我们从开闭原则(对扩展开放;对修改封闭)上来分析下简单工厂模式。当客户不再满足现有的车型号的时候,想要一种速度快的新型车,只要这种车符合抽象产品制定的合同,那么只要通知工厂类知道就可以被客户使用了。所以对产品部分来说,它是符合开闭原则的;但是工厂部分好像不太理想,因为每增加一种新型车,都要在工厂类中增加相应的创建业务逻辑(createBMW(int type)方法需要新增case),这显然是违背开闭原则的。可想而知对于新产品的加入,工厂类是很被动的。对于这样的工厂类,我们称它为全能类或者上帝类。
我们举的例子是最简单的情况,而在实际应用中,很可能产品是一个多层次的树状结构。由于简单工厂模式中只有一个工厂类来对应这些产品,所以这可能会把我们的上帝累坏了,也累坏了我们这些程序员。
于是工厂方法模式作为救世主出现了。 工厂类定义成了接口,而每新增的车种类型,就增加该车种类型对应工厂类的实现,这样工厂的设计就可以扩展了,而不必去修改原来的代码。
五、工厂方法模式
工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。
工厂方法模式组成:
1)抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在java中它由抽象类或者接口来实现。
2)具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
3)抽象产品角色:它是具体产品继承的父类或者是实现的接口。在java中一般有抽象类或者接口来实现。
4)具体产品角色:具体工厂角色所创建的对象就是此角色的实例。在java中由具体的类来实现。
工厂方法模式使用继承自抽象工厂角色的多个子类来代替简单工厂模式中的“上帝类”。正如上面所说,这样便分担了对象承受的压力;而且这样使得结构变得灵活 起来——当有新的产品产生时,只要按照抽象产品角色、抽象工厂角色提供的合同来生成,那么就可以被客户使用,而不必去修改任何已有 的代码。可以看出工厂角色的结构也是符合开闭原则的!

代码如下:

产品类:

abstract class BMW { public BMW(){ } } public class BMW320 extends BMW { public BMW320() { System.out.println("制造-->BMW320"); } } public class BMW523 extends BMW{ public BMW523(){ System.out.println("制造-->BMW523"); } }
Copy after login

创建工厂类:

interface FactoryBMW { BMW createBMW(); } public class FactoryBMW320 implements FactoryBMW{ @Override public BMW320 createBMW() { return new BMW320(); } } public class FactoryBMW523 implements FactoryBMW { @Override public BMW523 createBMW() { return new BMW523(); } }
Copy after login

客户类:

public class Customer { public static void main(String[] args) { FactoryBMW320 factoryBMW320 = new FactoryBMW320(); BMW320 bmw320 = factoryBMW320.createBMW(); FactoryBMW523 factoryBMW523 = new FactoryBMW523(); BMW523 bmw523 = factoryBMW523.createBMW(); } }
Copy after login

工厂方法模式仿佛已经很完美的对对象的创建进行了包装,使得客户程序中仅仅处理抽象产品角色提供的接口,但使得对象的数量成倍增长。当产品种类非常多时,会出现大量的与之对应的工厂对象,这不是我们所希望的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!