factory-method

Is Factory method more suitable for frameworks and Abstract facory for Library?

丶灬走出姿态 提交于 2019-12-09 04:23:23
Both Abstract Factory and Factory method patterns are creational design patterns which solves the object creation issues in different scenarios. As per GOF Factory Method pattern Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclass. My Understanding : Motive of the Client is to get a method present in the base Factory class get executed which is dependent upon an object whose concrete class is not known now (In such a case, either during providing the software to client, it will be

Factory method for template classes

强颜欢笑 提交于 2019-12-08 10:47:08
问题 I have an issue I'm facing where I'm trying to build a factory function that, given an ID and a type will return the correct (templated) subclass. What this is trying to solve: The id() values are sent across a network as soon as a connection is established, and specify to the receiver how a sequence of bytes are encoded. The receiver knows in advance the type T that it expects, but does not know how that type T is encoded on the wire until it gets this value. It also specifies how return

Abstract Factory vs Factory method: Composition vs Inplement? [duplicate]

喜欢而已 提交于 2019-12-08 08:24:51
问题 This question already has answers here : Factory, Abstract Factory and Factory Method (3 answers) Closed 5 years ago . I have read a lot of posts about different between Abstract Factory and Factory method, but there are a problem I can't understand. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass

Is Factory method more suitable for frameworks and Abstract facory for Library?

放肆的年华 提交于 2019-12-08 05:47:20
问题 Both Abstract Factory and Factory method patterns are creational design patterns which solves the object creation issues in different scenarios. As per GOF Factory Method pattern Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclass. My Understanding : Motive of the Client is to get a method present in the base Factory class get executed which is dependent upon an object whose concrete

Best way to implement a generic method in a type generic/agnostic way

╄→尐↘猪︶ㄣ 提交于 2019-12-07 19:41:30
问题 Suppose we have a generic method with such signature: T Obfuscate<T>(T value) where T : IConvertible I'm setting type constraint to IConvertible so this method can digest simple value types as well as strings. Let's forget for a moment that enums can also be supplied... I would like to avoid such implementation that would check actual parameter type to execute correct processing. // Please no GOD METHODS public T Obfuscate<T>(T value) where T : IConvertible { if (value is int) { ... } if

Best way to implement a generic method in a type generic/agnostic way

拜拜、爱过 提交于 2019-12-06 09:28:44
Suppose we have a generic method with such signature: T Obfuscate<T>(T value) where T : IConvertible I'm setting type constraint to IConvertible so this method can digest simple value types as well as strings. Let's forget for a moment that enums can also be supplied... I would like to avoid such implementation that would check actual parameter type to execute correct processing. // Please no GOD METHODS public T Obfuscate<T>(T value) where T : IConvertible { if (value is int) { ... } if (value is string) { ... } } This surely smells of a factory method, that would have to call particular

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

白昼怎懂夜的黑 提交于 2019-12-05 09:56:20
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 objects instead of creating a new object every time. Is it the reason why objects like LocalDate and

What is the purpose of using a factory method in objective-c context? [closed]

本秂侑毒 提交于 2019-12-04 22:33:54
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . What is the purpose of use of a factory method in objective-c context? I am a bit confused about the use of factory methods in objective-c? What is the usefulness of doing so? What is an example of using factory method in objective-c? A bit confused. Any explanation would help!

Is it OK for a factory method to return null?

余生长醉 提交于 2019-12-03 10:33:32
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? I think it's potentially reasonable for a factory method to return null in some situations, but not if it's a method called CreateCommand . If it were GetCommand or FetchCommand , that might be okay... but a Create

Is a switch statement applicable in a factory method? c#

守給你的承諾、 提交于 2019-12-03 09:08:25
问题 I want to return an Interface and inside a switch statement I would like to set it. Is this a bad design? private IResultEntity GetEntity(char? someType) { IResultEntity entity = null; switch (someType) { case 'L': //life entity = new LifeEntity(); break; case 'P': //property entity = new PropertyEntity(); break; case 'D': //disability entity = new DisabilityEntity(); break; case 'C': //credit card entity = new CreditCardEntity(); break; } return entity; } 回答1: I don't usually mind switch