Factory mode:
1) We first need to design the car car needs those processes (face)
PackageLight.zhang.model.model.facotry;/*** Car building process, defined process *@authorWitts **/ Public InterfaceIcarface {/*** Draw Pictures*/ voidDrawImage (); /*** Design Model*/ voidCreatemodel (); /*** Achieve production*/ voidImplcar (); /*** Test the car*/ voidTestcar ();}
2. Automotive Manufacturers (Implementation)
PackageLight.zhang.model.model.facotry;/*** Odison Manufacturers *@authorWitts **/ Public classAodicompanyImplementsIcarface {@Override Public voidDrawImage () {System.out.println ("Audi Company-image Model"); } @Override Public voidCreatemodel () {System.out.println ("Audi Company-Create Model"); } @Override Public voidImplcar () {System.out.println ("Audi Company-production Body"); } @Override Public voidTestcar () {System.out.println ("Audi Company-Test Body"); } }
Package light.zhang.model.model.facotry;
/**
* BMW Corporation
* @author Witts
*/
public class Bmwcompany implements Icarface {
@Override
public void DrawImage () {
System.out.println ("BMW Company-image model");
}
@Override
public void Createmodel () {
System.out.println ("BMW company-Create model");
}
@Override
public void Implcar () {
System.out.println ("BMW Company-production bodywork");
}
@Override
public void Testcar () {
System.out.println ("BMW Company-Test Body");
}
}
3) 4S Shop now to organize Audi BMW car, 4S Shop is a factory, he is responsible for the Audi BMW's car finishing, the user to buy a car only need to tell the salesman I want to buy what car
PackageLight.zhang.model.model.facotry;/*** 4S shop start to sell those cars? * @authorWitts **/ Public classCar4sdianfactory { PublicIcarface Getcar (inttype) { if(Type = = 1) { return NewAodicompany (); } if(Type = = 2) { return NewBmwcompany (); } return NULL; }}
4) The user starts to buy a car (call)
PackageLight.zhang.model.model.facotry;/*** Users start to go to the 4S shop to buy a car *@authorWitts **/ Public classUsershoppingcar { Public Static voidMain (string[] args) {Car4sdianfactory factory=Newcar4sdianfactory (); Icarface Aodicar=factory.getcar (1);//Audi Company's carAodicar.testcar (); Icarface Bmwcar= Factory.getcar (2);//BMW Company's carBmwcar.testcar (); }}
Java Factory mode Factory