190

I have seen the history of several С# and Java class library projects on GitHub and CodePlex, and I see a trend of switching to factory classes as opposed to direct object instantiation.

Why should I use factory classes extensively? I have pretty good library, where objects are created the old-fashioned way - by invoking public constructors of classes. In the last commits the authors quickly changed all of the public constructors of thousands of classes to internal, and also created one huge factory class with thousands of CreateXXX static methods that just return new objects by calling the internal constructors of the classes. The external project API is broken, well done.

Why would such a change be useful? What is the point of refactoring in this way? What are the benefits of replacing calls to public class constructors with static factory method calls?

When should I use public constructors, and when should I use factories?

guo
  • 103
  • 3
rufanov
  • 1,953
  • 2
  • 12
  • 9

9 Answers9

155

Like whatsisname said, I believe this is case of cargo cult software design. Factories, especially the abstract kind, are only usable when your module creates multiple instances of a class and you want to give user of this module ability to specify what type to create. This requirement is actually quite rare, because most of the time you just need one instance and you can just pass that instance directly instead of creating an explicit factory.

The thing is, factories (and singletons) are extremely easy to implement and so people use them a lot, even in places where they are not necessary. So when programmer thinks "What design patterns should I use in this code?" the Factory is first that comes to his mind.

Many factories are created because "Maybe, one day, I will need to create those classes differently" in mind. Which is a clear violation of YAGNI.

And factories become obsolete when you introduce IoC framework, because IoC is just a kind of factory. And many IoC frameworks are able to create implementations of specific factories.

Also, there is no design pattern that says to create huge static classes with CreateXXX methods which only call constructors. And it is especially not called a Factory (nor Abstract Factory).

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
Euphoric
  • 36,735
  • 6
  • 78
  • 110
  • 7
    I agree with most of your points except *"IoC is kind of factory"*: IoC containers are **not factories**. They are a convenient method of achieving dependency injection. Yes, some are capable of constructing automatic factories, but they are not factories themselves and should not be treated as such. I'd also contend the YAGNI point. It is useful to be able to substitute a test double in your unit test. Refactoring everything to provide this after the fact is a *pain in the ass*. Plan ahead and don't fall for the "YAGNI" excuse – Alex Aug 14 '14 at 11:29
  • 14
    @AlexG - eh... in practice, pretty much all IoC containers work as factories. – Telastyn Aug 14 '14 at 12:59
  • 8
    @AlexG Primary focus of IoC is to construct concrete objects for passing into other objects based on configuration/convention. This is same thing as using factory. And you don't need factory to be able to create a mock for test. You just instantiate and pass the mock directly. Like I said in first paragraph. Factories are only useful when you want to pass creation of an instance to user of the module, which calls the factory. – Euphoric Aug 14 '14 at 13:41
  • 5
    There's a distinction to be made. The *factory pattern* is used by a consumer to create entities during the runtime of a program. An *IoC container* is used to create the object graph of the program during start-up. You can do this by hand, the container is just a convenience. The consumer of a factory should not be aware of the IoC container. – Alex Aug 14 '14 at 14:16
  • 5
    Re: *"And you don't need factory to be able to create a mock for test. You just instantiate and pass the mock directly"* - again, different circumstances. You use a factory to **request an instance** - the consumer is in control of this interaction. Supplying an instance through the constructor or a method doesn't work, it's a different scenario. – Alex Aug 14 '14 at 14:26
  • @AlexG - again, about half of the IoC container use I've seen has been at runtime, not just program startup. – Telastyn Aug 14 '14 at 14:33
  • @Telastyn that sounds like the [Service Locator anti-pattern](http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/) - unless you mean instantiating an MVC Controller in a web application (which is equivalent to 'program startup' for a web request) – Alex Aug 14 '14 at 14:38
  • @AlexG - The line you're drawing between an IoC container (which must? be configurable) and a factory (which must not?) - which then details how they may be used is... not exactly clear, and I'm not sure it's a useful/good distinction. The consuming class shouldn't particularly care about the implementation details of a function that takes input and gives you an object. – Telastyn Aug 14 '14 at 14:52
  • @AlexG The point of my answer is that cases, where Abstract Factory makes sense are much rarer than you (and many other programmers) might think. Of course there are cases where it is useful. But having factory for everything is clear abuse of the pattern, which needlessly increases complexity for minimal gain. – Euphoric Aug 14 '14 at 15:07
  • @Telastyn Mark Seemann explains the difference [here](http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryorServiceLocator/) and in the link that I sent you. [Further reading](http://blog.ploeh.dk/2014/05/15/service-locator-violates-solid/). Being "configurable" has nothing to do with it (I don't think I mentioned configuration anywhere?). Primarily the difference is that a factory is called by a consumer, whereas an IoC container is called _once_, at the [composition root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/). – Alex Aug 14 '14 at 15:29
  • @Euphoric depends if you think testable code is a minimal gain – Alex Aug 14 '14 at 15:30
  • @AlexG I'm going to sound like a broken record, but factories are not required to make testable code. Also, ServiceLocator has nothing to do with this discussion. – Euphoric Aug 14 '14 at 15:35
  • @Euphoric I guess we agree to disagree then. If you are creating any nontrivial object (read: an object with methods) you will want to use a test double during testing. You can't do that if you are instantiating it yourself as a concrete class. RE: service locator: *"about half of the IoC container use I've seen has been at runtime, not just program startup"* - that is the Service Locator in action – Alex Aug 14 '14 at 15:49
  • @AlexG Again, I'm repeating myself.If you want to mock, you can pass the concrete instance in constructor instead of passing a factory and calling it. Factory is only needed when you want multiple instances. Also, if IoC can create it's own factory implementations/methods, then those can be called at runtime, thus using IoC at runtime. But I do agree what Telastyn said heavily implies Service locator. – Euphoric Aug 14 '14 at 15:58
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16436/discussion-between-alex-g-and-euphoric). – Alex Aug 14 '14 at 16:02
  • I agree with most of this but "And factories become obsolete when you introduce IoC framework, because IoC is just a kind of factory. And many IoC frameworks are able to create implementations of specific factories." seems to contradict itself but the message is still agreeable lol. IoC doesn't obsolete factories, if anything it actually enables them. – Sinaesthetic Oct 21 '22 at 16:34
