Easy to master Java factory mode abstract factory mode

  • 2020-05-10 18:06:50
  • OfStack

In object-oriented programming, the most common operation is new object, but in the process of creating a new object, there will be some problems, such as we need to pay attention to the implementation details of creating a new object, initialization 1 some necessary parameters and so on. This will make us pay more attention to the creation of objects, rather than the implementation of the logic of the program, seriously delaying the efficiency of our program development. The emergence of the factory pattern and the abstract factory pattern perfectly solves this problem, allowing us to focus less on object creation and more on business implementation.

Features:

1. The programmer creates objects directly from the factory method, without paying attention to the details of creating objects.

      2, hiding the implementation details of the object, is also conducive to the security of the program.

3. Reduce program coupling.

Enterprise development and common framework applications:

      Hibernate sessionfactory etc

Factory mode classification:

Simple factory mode, the most commonly used form in program development, the specific code is as follows:


public class Demo {

 /**
 * demo This class is our usual operation class, in this class we do not have to care   Create the implementation details for the car 
 */
 public static void main(String[] args) {
 Car car = CarFactory.createCar("dz");
 car.run();
 Car car2 = CarFactory.createCar("at");
 car2.run();
 }
}
interface Car{
 public void run();
}

class Dz implements Car{
 public void run() {
 System.out.println(" The Volkswagen is running "); 
 }
}

class At implements Car{
 public void run() {
 System.out.println(" The alto car is running ");
 }
}

class CarFactory{
 public static Car createCar(String type){
 if("dz".equals(type)){
  System.out.println(" To create the 1 A Volkswagen ");
  return new Dz();
 }
 if("at".equals(type)){
  System.out.println(" To create the 1 A rolling car ");
  return new At();
 }
 return null;
 }
}

The factory method pattern, compared to the simple factory pattern, is easy to extend without having to modify the previous code


public class Demo {

 /**
 * demo This class is our usual operation class, in this class we do not have to care   Create the implementation details for the car 
 */
 public static void main(String[] args) {
 AtFactory atFactory = new AtFactory();
 DzFactory dzFactory = new DzFactory();
 Car at = atFactory.createCar();
 Car dz = dzFactory.createCar();
 at.run();
 dz.run();
 }
}

interface Car {
 public void run();
}

class Dz implements Car {
 public void run() {
 System.out.println(" The Volkswagen is running ");
 }
}

class At implements Car {
 public void run() {
 System.out.println(" The alto car is running ");
 }
}

interface CarFactory {
 Car createCar();
}

class DzFactory implements CarFactory {
 public Car createCar() {
 return new Dz();
 }
}

class AtFactory implements CarFactory {
 public Car createCar() {
 return new At();
 }
}

Abstract factory method pattern:


public class Demo {

 public static void main(String[] args) {
 Car carFactory = new GDCarFactory();
 FDZ fdz = carFactory.createFdz();
 fdz.zhuansu();
 }
}

interface FDZ {
 void zhuansu();
}

class GDFDZ implements FDZ {
 public void zhuansu() {
 System.out.println(" The high end engine revs fast ");
 }
}

class DDFDZ implements FDZ {

 public void zhuansu() {
 System.out.println(" Low end engine is slow ");
 }

}

interface ZY {
 void shushidu();
}

class GDZY implements ZY {

 public void shushidu() {
 System.out.println(" The high-end seats are comfortable ");
 }

}

class DDZY implements ZY {

 public void shushidu() {
 System.out.println(" Low end seats are uncomfortable ");
 }

}

interface LT {
 void mosundu();
}

class GDLT implements LT {

 public void mosundu() {
 System.out.println(" High-end tires don't wear ");
 }

}

class DDLT implements LT {

 public void mosundu() {
 System.out.println(" Low-end tires wear fast ");
 }

}

interface Car {
 FDZ createFdz();

 ZY createZy();

 LT createLt();
}

class GDCarFactory implements Car{

 @Override
 public FDZ createFdz() {
 return new GDFDZ();
 }

 @Override
 public ZY createZy() {
 return new GDZY();
 }

 @Override
 public LT createLt() {
 return new GDLT();
 }
 
}

class DDCarFactory implements Car{

 @Override
 public FDZ createFdz() {
 return new DDFDZ();
 }

 @Override
 public ZY createZy() {
 return new DDZY();
 }

 @Override
 public LT createLt() {
 return new DDLT();
 }
 
}


Comparison of three methods:

1. Simple factory mode: simple factory mode is simple in design, with little code, but poor in scalability, and the previous code needs to be modified when the extension is needed

2. Factory method pattern: strong scalability, but increased code complexity

3, the abstract factory pattern: the abstract factory pattern and factory pattern is different, the abstract factory pattern is the product grade, but the factory pattern is to product classification, for example: a car factory pattern is the production of different kinds of cars, such as the audi and Volkswagen, and abstract factory pattern is to hierarchies with 1 car, such as the same are vw, we divided the high-end and low-end models. The abstract factory pattern is more like a refinement of the factory pattern in approach. One for different products, one for the same family of products.


Related articles: