First, a word background
I need a car, I can pick up from inside the factory directly, without having to pipe the car is how to do it, as well as the production of concrete realization of the car, I just need to tell what brand of car like this factory to produce specific I do not have auto production line pipe.
Second, the use of scenarios
We know some characteristics and create specific object scene.
As: according to the type of environment (dev, test, or master, etc.) directly call set environment configuration
Third, the model analysis
Production lines: interface defines the methods of production of cars
Specific product production lines: class, inside methods to help us create a concrete class object (car), production line interface specific implementation
Factory: class, according to different customer needs to call the assembly line production of cars
Fourth, code analysis
Production lines
/ ** * Create a production line interface methods defined automobile production * / public interface CarProductionLine { // The method of producing an automobile void produceCar (); }
Various brands of automobile production lines (product-specific production lines)
/ ** * BMW production line * / public class BmwProductionLine the implements CarProductionLine { @Override public void produceCar () { // A method to achieve the production line defined by the interface, producing BMW System.out.println ( "BMW is opened and the whole - " ); } }
/ ** * Mercedes production line * / public class BenzProductionLine the implements CarProductionLine { @Override public void produceCar () { // A method to achieve the production line defined by the interface, producing Mercedes System.out.println ( "Benz opened and the whole ~ " ); } }
/ ** * Audi production line * / public class AudiProductionLine the implements CarProductionLine { @Override public void produceCar () { // A method to achieve the production line defined by the interface, producing Audi System.out.println ( "Audi opened and the whole ~ " ); } }
factory
/ ** * automobile manufacturing plant * / public class CarFactory { // use getCar method call different production lines public CarProductionLine getCar (String carBrand) { IF (carBrand == null ) { return null ; } IF (carBrand.equalsIgnoreCase ( "BMW " )) { return new new BmwProductionLine (); } the else IF (carBrand.equalsIgnoreCase (" Benz " )) { return new new BenzProductionLine (); } the else IF (carBrand.equalsIgnoreCase("audi")) { return new AudiProductionLine(); } return null; } }
Customer orders
public class ConsumerOrderDemo { public static void main (String [] args) { CarFactory carFactory = new new CarFactory (); // customer to buy a BMW, the factory get the object BmwProductionLine and call it getCar method ProductionLine bmwProductionLine = carFactory.getCar ( "bmw " ); // call produceCar method BmwProductionLine of bmwProductionLine.produceCar ();
// customer to buy the Mercedes-Benz factory get the object BenzProductionLine and call it getCar method ProductionLine benzProductionLine = carFactory.getCar ( "Benz" ); // call produceCar method BenzProductionLine of benzProductionLine.produceCar ();
// Audi customers want to buy, factory get the object AudiProductionLine and call it getCar method ProductionLine audiProductionLine = carFactory.getCar ( "Audi" ); // call produceCar method AudiProductionLine of audiProductionLine.produceCar ();
} }