In object-oriented programming program design, our 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 create a new object implementation details, initialization of some necessary parameters, and so on. This will allow us to talk more about the creation of objects, rather than the implementation of the logic of the program, seriously delaying the efficiency of our program development. The appearance of Factory mode and abstract factory pattern solves this problem perfectly, let us no longer care about the creation of object, more focus on the realization of the business.
Characteristics:
1. The programmer creates objects directly from the factory method and no longer focuses on the details of the object being created.
2, Hidden object implementation details, but also conducive to the security of the program.
3, reduce the program coupling degree.
Enterprise-class development and applications in common frameworks:
Hibernate in the sessionfactory, etc.
Factory Pattern Classification:
Simple Factory mode, the most commonly used form of 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 about the implementation details of the creation of cars
/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 ("Volkswagen Running");
}
Class at implements car{public
void Run () {
System.out.println ("ao tuo car Running");
}
Class carfactory{public
static car Createcar (String type) {
if ("dz". Equals (Type)) {
System.out.println ("The creation of a Volkswagen car");
return new Dz ();
}
if ("at". Equals (Type)) {
System.out.println ("created an Austrian extension car");
Return to New at ();
}
return null;
}
}
Factory approach mode, easy to extend compared to simple Factory mode, no need 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 about the implementation details of the creation of cars
/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 ("Volkswagen Running");
}
Class at implements auto {public
void run () {
System.out.println ("ao tuo car 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 ("High end engine speed");
Class Ddfdz implements Fdz {public void Zhuansu () {System.out.println ("Low engine speed slow");
} interface ZY {void Shushidu ();}
Class Gdzy implements ZY {public void Shushidu () {System.out.println ("High-end seating is comfortable");
Class Ddzy implements ZY {public void Shushidu () {System.out.println ("low-end seat discomfort");
} interface LT {void Mosundu ();}
Class GDLT implements LT {public void Mosundu () {System.out.println ("High-end tyre not worn");
Class DDLT implements LT {public void Mosundu () {System.out.println ("Low-end tyre 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 ();
}
}
Three comparison of methods:
1, Simple factory model: Simple factory model design simple, small amount of code, but the scalability is poor, need to modify the previous code to expand
2, factory method mode: scalability, but increased the complexity of the code
3. Abstract Factory mode: Abstract Factory mode and Factory mode are different, the abstract factory model is graded on the product, but the factory model is the product classification, the example of a car: The factory model is to produce different kinds of cars, such as Audi and Volkswagen, and the abstract factory model is to classify the same car. For example, the same is Volkswagen, we split high-end cars and low-end cars. The abstraction of the factory pattern from the method is more like the refinement of the factory pattern. One for the different products, one for the same product family.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.