Tag Info

Hot answers tagged

32

As so often, people misunderstand what's going on (and that includes many of the laughers). It's not the factory pattern per se that's bad as much as the way many (maybe even most) people use it. And that no doubt stems from the way programming (and patterns) is taught. Schoolkids (often calling themselves "students") get told to "create X using pattern Y" ...


16

Your question is tagged with "Java", no surprise you're asking why is the Factory pattern being mocked: Java itself comes with a nicely packaged abuses of that pattern. For example try loading an XML document from a file and run an XPath query against it. You need something like 10 lines of code just to setup the Factories and Builders: ...


11

Factories have many advantages which allow for elegant application designs in some situations. One is that you can set the properties of objects you later want to create in one place by creating a factory, and then hand that factory around. But often you don't actually need to do that. In that case using a Factory just adds additional complexity without ...


9

The question asks "which process determines which method should execute?" This is a bad question. But, we can immediately eliminate three of the choices: Is-A, Has-A, and Parent Class, since those are object-oriented, but not certainly not processes. Even if Is-A and Has-A were processes, they would be processes regarding class and composition, as you ...


7

Option 2. The bytecode is referenced dynamically at runtime: this is why, for example, LinkageErrors occur. For example, assume you compile two classes: public class Parent { public void doSomething(String x) { ... } } public class Child extends Parent { @Override public void doSomething(String x) { super.doSomething(x); ... } } Now ...


5

Think about it in these terms: If I were to look at it with the purpose of modifying some code, how long will it take me to find where to do so without talking to you first? Is some code repeated? Can you avoid that? What if you need to invoke a method from another class - can you do it easily? Did you write unit tests? Do they pass? If not, try to write ...


5

None of the above. There is no universal term, it depends on the language and the community, but the correct answer is one of message dispatch (virtual) method dispatch (virtual) method resolution (virtual) method lookup vtable lookup or a similar term.


4

Lets start out with two simple classes: package com.michaelt.so.supers; public class Sup { int methodA(int a, int b) { return a + b; } } and then package com.michaelt.so.supers; public class Sub extends Sup { @Override int methodA(int a, int b) { return super.methodA(a, b); } } Compiling methodA and looking at the ...


3

It's called a Factory Method. You just have to make it static, so that you can call it on a class, rather than an instantiated object. public static Player generate_player() { elder_tales.Player p = new ...whatever p.set_stats(); p.set_server(); p.calculate_experience(); return p; } This method goes in your Player class, or whatever ...


3

Factories of one sort or another are found in pretty much any object-oriented language under appropriate circumstances. Sometimes you just plain need a way to select what kind of object to create based on a simple parameter like a string. Some people take it too far and try to architect their code to never need to call a constructor except inside a ...


2

In general, there is this trend that Java programs are horrendously over-engineered [citation needed]. Having many factories is one of the most common symptoms of over-engineering; that's why people make fun of those. In particular, the problem Java has with factories is that in Java a) constructors are not functions and b) functions are not first-class ...


1

You have written an application which is a God Object, maybe even with a Megamoth (MEGA MOnolithic meTHod). This style of programming isn't object-oriented. It is procedural. There is nothing wrong about procedural programming per-se. There are many important programs which work very well and are sufficiently maintainable even though they are completely ...


1

You would declare a class abstract when you don't want the developer (probably yourself) to be allowed to instantiate it, because it wouldn't work or wouldn't make sense. For example, consider a game where there are different types of game entities. They all inherit from the base GameEntity class. abstract class GameEntity{ int lifePoint, speed, ...


1

Let me introduce you to Amazon Mechanical Turk. It was built for this kind of problem. It has an API that allows you to submit "hits" (think of it as like campaigns with people providing answers), and all you have to decide is how much this service is worth to you, and how paranoid you are about the answers. If you're paranoid, then you set it up so the ...


1

If you want to build a hash table in Java, you should take advantage of the hashCode and equals methods which every object has, so there is no need to devise a custom hash function. Note that all Java “characters” are already numbers in the range 0x00 – 0xFFFF (they are UTF-16 code units, not ASCII characters or bytes). Your idea of having the hash code be ...


1

Using ASCII codes of a string is actually not a bad method of hashing a value. Individual letters are not equally common - e is more common than g, and g is much more common than x, so the buckets would become somewhat unbalanced, but taking a MOD 26 sum over several letters will work much better, because the rare and common letters combine so that all ...



Only top voted, non community-wiki answers of a minimum length are eligible