94

The Factory pattern vogue stems from an almost-dogmatic belief among coders in "C-style" languages (C/C++, C#, Java) that use of the "new" keyword is bad, and should be avoided at all costs (or at least centralized). This, in turn, comes from an ultra-strict interpretation of the Single Responsibility Principle (the "S" of SOLID), and also of the Dependency Inversion Principle (the "D"). Simply stated, the SRP says that ideally a code object should have one "reason to change", and one only; that "reason to change" is the central purpose of that object, its "responsibility" in the codebase, and anything else that requires a change to code should not require opening up that class file. The DIP is even simpler; a code object should never be dependent upon another concrete object, but instead on an abstraction.

Case in point, by using "new" and a public constructor, you are coupling the calling code to a specific construction method of a specific concrete class. Your code now has to know that a class MyFooObject exists, and has a constructor that takes a string and an int. If that constructor ever needs more information, all usages of the constructor have to be updated to pass in that information including the one you're writing now, and therefore they are required to have something valid to pass in, and so they must either have it or be changed to get it (adding more responsibilities to the consuming objects). In addition, if MyFooObject is ever replaced in the codebase by BetterFooObject, all usages of the old class have to change to construct the new object instead of the old one.

So, instead, all consumers of MyFooObject should be directly dependent on "IFooObject", which defines the behavior of implementing classes including MyFooObject. Now, consumers of IFooObjects can't just construct an IFooObject (without having the knowledge that a particular concrete class is an IFooObject, which they don't need), so instead they must be given an instance of an IFooObject-implementing class or method from outside, by another object that has the responsibility of knowing how to create the correct IFooObject for the circumstance, which in our parlance is usually known as a Factory.

Now, here's where theory meets reality; an object can never be closed to all types of change all the time. Case in point, IFooObject is now an additional code object in the codebase, which must change whenever the interface required by consumers or implementations of IFooObjects change. That introduces a new level of complexity involved in changing the way objects interact with each other across this abstraction. In addition, consumers will still have to change, and more deeply, if the interface itself is replaced by a new one.

A good coder knows how to balance YAGNI ("You Ain't Gonna Need It") with SOLID, by analyzing the design and finding places that are very likely to have to change in a particular way, and refactoring them to be more tolerant of that type of change, because in that case "you are gonna need it".

