static-factory

Is it forbidden to use static methods in factory pattern?

♀尐吖头ヾ 提交于 2021-02-02 09:55:38
问题 I got told that using static methods when implementing the factory-method-pattern is wrong and should be avoided. Because I wasn't really familiar with the pattern I accepted that answer. After reading articles and getting deeper into it, I couldn't find any source which supports this statement. Can someone help me out with this situation. Should I avoid the static-keyword in factory-methods and if so, when are they useful? 回答1: The first thing to be 100% clear about is that there is no

Is this Factory Method creation pattern?

我的梦境 提交于 2020-01-09 12:18:27
问题 I have been using factory method creation pattern for awhile now. I was just recently told that this: public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { IScheduleItem scheduleItem = null; switch (scheduleType) { case ScheduleTypeEnum.CableOnDemandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.BroadbandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum

What is the best way to cache and reuse immutable singleton objects in Java?

ε祈祈猫儿з 提交于 2020-01-03 00:52:29
问题 I have a class representing a set of values that will be used as a key in maps. This class is immutable and I want to make it a singleton for every distinct set of values, using the static factory pattern. The goal is to prevent identical objects from being created many (100+) times and to optimize the equals method. I am looking for the best way to cache and reuse previous instances of this class. The first thing that pops to mind is a simple hashmap, but are there alternatives? 回答1: There

Spring 3 @Component and static factory method

亡梦爱人 提交于 2019-12-29 08:27:13
问题 If I'm writing a static factory method to create objects, how do I use the '@Component' annotation for that factory class and indicate (with some annotation) the static factory method which should be called to create beans of that class? Following is the pseudo-code of what I mean: @Component class MyStaticFactory { @<some-annotation> public static MyObject getObject() { // code to create/return the instance } } 回答1: I am afraid you can't do this currently. However it is pretty simple with

Spring 3 @Component and static factory method

牧云@^-^@ 提交于 2019-11-29 13:11:02
If I'm writing a static factory method to create objects, how do I use the '@Component' annotation for that factory class and indicate (with some annotation) the static factory method which should be called to create beans of that class? Following is the pseudo-code of what I mean: @Component class MyStaticFactory { @<some-annotation> public static MyObject getObject() { // code to create/return the instance } } I am afraid you can't do this currently. However it is pretty simple with Java Configuration: @Configuration public class Conf { @Bean public MyObject myObject() { return

Is this Factory Method creation pattern?

为君一笑 提交于 2019-11-28 16:05:21
I have been using factory method creation pattern for awhile now. I was just recently told that this: public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { IScheduleItem scheduleItem = null; switch (scheduleType) { case ScheduleTypeEnum.CableOnDemandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.BroadbandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.LinearCableScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } case ScheduleTypeEnum