Detailed explanation of common design patterns in Java-factory pattern
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