KeithS
  • 21,994
  • 6
  • 52
  • 79
  • 27
    Love this answer, in special after reading all other ones. I can add almost all (good) new coders are too much dogmatic about principles by the simple fact they really want to be good but don't learned yet the value of keeping things simple and don't overkill. – jean Aug 14 '14 at 17:48
  • 1
    Another problem with public constructors is that there's no nice way for a class `Foo` to specify a public constructor should be usable for the creation of `Foo` instances, or the creation of other types *within the same package/assembly*, but should not be usable for the creation of types derived elsewhere. I don't know of any particularly compelling reason a language/framework couldn't define separate constructors for use in `new` expressions, versus invocation from subtype constructors, but I don't know of any languages that do make that distinction. – supercat Feb 11 '15 at 16:12
  • 2
    A protected and/or internal constructor would be such a signal; this constructor would only be available to consuming code whether in a subclass or within the same assembly. C# does not have a keyword combination for "protected and internal" meaning only subtypes within the assembly could use it, but MSIL has a scope identifier for that type of visibility so it's conceivable the C# spec could be extended to provide a way to utilize it. But, this really doesn't have much to do with the use of factories (unless you use the restriction of visibility to enforce a factory's use). – KeithS May 04 '15 at 15:52
  • 2
    Perfect answer. Right on point with the 'theory meets reality' part. Just think of how many thousands of developers and human time spent cargo-cult praising, justifying, implementing, using them to then falling to the same situation you described, is just maddening. Following YAGNI, Ive never identified the need to implement a factory – Breno Salgado Dec 09 '17 at 01:54
  • I'm programming on a codebase where because the OOP implementation doesn't allow overloading the constructor, the use of public constructors is effectively banned. This is already a minor problem in languages that do allow it. Like create temperature. Both Fahrenheit and Celsius are floats. You can however box those, then problem solved. – jgmjgm Feb 11 '18 at 18:25
  • 1
    Nice answer, but I have one disagreement, new being evil is more of C++ thing. for C#/Java new is perfectly alright with most programmers. – Code Name Jack Jun 04 '20 at 11:43
  • Just need to point out that this has nothing to do with SRP -- it's SoC (separation of concern). SRP is more of a people problem with regard to code, not a code problem with regard to function. "one reason to change" actually refers to stakeholders - it's a commentary on conflicting intent (e.g. payroll vs. finance which are similar enough to collide and break each other). – Sinaesthetic Oct 21 '22 at 16:36
  • ^ Just in case someone wants to start an argument over the accuracy of this as opinion - Bob Martin coined the term and he explains exactly this misunderstanding in his book *Clean Architecture*. Just wanted to leave the note here for someone that is still trying to learn the difference. Easier to look at SRP as a system level architecture concern whereas SoC is an component level architectural concern. – Sinaesthetic Oct 21 '22 at 16:48
72

Factory classes are often implemented because they allow the project to follow the SOLID principles more closely. In particular, the interface segregation and dependency inversion principles.

Factories and interfaces allow for a lot more long term flexibility. It allows for a more decoupled - and therefore more testable - design. Here is a non-exhaustive list of why you might go down this path:

  • It allows you to introduce an Inversion of Control (IoC) container easily
  • It makes your code more testable as you can mock interfaces
  • It gives you a lot more flexibility when it comes time to change the application (i.e. you can create new implementations without changing the dependent code)

Consider this situation.

Assembly A (-> means depends upon):

Class A -> Class B
Class A -> Class C
Class B -> Class D

I want to move Class B to Assembly B, which is dependent on Assembly A. With these concrete dependencies I have to move most of my entire class hierarchy across. If I use interfaces, I can avoid much of the pain.

Assembly A:

Class A -> Interface IB
Class A -> Interface IC
Class B -> Interface IB
Class C -> Interface IC
Class B -> Interface ID
Class D -> Interface ID

I can now move class B across to assembly B with no pain whatsoever. It still depends on the interfaces in assembly A.

Using an IoC container to resolve your dependencies allows you even more flexibility. There is no need to update each call to the constructor whenever you change the dependencies of the class.

Following the interface segregation principle and the dependency inversion principle allows us to build highly flexible, decoupled applications. Once you have worked on one of these types of applications you will never again want to go back to using the newkeyword.

Dan Atkinson
  • 243
  • 1
  • 4
  • 8
