Java factory model, java Factory
1. Simple Mode 2 // There is a common method for all vehicle interfaces to drive 3 public interface Car {4 public void drive (); 5} 6 // the Audi class implements the car interface because Audi is also a Car that can certainly be opened. 7 public class Audi implements car {8 public Audi () {9 System. out. println ("producing an Audi"); 10} 11 @ Override 12 public void drive () {13 System. out. println ("Audi "); 14} 15} 16 // The Bmw class implements the car interface because the Bmw is also a Car, you can certainly drive 17 public class Bmw implements car {18 public Bmw () {19 System. out. println ("produce a BMW"); 20} 21 @ Override 22 public void drive () {23 System. out. println ("Open BMW "); 24} 25} 26 // The factory that produces cars the Factory receives a car name and you only need to tell him what car you want to produce the corresponding Car 27 public class CarFactory {28 public static Car getCar (String caename) {29 if (caename. equals ("audi") {30 return new Audi (); 31} else if (caename. equals ("bmw") {32 return new Bmw (); 33} 34 return null; 35} 36} 37 // Test 38 public class Test {39 public static void main (String [] args) {40 // tell the factory class that you need to produce an audi Car, then send a parameter audi to aon to 41 Car audi = CarFactory. getCar ("audi"); 42 // you can generate an audi car and call the Driving Method to drive 43 audi. drive (); 44 // bmw is similar to 45 Car bmw = CarFactory. getCar ("bmw"); 46 bmw. drive (); 47} 48} 49 output: 50 to generate an Audi 51 open Audi 52 to produce a BMW 53 open BMW 54/** 55 ** 2. Factory method mode 56 * based on the above code, you can find that compared with the traditional new Method Audi audi = new Audi (); A lot of convenience. If you want to add a Mercedes-benz car as needed, you need Bnez benz = new Bnez (); isn't it very troublesome to say this, in factory mode, the client does not need to submit the detailed code of the generated object to the factory for management and implementation of the new object. You only need to write a getObject (); the method gets the desired object. * if you want to add a Mercedes-benz, you only need to write a benz class to implement the car interface, and then add the corresponding logic code to the factory class. okay 57 * This mode is better, but every time we want to add a car, we have to modify and judge the logic code in the factory class, but it still has some limitations and does not comply with the open and closed principles. Therefore, we will introduce a better method below *: factory method mode 58 */59 // first we abstract the carFactory class into an interface 60 public interface CarFactory {61 public Car getCar (); 62} 63 // then we will write two more first specific factory classes (Audi and BMW) for this interface 64 public class AudiFactory implements CarFactory {65 @ Override 66 public Car getCar () {67 return new Audi (); 68} 69} 70 public class BmwFactory implements CarFactory {71 @ Override 72 public Car getCar () {73 return new Bmw (); 74} 75} 76 // Test 77 public class Test {78 public static void main (String [] args) {79 // instantiate the audi factory class 80 CarFactory factory = new AudiFactory (); 81 // call the audi factory class Production Method 82 Car audi = factory. getCar (); 83 audi. drive (); 84 // same as bmw and Audi 85 CarFactory factory2 = new BmwFactory (); 86 Car bmw = factory2.getCar (); 87 bmw. drive (); 88} 89} 90 output result: 91 produce an Audi 92 drive Audi 93 produce a BMW 94 drive BMW 95 // In this case, the Audi factory will only focus on the production of Audi's car BMW, just produce a BMW car, if we need to add a Mercedes-Benz, we only need to write another factory class that produces the Mercedes-Benz to implement the CarFactory interface and a Mercedes-Benz real // body class to implement the car interface. The Code is as follows: 96 public class Bnez implements Car {97 98 public Bnez () {99 System. out. println ("produce a Mercedes-Benz"); 100} 101 @ Override102 public void drive () {103 System. out. println (""); 104} 105} 106 public class BnezFactory implements CarFactory {107 @ Override108 public Car getCar () {109 return null; 110} 111} 112 // call in Test: 113 CarFactory factory = new BenzFactory (); 114 Car benz = factory. getCar (); 115 benz. drive (); 116/** 117*3. Abstract Factory model 118 *, but everyone will think that there are more than one type of Audi and BMW cars, as well as audi sports cars and Audi off-road vehicles, the BMW sports car and the bmw suv are indeed so that the above two aspects cannot be implemented because the car interface cannot be used. He can only produce cars and cannot produce sports cars and off-road vehicles, therefore, in the third method, we need to convert the getCar () method in the car interface into two methods (the getPaoche () method of a sports car (); getYueye (), a method for producing yuyue * wild vehicles, introduces the third factory model: abstract Factory mode 119 */120 // sports car interface 121 public interface Paoche {122 public void drive (); 123} 124 // SUV interface 125 public interface Yueye {126 public void drive (); 127} 128 // in this case, will audi become two Physical Classes? previously, it was just an audi class. Now it has become an audi sports car class, an audi SUV class, And a BMW class. Then there are four specific categories. implemented class 129 // Audi sports car 130 public class AudiPaoche implements Paoche {131 public AudiPaoche () {132 System. out. println ("an Audi sports car produced at the Audi factory"); 133} 134 public void drive () {135 System. out. println ("Audi sports car running on the road"); 136} 137} 138 // Audi SUV 139 public class AudiYueye implements Yueye {140 public AudiYueye () {141 System. out. println ("the Audi factory produces an Audi SUV"); 142} 143 public void drive () {144 System. out. println ("Audi SUV running on the road"); 145} 146} 147 // BMW sports car 148 public class BmwPaoche implements Paoche {149 public BmwPaoche () {150 System. out. println ("the BMW factory produces a BMW sports car"); 151} 152 public void drive () {153 System. out. println ("BMW sports car running on the road"); 154} 155} 156 // bmw suv 157 public class BmwYueye implements Yueye {158 public BmwYueye () {159 System. out. println ("the BMW factory produces a bmw suv"); 160} 161 public void drive () {162 System. out. println ("bmw suv"); 163} 164} 165 // interface for producing sports cars and SUVs 166 public interface CarFactory {167 public Paoche getPaoche (); 168 public Yueye getYueye (); 169} 170 // implement CarFactory171 public class AudiFactory implements CarFactory {172 @ Override173 public Paoche getPaoche () {174 return new AudiPaoche (); 175} 176 @ Override177 public Yueye getYueye () {178 return new AudiYueye (); 179} 180} 181 // The factory class implementing CarFactory182 public class BmwFactory implements CarFactory {183 @ Override184 public Paoche getPaoche () {185 return new BmwPaoche (); 186} 187 @ Override188 public Yueye getYueye () {189 return new BmwYueye (); 190} 191} 192 // Test 193 public class Test {194 public static void main (String [] args) {195 // instantiate the factory class that produces the BMW to produce the BMW sports car and the bmw suv factory. There are two ways to produce the BMW sports car and the bmw suv. Specifically, choose 196 for the production of the sports car or the SUV client. carFactory factory = new BmwFactory (); 197 // here, the production sports car calls the method of the production sports car 198 Paoche paoche = factory. getPaoche (); 199 paoche. drive (); 200 201 CarFactory factory2 = new AudiFactory (); 202 Yueye yueye = factory2.getYueye (); 203 yueye. drive (); 204} 205} 206 output: 207 the BMW factory produced a BMW sports car 208 the BMW sports car ran on the road 209 the Audi factory produced an Audi SUV 210 the Audi SUV ran 211 on the road // wrote so much hope to help everyone right