_1 design patterns factory pattern

Factory Pattern

Factory pattern (Factory Pattern) Java is one of the most commonly used design patterns. This type of design pattern belongs create schema, which provides the best way to create objects. In Factory mode, we will not expose the client to create a logical when you create an object, and through the use of a common interface to point to the newly created object 

Introduction

Intent: Defines a created object's interface, let subclasses decide which instance of a class factory, the factory model so that the process of creating the delay to subclasses.

Mainly to solve: the main problem of interface options.

When to use: When you create different instances in different conditions we definitely plan.

How to solve: let subclasses to implement the factory interface, it is an abstract of the returned product.

The key code: the creation process is executed in its subclasses.

Application examples:  1, you need a car, you can pick up directly from inside the factory, the car is how to do it, and this particular implementation inside the car without having tube. 2, Hibernate change just to change the database dialect and driving can be.

Advantages:  1, a caller wanted to create an object, just know that the name on it. 2, high scalability, if you want to add a product, as long as the expansion of a factory class can be. 3, concrete realization of shielding products, the caller only concerned with the interface of the product.

Disadvantages: Each time a product increases, increasing the need to achieve a concrete factory classes and objects, such that the number of classes in the system increases exponentially, increasing the complexity of the system to some extent, but also increases the system specific classes rely. This is not a good thing.

Usage scenarios:  1, Logger: record may be recorded to your local hard disk, system events, such as a remote server, the user can choose to log somewhere. 2, database access, when users do not know what kind of final system uses a database, and the database may have changes. 3, design a framework for connection to the server, you need three protocol, "POP3", "IMAP" , "HTTP", can these three products as a class, implement a common interface.

Note: As a class to create a pattern in any place need to generate complex objects, you can use the factory method pattern. Local One thing to note is that complex objects suitable for factory mode, and simple objects, in particular, need only to complete the objects created by new, without the use of the factory model. If the factory mode, it is necessary to introduce a factory class, it will increase the complexity of the system.


 More from rookie tutorial design patterns (this presentation format, great, I can not write)

The following is another DEMO is different from my own understanding and rookie tutorial


 The so-called local factory is to build a variety of products, there is a variety of products of various objects. Generally speaking, a factory will produce one type of product, it does not generate all types of products. Production of the same types of products require a production line, if the production of many types of products, that means might require multiple sets of production lines. If you use the factory of the company, while actually sell many types of goods, without taking into account other factors, conditions, production of many types of products factory, there may be a lot of common pipeline section, there are really good, but to sell only one type commodity companies, this factory there are too many useless equipment, too complicated. For the user, the user will not consider how the factory complex, he only needs to buy their own products to use, such as a user at the factory to buy a car, I know the car can be opened on it, and do not know your production processes, you Does the facility produce snacks (of course, there may be some users may be concerned). In the code, a variety of types of plant-based, system complexity is too high, the developer is a kind of torture (method too, simply indistinguishable).

 At MG car plant, for example, MG factory production of various types of vehicles, such as the common five cars and luxury sports cars. . . All cars have driver (drive) method.

/ ** 
* bus interface
* /
public
interface Car { void Drive (); }
/ ** 
* normal car
* /
public class Car {SaloonCar the implements
@Override
public void Drive () {
System.out.println ( "Woo");
}
}
/**
* 跑车
*/
public class SportsCar implements Car {
@Override
public void drive(){
System.out.println("呜呜");
}
}
/**
* 汽车工厂类
*/
public class CarFactory {
public Car getCar(String carType){
if(carType == null){
return null;
}
if(carType.equalsIgnoreCase("跑车")){
return new SportsCar();
} else if(carType.equalsIgnoreCase("汽车")){
return new SaloonCar();
}
return null;
}
}

/ ** 
* automobile factory Demo
* /
public class CarFactotyDemo {
public static void main (String [] args) {
CarFactory carFactory new new CarFactory = ();

Car CAR1 = carFactory.getCar ( "sports");
car1.drive ();

car car2 = carFactory.getCar ( "car");
car2.drive ();

}
}

 

  

 

Guess you like

Origin www.cnblogs.com/mgyh/p/11745495.html