Stephen
  • 8,800
  • 3
  • 30
  • 43
  • 50
    IMO factories are least important thing for SOLID. You can do SOLID just fine without factories. – Euphoric Aug 14 '14 at 06:29
  • 1
    You forgot the "open/close" principle. With a factory it is easy not to break it – BЈовић Aug 14 '14 at 07:02
  • 32
    One thing that never made sense to me is, if you use factories to make new objects then you have to *make* the factory in the first place. So what exactly does that get you? Is it assumed that someone else will give you the factory instead of you instantiating it on your own, or something else? This should be mentioned in the answer, otherwise it's unclear how factories actually solve any problem. – user541686 Aug 14 '14 at 10:14
  • 10
    @BЈовић - Except that every time you add a new implementation, you now get to crack open the factory and modify it to account for the new implementation - explicitly violating OCP. – Telastyn Aug 14 '14 at 12:58
  • 7
    @Telastyn Yes, but the code using the created objects do not change. That is more important then the changes to the factory. – BЈовић Aug 14 '14 at 13:34
  • @BЈовић - I don't really agree, given how easy it is to not violate OCP at all by judicious use of non-factory DI. – Telastyn Aug 14 '14 at 13:36
  • 9
    Factories are useful in certain areas which the answer addressed. They are not appropriate to use _everywhere_. For example, using factories to create strings or lists would be taking it way too far. Even for UDTs they are not always necessary. The key is to use a factory when the exact implementation of an interface needs to be decoupled. –  Aug 14 '14 at 14:43
  • When learning to design Windows Store apps, I began using MEF's `ExportFactory` which, in that context, is the only way to request a new instance of a class with all it's imports (dependencies) resolved. But I do have plenty of code that has no dependencies or that can utilize shared instances. Factories have various uses, but don't need to be used for everything. – Magus Aug 14 '14 at 18:32
  • 2
    I know this is old question, but isn't this can be solved easier with Service Pattern? it have all the pros you list, with other pros that it was easier to understand and read. Factory Pattern often dish out thing that hard to predict if you just dip in it in the first hour where service pattern is self explanatory – kirie Jan 24 '16 at 16:14
  • 2
    "The key is to use a factory when the exact implementation of an interface needs to be decoupled" @Snowman I disagree. Factories are the exact OPPOSITE of decoupling. Say if a factory is injecting objects within your constructor, and obviously the implementation is hidden to the class initialising the constructor...then how do you test the class without worrying about the factory objects injected within the constructor? Answer: to set the objects in the constructor parameters - this works just fine, and offers the best chance of low coupling. – PostCodeism Jan 30 '16 at 04:33
  • 4
    @PostCodeism Factories are used to move the responsibility for deciding which object to create from the local code you are writing to a central repository, the factory. The reason for using a factory at all is to be able to change _which_ object is being instantiated. Otherwise, just create the object directly. Factories _can_ inject objects in the constructor, yes, but that is not necessarily the _reason_ for using a factory. This answer explains some good reasons for this decoupling and how it can make an application easier to work with. –  Jan 30 '16 at 21:12
  • 1
    Factory, IoC container, DI framework. However you implement it the point is to separate use from construction. Doing this respects many of the SOLID principles. But mainly it lets you easily obey the law of demeter http://programmers.stackexchange.com/questions/284139/the-principle-of-least-knowledge – candied_orange Feb 03 '16 at 06:42
  • @Mehrdad: You assume that you might want a different class sometimes (for example, two subclasses of an abstract class which are each better suited to different situations, or a different class altogether for testing). You assume that you _always_ use that same factory. – gnasher729 Jun 13 '16 at 13:02
  • Provides the ability to extend the creation of objects, with-out modifying the existing code. https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle – Eric Schneider Oct 31 '18 at 16:31
  • @Mehrdad One does not instantiate the factory itself though, The factory method is supposed to be `static` – Makan Mar 12 '19 at 15:22
  • 2
    This is a great explanation of why we should use interfaces but wasn't the question about factories? – Declan McKenna Jun 13 '19 at 13:35
  • 1
    @DeclanMcKenna a factory decouples the construction of an object with its usage. An interface decouples the implementation of an object with its usage. Both go hand in hand to create loosely coupled code. One without the other is like playing a video game without sound. You can do it but you're missing something. – Stephen Jun 13 '19 at 22:56
  • 1
    @DeclanMcKenna Try to think about what would happen in the refactoring situation towards the end of this answer. Without factories, the object creation code in each of the classes would prevent the refactoring because of the dependency on the constructor of the concrete object. With factories, the dependency is not on the concrete object, it's on the interface for that object, which allows the refactoring. – Stephen Jun 13 '19 at 23:00
  • @Mehrdad not necessarily true. There are several factory patterns, factory method is just one of them. And even those can be static or instanced, depending on your domain and what it is you're solving. Generic factories can be implemented like small services and requested via DI, for example. You wouldn't instantiate it directly, but it's also not static. – Sinaesthetic Oct 21 '22 at 16:25
