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