This article mainly introduces the factory method pattern_related information compiled by the Power Node Java Academy. Friends in need can refer to the following
Definition: Define an interface for creating objects , let subclasses decide which class to instantiate, and factory methods defer instantiation of a class to its subclasses.
Type : Create class pattern
Class diagram :
##Factory method pattern code
interface IProduct { public void productMethod(); } class Product implements IProduct { public void productMethod() { System.out.println("产品"); } } interface IFactory { public IProduct createProduct(); } class Factory implements IFactory { public IProduct createProduct() { return new Product(); } } public class Client { public static void main(String[] args) { IFactory factory = new Factory(); IProduct prodect = factory.createProduct(); prodect.productMethod(); } }
Factory pattern:
Factory method pattern:
Whether it is a simple factory pattern, a factory method pattern or an abstract factory pattern, they have similar characteristics, so their The applicable scenarios are also similar.
First of all, as a class creation pattern, the factory method pattern can be used wherever complex objects need to be generated. One thing to note is that complex objects are suitable for using the factory pattern, while simple objects, especially those that can be created only through new, do not need to use the factory pattern. If you use the factory pattern, you need to introduce a factory class, which will increase the complexity of the system.
Secondly, the factory mode is a typical decoupling mode, and Dimit's law is particularly obvious in the factory mode. If the caller assembles the product himself and needs to add dependencies, he can consider using the factory pattern. Will greatly reduce the coupling between objects.
Thirdly, because the factory pattern relies on abstract architecture, it hands over the task of instantiating products to the implementation class, and has better scalability. In other words, when the system needs to have better scalability, the factory model can be considered, and different products are assembled using different implementation factories.
要说明工厂模式的优点,可能没有比组装汽车更合适的例子了。场景是这样的:汽车由发动机、轮、底盘组成,现在需要组装一辆车交给调用者。假如不使用工厂模式,代码如下:
class Engine { public void getStyle(){ System.out.println("这是汽车的发动机"); } } class Underpan { public void getStyle(){ System.out.println("这是汽车的底盘"); } } class Wheel { public void getStyle(){ System.out.println("这是汽车的轮胎"); } } public class Client { public static void main(String[] args) { Engine engine = new Engine(); Underpan underpan = new Underpan(); Wheel wheel = new Wheel(); ICar car = new Car(underpan, wheel, engine); car.show(); } }
可以看到,调用者为了组装汽车还需要另外实例化发动机、底盘和轮胎,而这些汽车的组件是与调用者无关的,严重违反了迪米特法则,耦合度太高。并且非常不利于扩展。另外,本例中发动机、底盘和轮胎还是比较具体的,在实际应用中,可能这些产品的组件也都是抽象的,调用者根本不知道怎样组装产品。假如使用工厂方法的话,整个架构就显得清晰了许多。
interface IFactory { public ICar createCar(); } class Factory implements IFactory { public ICar createCar() { Engine engine = new Engine(); Underpan underpan = new Underpan(); Wheel wheel = new Wheel(); ICar car = new Car(underpan, wheel, engine); return car; } } public class Client { public static void main(String[] args) { IFactory factory = new Factory(); ICar car = factory.createCar(); car.show(); } }
使用工厂方法后,调用端的耦合度大大降低了。并且对于工厂来说,是可以扩展的,以后如果想组装其他的汽车,只需要再增加一个工厂类的实现就可以。无论是灵活性还是稳定性都得到了极大的提高。
The above is the detailed content of Detailed analysis of factory method pattern in Java. For more information, please follow other related articles on the PHP Chinese website!