Tag Info

Hot answers tagged

41

An example is when you want to have the same object in two separate lists: Dog myDog = new Dog(); List dogsWithRabies = new ArrayList(); List dogsThatCanPlayPiano = new ArrayList(); dogsWithRabies.add(myDog); dogsThatCanPlayPiano.add(myDog); // Now each List has a reference to the same dog Another use is when you have the same object playing several ...


27

The case for any change of practice is made by identifying the pain points created by the existing design. Specifically, you need to identify what is harder than it should be because of the existing design, what is fragile, what is breaking now, what behaviors can't be implemented in a simple manner as a direct (or even somewhat indirect) result of the ...


21

I'm seeing a lot of instantiable classes in the C++ and Java world that don't have any state. Some possibile reasons to create classes without ivars of their own: State is or could be contained in a superclass. Class implements some interface and needs to be instantiable so that instances can be passed to other objects. Class is intended to be ...


19

I was then asked how many strings this program would generate, assuming garbage collection does not happen. My thoughts for n=3 was (7) Strings 1 ("") and 2 ("a") are the constants in the program, these are not created as part of things but are 'interned' because they are constants the compiler knows about. Read more about this at String interning on ...


17

First, you need to present that any measurable organisation need to adopt industry best practices. Saying that "it just works for us!" cannot be measured, neither in time or in resource as it is simply unpredictable. Software engineering is a science as much as any other fields of science, and these concepts have been studied, researched, tested, and ...


15

No, it is not right that an "object" is always an instance of a class. Just for example, the standard for C (which doesn't have classes at all) defines an object as (ยง3.14/1): "region of data storage in the execution environment, the contents of which can represent values." Now, it is true that using "object" to refer to an instance of a class is quite ...


14

That's actually a surprisingly profound question! Experience from modern C++ (and languages that take from modern C++, such as Rust) suggests that very often, you don't want that! For most data, you want a single or unique ("owning") reference. This idea is also the one main reason for linear type systems. However, even then you typically want some ...


12

It sure looks like a Mock. While often used for testing, it's also sensible in a Duck-typed language to mock other class definitions. You've got two classes which are both implementations of a common interface. This is polymorphism in action. There's not much of "standard formal" name for it because it's just OO programming. In Python, because there's ...


12

I have "inherited" a lot of legacy code using your first variant, and also written lots of code by myself using Point2D and Point3D classes instead (essentially the same what your Vector<int> is about). The first variant leads always to functions with too many parameters and too many repeated constructs where simple vector addition or scalar ...


11

The Translator Pattern is what you're asking for. But I suspect what you're looking for is a framework, more than a pattern. I believe Dozer is popular in the Java world.


9

using is not a "bloat", it's absolutely necessary to free resources wrapped into IDisposable objects. using is compiled into try...finally, with a call to the Dispose method in the finally section. For example, when StreamReader and StreamWriter are used to read and write files, respectively - their Dispose method automatically closes the file. Another ...


9

On each iteration, a new String is created by the + operator and assigned to s. After return, all of them but the last are garbage-collected. String constants like "" and "a" are not created every time, these are interned strings. Since strings are immutable, they can freely be shared; this happens to string constants. To efficiently concatenate strings, ...


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 ...


8

Constructors are not just "methods that are called when object is created", they are conceptually different. Constructor's purpose is to constrain what states objects can be in initially. A freshly created object is zeroed on all fields (null/0/false) and that may be an invalid state in your program. For example, a Customer object in your application needs ...


7

An object, often called an instance, is a specific instantiation of a class. If you instantiate (make an instance of) a class ten times, you get ten objects, but there's still just the one class.


7

We have to decouple two concepts here. As of Java 7, the language does have RAII. It's called the try-with-resources statement. static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } } This guarantees that the reader will be closed no ...


6

Doing this is the best (and verbose) option right now. return {'x': x, 'y': y} The caller would have to do this to use the values var c = get_coords(); alert(c.x + ' ' + c.y); // c.x and c.y holds the coordinates If you care to use only single axis you could do - alert(get_coords().x); With the ECMAScript 6 you would be able to do ...


6

Temporary variables: consider the following pseudocode. Object getMaximum(Collection objects) { Object max = null; for (Object candidate IN objects) { if ((max is null) OR (candidate > max)) { max = candidate; } } return max; } The variables max and candidate may point to the same object, but the variable assignment changes using ...


6

Consider two distinct concepts: objects and references. References 'point' to objects in memory, but they are not the objects themselves. In Java, we always refer to and control objects through references to them. In Java, you never store the object itself in the variable, only it's address in memory (not exactly - implementation dependent - but you get the ...


6

Create alias references for the Location and Address objects to shorten the assignments. $loc = $api_object->Organisation->Location; $main = $db_object->MainAddress; $loc[0]->AddressLine1 = $main->address_line_1; $loc[0]->AddressLine2 = $main->address_line_2;


5

The term object can refer to (at least) three different independent concepts: An instance of a class. This is the case for object-oriented, strongly-typed, statically-typed languages such as Java and C#. In these languages, the "class" is the definition and "object" is a single manifestation ("instance") of that. An untyped area in memory containing ...


5

The analogy is a bad one, because it can convey the essence of classes and objects if you apply the right mental mapping, but that mapping is not obvious to arrive at, and perhaps not even the most convincing one. A class is like a cookie-cutter in that it has the power to create multiple items that are alike in some ways: they all have the same number and ...


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.


5

I have often pondered the same question. I was a C++ programmer for many years and an Objective C programmer for many years before that. That experience taught me the discipline to track every object creation and achieve the nirvana of code - to logically assure the ultimate destruction of every created object and buffer. And what a discipline it was - I ...


5

If you are coming from a background in dynamic programming languages, or new to programming generally, the new keyword can be a bit confusing, especially in languages that don't seem to always require it's use. In Java it is not uncommon to see code like this: User user = ApplicationEnvironment.getCurrentUser(); String username = user.getName(); int ...


4

The reason is that the Java Language Specification says so. It could probably have been written to allow what you describe, but not for free: enforcing the order makes things simpler. Think about inheritance and calls to super(). If it wouldn't be the first statement in a constructor, then the subclass could do something with a superclass which is not yet ...


4

fooObj is an object and it is also an instance of the foo prototype. Though javascript doesn't technically have classes, many would also call fooObj an object of the foo class because it works somewhat similarly to classes in other languages.


4

Caleb and Robert Harvey have already pointed out what utility classes are, and some legitimate reasons for having "data-less" classes. Those descriptions are spot-on, but deal with the positive aspects. I would like to just mention that the abuse of utility classes can definitely be an OO anti-pattern (see c2wiki's description). This quote sums it up ...


4

In my purview, Object is undefinitive, and belongs in a binary format, not in a human-readable text. I don't see the relationship between these two things; there are plenty of good reasons to choose text versus binary: this is not one of them. How it appears is an implementation detail only. any derived type of Object The biggest challenge, then, ...


3

Yes, the prototype property on a given object refers to the parent object, which allows the given object to inherit all its properties and methods. However, the definition of parent probably should be defined here. In a prototype-based language, that means that a single object can be the parent of many other objects. A side effect of this is that a property ...



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