36

Constructors are fine when they contain short, simple code.

When initialization becomes more than assigning a few variables to the fields, a factory makes sense. Here are some of the benefits:

  • Long, complicated code makes more sense in a dedicated class (a factory). If the same code is put in a constructor which calls a bunch of static methods, this will pollute the main class.

  • In some languages and some cases, throwing exceptions in constructors is a really bad idea, since it can introduce bugs.

  • When you invoke a constructor, you, the caller, needs to know the exact type of the instance you want to create. This is not always the case (as a Feeder, I just need to construct the Animal in order to feed it; I don't care if it's a Dog or a Cat).

Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
  • 2
    The choices are not only "factory" or "constructor." A `Feeder` might use neither, and instead call it's `Kennel` object's `getHungryAnimal` method. – DougM Aug 14 '14 at 13:13
  • 6
    +1 I find that adhering to a rule of absolutely no logic in a constructor is not a bad idea. A constructor should only be used for setting the initial state of the object by assigning its arguments values to instance variables. If anything more complicated is needed, at the very least make a factory (class) method to build the instance. – KaptajnKold Aug 14 '14 at 15:07
  • This is the only satisfying answer I've seen here, saved me from having to write my own. The other answers are only dealing in abstract concepts. – TheCatWhisperer Mar 17 '17 at 18:15
  • But this argument can be held valid true for `Builder Pattern` as well. Isn't it ? – soufrk Sep 26 '17 at 14:07
  • Constructors rule – Breno Salgado Dec 09 '17 at 01:58
13

If you work with interfaces then you can remain independent of the actual implementation. A factory can be configured (via properties, parameters or some other method) to instantiate one of a number of different implementations.

One simple example: you want to communicate with a device but you don't know if it will be via Ethernet, COM or USB. You define one interface and 3 implementations. At runtime you could then select which method you want and the factory will give you the appropriate implementation.

Use it often...

paul
  • 1,201
  • 9
  • 10
  • 7
    I would add that it is great to use when there are multiple implementations of an interface _and_ the calling code does not or should not know which one to pick. But when a factory method is simply a static wrapper around a single constructor as in the question, _that_ is the anti-pattern. There _has to be_ multiple implementations from which to pick or the factory is getting in the way and adding unnecessary complexity. –  Aug 14 '14 at 14:46
  • Now you have the added flexibility and in theory your application can use Ethernet, COM, USB and serial, is ready for fireloop or whatever, in theory. In reality your app will only communicate over Ethernet....ever. – Pieter B Feb 01 '16 at 12:11
9

It's a symptom of a limitation in Java/C#'s module systems.

In principle, there's no reason you shouldn't be able to swap out one implementation of a class for another with the same constructor and method signatures. There are languages that allow this. However, Java and C# insist that every class have a unique identifier (the fully qualified name) and the client code ends up with a hard-coded dependency on it.

You can sort of get around this by fiddling with the file system and compiler options so that com.example.Foo maps to a different file, but this is surprising and unintuitive. Even if you do so, your code is still tied to only one implementation of the class. I.e. If you write a class Foo that depends on a class MySet, you can choose an implementation of MySet at compile time but you still can't instantiate Foos using two different implementations of MySet.

This unfortunate design decision forces people to use interfaces unnecessarily to future-proof their code against the possibility that they'll later need a different implementation of something, or to facilitate unit testing. This isn't always feasible; if you have any methods that look at the private fields of two instances of the class, won't be able to implement them in an interface. That's why, for example, you don't see union in Java's Set interface. Still, outside of numeric types and collections, binary methods aren't common, so you can usually get away with it.

Of course, if you call new Foo(...) you still have a dependency on the class, so you need a factory if you want a class to be able to instantiate an interface directly. However, it's usually a better idea to accept the instance in the constructor and let someone else decide which implementation to use.

