factory-method

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

How to create a Simple Factory Pattern with autowired beans in Spring?

浪尽此生 提交于 2020-04-10 05:59:29
问题 I had a controller with 4 very similar methods, calling an API on a remote server to perform different actions on different types of users. What changed between these API calls are just the endpoint and some parameters. Therefore, these 4 methods all called services with very similar code: they got a token from the server, set the parameters, return the API's response. Since more actions will be added later , I decided to use create a ServiceFactory using the Factory Method pattern and use

What is this Design Pattern?

喜欢而已 提交于 2020-01-04 05:08:06
问题 I read the Wikipedia articles on FactoryMethod and AbstractFactory but the following code doesn't seem to fit anywhere. Can someone explain to me what the following pattern is or if it is an anti-pattern? interace PaymentGateway{ void makePayment(); } class PaypalPaymentGateway implements PaymentGateway { public void makePayment() { //some implementation } } class AuthorizeNetPaymentGateway implements PaymentGateway { public void makePayment() { //some implementation } } class

Factory method anti-if implementation

走远了吗. 提交于 2019-12-30 12:37:17
问题 I'm applying the Factory design pattern in my C++ project, and below you can see how I am doing it. I try to improve my code by following the "anti-if" campaign, thus want to remove the if statements that I am having. Any idea how can I do it? typedef std::map<std::string, Chip*> ChipList; Chip* ChipFactory::createChip(const std::string& type) { MCList::iterator existing = Chips.find(type); if (existing != Chips.end()) { return (existing->second); } if (type == "R500") { return Chips[type] =

Factory method anti-if implementation

匆匆过客 提交于 2019-12-30 12:36:15
问题 I'm applying the Factory design pattern in my C++ project, and below you can see how I am doing it. I try to improve my code by following the "anti-if" campaign, thus want to remove the if statements that I am having. Any idea how can I do it? typedef std::map<std::string, Chip*> ChipList; Chip* ChipFactory::createChip(const std::string& type) { MCList::iterator existing = Chips.find(type); if (existing != Chips.end()) { return (existing->second); } if (type == "R500") { return Chips[type] =

Spring - factory method for Path

余生长醉 提交于 2019-12-23 09:23:28
问题 I am trying to generate a bean that would represent java.nio.file.Path using a static method Paths.get(String path) . my current Spring setup looks as follows: <bean id="myPath" class="java.nio.file.Paths" factory-method="get"> <constructor-arg value="c:\\tmp\\" /> </bean> but it comes back with an excpetion No matching factory method found: factory method 'get' . Any ideas why that is the case? 回答1: java.nio.file.Paths.get expects URI. Besides, this is xml not java don't use \\ Try as file:

Why LocalDate, LocalTime and Stream objects use a factory method of() instead of a constructor?

拈花ヽ惹草 提交于 2019-12-22 06:36:00
问题 Why LocalDate , LocalTime , Stream , etc. objects use a factory method of() instead of a constructor? I found an explanation of why a factory methods should be used instead of new here. This answer gives a number of reasons, but the only thing that is relevant to Java Date/Time API is the following: unlike constructors, they are not required to create a new object each time they’re invoked As LocalDate and LocalTime are immutable, it probably makes sense to use a factory and reuse existing

factory method design pattern

和自甴很熟 提交于 2019-12-21 04:40:56
问题 According to the book: The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses. Say I have a Creator class: class Product; //this is what the Factory Method should return class Creator { public: Creator() //ctor { //... } virtual Product make(//args) { //... } } Ok, that's my Creator class, but I don't understand The Factory method lets a

Is it OK for a factory method to return null?

吃可爱长大的小学妹 提交于 2019-12-21 03:12:17
问题 I'm wondering about best practice here. Is it good practice for a factory method to return null if it can't create anything? Here's an example: ICommand command = CommandFactory.CreateCommand(args); if (command != null) command.Execute(); else // do something else if there is no command An alternative would be to return a NullCommand or something, I guess, but what is best practice? 回答1: I think it's potentially reasonable for a factory method to return null in some situations, but not if it

Can I implement the Factory Method pattern in C++ without using new?

吃可爱长大的小学妹 提交于 2019-12-18 03:39:36
问题 I'm working in an embedded environment (Arduino/AVR ATMega328) and want to implement the Factory Method pattern in C++. However, the compiler I'm using (avr-gcc) doesn't support the new keyword. Is there a way of implementing this pattern without using new ? 回答1: Since the AVR compiler is based on the gcc compiler, it is very likely to support the new keyword. What exactly is the error you're getting. I'm guessing it's a link/compiler error along the lines of an undefined function, namely,