Home>Article>Java> Detailed explanation of common design patterns in Java-factory pattern

Detailed explanation of common design patterns in Java-factory pattern

高洛峰
高洛峰 Original
2016-12-15 13:42:15 1253browse

Principles of design patterns: For excuse programming

The role of the factory pattern:

A. In the design of the application, the creation of objects is concentrated in one place or unified and managed by a certain class (spring)

B. In Objects can be added directly without modifying the application, which also facilitates object maintenance.

Types of factory pattern:

‐ ’ s ’ s ’ s ’ s- with with swiss, Got it An experienced driver will instruct the driver to drive different cars to socialize on different occasions.

Simple factory:

abstract:

package com.product.abstruct;

public interface Car {

public void drive() ;//A car is meant to be driven, so it must have an engine

}

T Implements:

Package com.product.implement;

Import com.product.abstruct.car;

Public Class Audi Implements Car {

Public Void Drive () {// BMW He is also a car or a horse, and he has to There is an engine

}

Implement; m Import com.product.abstruct.car;

public class benz images car {

public void drive () {// Audi is also a car, but also engine

// Todo Auto-Gen Eraate method stub

System.out.println("yes, today I am driving Benz..."); }

} = ====== Lamborghini Province ======

Factory class:

Factory class is a management class, the direct person in charge of the car, this person is the driver of the boss;

package com.product.factory;

import com.product.abstruct.Car; Public class Driver {

Public static Car driveCar(String carName) throws Exception{

Benz (); return new Bwm();

N Return New Audi ();

}}}

One day the boss wants to go to England to watch Liverpool's home game against Vela, and you need to play BMW to the airport, so he calls the driver and let him open BMW 2 pm BMW. pick him up. . .

package com.product.test; public class Test {

public static void main(String[] args) throws Exception {

// Tell The Driver Which Car Wo Drive Todaty Wo Will to Amsterdam

Car Car = Driver.drivecar ("Benz"); drive();//One-click start

As the boss’s business grew bigger and bigger, he became richer and richer, so he bought several sports cars. ---- Ferrari, Porsche, and Jaguar are three cars. Due to the increase in the number of cars, all the cars must be managed by an experienced driver. Annual inspections, insurance, tickets, and car washes are too much for the experienced driver alone, and the boss will give orders. The old driver has to drive different models of cars from Monday to Saturday. At this time, the old driver has to remember which car the boss needs to drive on which day, so he proposes to the boss: Hire a driver for each car. Each car has a dedicated person in charge. When you need to go out, just say hello to me, and I will send the corresponding person to pick up the boss. The boss said without hesitation: That’s it...

:

package com.product.car.abstruct;

public interface car {

public void drive (); com. product.car.abstruct.Car; System.out.println("Four circles of wanting to be an official");

com.product.car.abstruct.Car; O System.out.println ("I want to be an official four circle");

package com.product.car.manager; Car.abstruct.Car; does not need to be .product.car.abstruct.Car; public class AudiManager implements CarManager {

public Car driveCar() {

return new AudiDriver(); did the new Mercedes-Benz driver: ; Or Import com.product.car.implement.benzdriver;

import com.product.manager.Carmanager; C Car Drivecar () {

Return new BenzDriver();

package com.product.test;

import com.product.car.abstruct.Car; .car.implement.benzdriver;

Import com.product.Car.Carmanager; ON {

// Find a car supervisor , told him that I want to drive a BMW to watch the match between Liverpool and Villa today

CarManager carManager = (CarManager) new BenzDriver(); Home phone, fill up the BMW, BMW is coming

Car driveCar = carManager.driveCar();

’s ’ ’ s ’ ’ s ’ s ’ s ‐ to ‐ ‐ ‐ ‐ ‐ ‐‐ ‐ and to Car driveCar = carManager.driveCar(); driveCar.drive(); Compare the differences between these two modes and their respective advantages,

Programming perspective:

a. A simple factory has no abstract class, only one factory class. The parameters that need to be created are passed over, and the factory class creates them uniformly.

b. Define an interface for creating product objects. This interface plays this core role. He only needs to define the methods that subclasses must implement, and hand over the object to the object itself to create

Advantages and Disadvantages:

a. The factory method is more abstract than the simple factory method. The advantage of this is that it can easily add new members (recruit more drivers) without changing the role of the factory (without firing the old driver or reassigning him);

b. The advantage of a simple factory is that it is simple. It only creates corresponding objects based on parameters, but its disadvantages are also quite obvious. All objects are created in this class, and the objects created can only be known in advance ( Suddenly one day the boss bought a manual Jetta, and the boss had to inform the old driver that I bought a Jetta and put it in X); the addition of member objects will cause the core class to add methods, which violates high cohesion. When the system As the number of specific product categories in the product continues to increase, there may be a need for the factory class to create different instances according to different conditions. This kind of judgment of conditions and judgment of specific product types are intertwined, which makes it difficult to avoid the spread of module functions and is very detrimental to the maintenance and expansion of the system;

The next step is the application of the factory pattern. How can we be reasonable in program design? What about the application factory design pattern? a. For a certain product, the caller clearly knows which specific factory service should be used, instantiates the specific factory, and produces a specific product. This is the case with the iterator() method in Java Collection.

 b. Just need a product, but do not want to know or need to know which factory is producing it. That is, the final decision on which specific factory to use lies with the producer. They instantiate a specific product based on the current system situation. The factory is returned to the user, and this decision-making process is transparent to the user.

More Detailed explanations of common Java design patterns - Factory pattern related articles please pay attention to the PHP Chinese website!

Statement:
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