Java factory method design pattern
Factory method pattern is the most widely used pattern in design pattern. In object-oriented programming, the creation of objects is very simple, but the time of object creation is very important. Factory method pattern is to solve this problem. It delays the creation of specific objects to be created to subclasses through object-oriented approach, so as to provide an extension strategy and better solve this tight coupling relationship.
Bmw. java
BmwFactory. java
Main. jav
Output:
Production of BMW 3 Series BMW 3 series production of BMW 5 Series BMW 5 series production of BMW 7 Series BMW 7 Series
Note: the new object is isolated through the factory, and the implementation classes of different actual products can be accepted through the product interface. The change of the class name of the instance will not affect the programming of other cooperative developers.