Hot answers tagged

101

No, an object does not have to represent an entity. In fact, I would argue that when you stop thinking about objects as physical entities is when you finally get the benefits that OOP promises. This isn't the best example, but the Coffee Maker design is probably where the light started to come on for me. Objects are about messages. They're about ...


60

Congratulations! You rediscovered the well known fact that object orientation can be done without specific programming language support. It is basically the same way objects are introduced in Scheme in this classic text book. Note that Scheme does not have a class keyword or some kind of equivalent, and objects can be created without having even classes. ...


42

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


34

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

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


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


20

Can classes represent entity-less objects? If not, why they are bad/incomplete/non-OOP-centric? Are there ways they need to be changed/improved? In short, you can do anything, but this specific scenario would be against OOP principles :) What you are describing is sometimes called a "utility" class - usually a sign of code smell. You want to avoid ...


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


15

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


15

Can classes represent entity-less objects? Can? Yes. Should? Probably not - or at least, not how you're phrasing things. Objects actually are best when not representing a physical object directly since reality so infrequently maps nicely to code. But they do need to represent a cohesive concept or code-object. They need to represent a single cohesive ...


14

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.


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


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


11

I would agree that the first definition satisfies the three points your teacher made. I do not think we need the class keyword for anything. Under the covers, what else is an object but a data structure with with different types of data and functions to work with the data? Of course, the functions are data as well.. I would go even further and say that ...


10

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


10

A class should model something - otherwise it's pointless. However, what's being modeled may not be a physical "thing"; instead, it may be a representation of something which is not 'real', but which is needed to control the system being modeled. For example, in a traffic light control system you could very well have some sort of ControlSignal class, ...


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


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


8

Of course you can! The Self programming language is a dynamic prototype-based object oriented language in which everything is an object and there is no sense of classes or whatsoever. It's focused in the idea of prototypical objects and the idea of cloning them instead of having classes as templates of how to create objects. You should check http://www....


8

As per this link Seeing as rule #1 is horrible, I would caution against taking the rest of the blog post as law. Though really, no blog post should be taken as law. And the quote itself is rather contradictory: A good object should never change his encapsulated state. Be aware that immutability doesn't mean that all methods always return the ...


8

If this is the case then would it be right to say that it is not stored in memory? Not really. The Scene object is being instantiated, which by definition means memory is allocated. Why would we want to do this? As user232967 pointed out, it's a convenient way to declutter your source code. Why write (and force others to read) 5 lines of code ...


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


7

No. (Title edited to ask the opposite question!) eg: Public class MyRepository { public MyObject GetObject(string id) { //get data from the DB and build an object } } or Public class MyService { public MyObject3 ProcessData(MyObject1 obj1, MyObject2 obj2) { //perform a process which is not a sole responsiblity of ...


7

They both are member variables, meaning that both are associated with a class. Now of course, there are differences between the two: Instance variables: These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other ...


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

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.


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



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