It's up to you to decide if it's worth bloating your codebase with interfaces and factories. On the one hand, if the class in question is internal to your codebase, refactoring the code so that it uses a different class or an interface in the future is trivial; you could invoke YAGNI and refactor later if the situation arises. But if the class is part of the public API of a library you've published, you don't have the option of fixing the client code. If you don't use an interface and later need multiple implementations, you'll be stuck between a rock and a hard place.

Doval
  • 15,347
  • 3
  • 43
  • 58
  • 2
    I wish Java and derivatives like .NET had a special syntax for a class creating instances of itself, and otherwise had `new` simply be syntactic sugar for calling a specially-named static method (which would be auto-generated if a type had a "public constructor" but didn't explicitly include the method). IMHO, if code just wants a boring default thing that implements `List`, it should be possible for the interface to give it one without the client having to know of any particular implementation (e.g. `ArrayList`). – supercat Aug 14 '14 at 20:39
2

In my opinion, they're just using the Simple Factory, which is not a proper design pattern and should not be confused with the Abstract Factory or the Factory Method.

And since they have created a "huge fabric class with thousands of CreateXXX static methods", that sounds to an anti-pattern (a God class maybe?).

I think the Simple Factory and static creator methods (which doesn't require an external class), can be useful in some cases. For example, when the construction of an object requires various steps like instancing other objects (e.g. favoring composition).

I wouldn't call even call that a Factory, but just a bunch of methods encapsulated in some random class with the suffix "Factory".

FranMowinckel
  • 215
  • 1
  • 5
  • 1
    Simple Factory has its place. Imagine an entity takes two constructor parameters, `int x` and `IFooService fooService`. You don't want to be passing around `fooService` everywhere, so you create a factory with a method `Create(int x)` and inject the service inside the factory. – Alex Aug 14 '14 at 14:29
  • 4
    @AlexG And then, you have to pass around `IFactory` everywhere instead of `IFooService`. – Euphoric Aug 14 '14 at 15:10
  • 3
    I agree with Euphoric; in my experience, objects that get injected into a graph from the top tend to be of a type that all lower-level objects need, and so passing IFooServices around is no big deal. Replacing one abstraction with another doesn't accomplish anything but further obfuscating the codebase. – KeithS Aug 14 '14 at 16:10
  • this looks totally irrelevant to the question asked, "Why should I use a factory class instead of direct object construction? When should I use public constructors, and when should I use factories?" See [answer] – gnat Mar 07 '16 at 21:07
  • 1
    That's just the title of the question, I think you missed the rest of it. See last point of link provided ;). – FranMowinckel Mar 07 '16 at 21:56
1

As the user of a library, if the library has factory methods, then you should use them. You would assume that the factory method gives the author of the library the flexibility to make certain changes without affecting your code. They might for example return an instance of some subclass in a factory method, which wouldn't work with a simple constructor.

As the creator of a library, you would use factory methods if you want to use that flexibility yourself.

In the case you describe, you seem to have the impression that replacing the constructors with factory methods was just pointless. It was certainly a pain for everyone involved; a library shouldn't remove anything from its API without a very good reason. So if I had added factory methods, I would have left existing constructors available, perhaps deprecated, until a factory method doesn't just call that constructor anymore and code using the plain constructor works less good than it should. Your impression might very well be right.

gnasher729
  • 42,090
  • 4
  • 59
  • 119
  • Also note; If the developer needs to provide a derived class with extra functionality (extra properties, initialization) the developer can do it. (assuming that the factory methods are overridable). The API author may also provide a work around for special customer needs. – Eric Schneider Oct 31 '18 at 16:26
-4

This seems to be obsolete in the age of Scala and functional programming. A solid foundation of functions replaces a gazillion classes.

Also to note that Java's double {{ doesn't work anymore when using a factory i.e

someFunction(new someObject() {{
    setSomeParam(...);
    etc..
}})

Which can let you create an anonymous class and customize it.

In the time space dilema the time factor is now so much shrunk thanks to fast CPUs that functional programming enabling the shrinking of space i.e code size is now practical.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
Marc
  • 1
  • 2
    this looks more like a tangential comment (see [answer]), and it doesn't seem to offer anything substantial over points made and explained in prior 9 answers – gnat Mar 07 '16 at 21:05