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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>