Tag Info

New answers tagged

0

An object is a collection of methods and data that have high cohesion. They belong in a class because they are highly interrelated and state is retained. This is true whether you use big up front design and attempt to model everything, or emergent design where you iterate and evolve the object into whatever fits the needs of the system at the time. If you ...


3

In layman's words What you describe is called an utility class. I has no state, meaning you will not ever really need to have several instances of it All methods are static, meaning you have to pass everything as parameters Such classes do exist, for example the Java SDK has the Collections class which consists exclusively of static methods that operate ...


1

The answers to your questions ultimately hinge on how precisely one defines terms like "class" and "entity." You did a good job of defining the latter, allowing both physical and conceptual entities. But the term "class" to purists will always be "a factory for instantiating objects with a particular internal representation" and to pragmatists the term will ...


2

No, a class merely represents something that can be instantiated, has members, and can be inherited from. In fact, your language may have static classes, which are not even instantiated more than once. .NET's Math is a great example of a "professional" class that represent no entity (unless you want to get philosophical about what math is...), and also ...


5

Does an object have to represent an entity? Question: Which entity does a Logger represent? Not metaphorically - literally. When explaining OOP to students it is helpful to explain objects with analogies to the physical world. Alan Kay - one of the fathers of OOP - wrote the following: [...] The original conception of it had the following ...


0

Taking the given examples: getMotorDataFromHTMLForm() selectMotorFromUserRequirements($requirements) These look like "factory" methods, that would return a Motor object. The second implies that either you're creating a new object to meet requirements, or you have a database or in-memory collection of motors that you're examining. In which case it should ...


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


93

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 it's probably where the light started to come on for me: http://www.objectmentor.com/resources/articles/CoffeeMaker.pdf Objects ...


5

I think that visualizing something in the real world is helpful when coding classes but not necessary. In fact, depending on how literal you want to be, many objects we work with don't have physical representations at all. For instance--consider MVC architecture. In the real world there is no Model or Controller even though they are generally represented ...


3

Yes, you can do this. But you're essentially creating a class to provide the same functionality as a procedural library. The only reason to do that is if your language lacks procedural modules (e.g., Java or Ruby). In a language with procedural modules (I think PHP has procedural modules), you might consider just using a procedural module. You could also ...


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


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


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


0

The statement Fruit x = new Fruit(); creates a new object belonging to the class Fruit and x stores a reference to that object. More specifically, when the computer executes this statement, it allocates memory to hold a new object of type Fruit. It calls a constructor, which can initialize the instance variable of the object as well as perform other ...



Top 50 recent answers are included