简体   繁体   中英

Create a factory class in IntelliJ IDEA

In my Java development, I often have to create factory classes for instantiating things: either because the actual class to be instantiated is only known at runtime, or to keep things tidy and testable/mockable.

Is there a quick way to generate the create() method from the class' constructor in IDEA? I found an easy way to generate a static create , but could not find a way to create a factory class w/o manual cut/paste.

The shortcut Ctrl + Alt + Shift + T might be helpful to you. Toward the bottom there is an option to 'Replace Constructor with Factory Method.'

You can try using lombok but its nothing related to IDE. Its nice and can create factory in predictable style always. Also it keeps source clean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM