New answers tagged

1

I agree with Martin Maat's remark that understanding the underlying reasons behind these practices will help you make sense of it all. You see, it's not so much about data hiding, it's more about strategically hiding implementation details (which include the choice of data structures you use internally, but also the exact details of how you implement ...


1

Is this data hiding? Yes, it is. Although you don't sound sure about it yet, it seems like you have understood the concept. That is already the full, direct answer to your question. A little more detail You were worried about changing the implementation, and that is a valid concern. However, there is an even nastier surprise waiting there; imagine code ...


1

A technical definition may be less clarifying than the reason for applying it. Your number 2 is just another property/ability of data hiding, not so much a reason to apply it. Data hiding is a form of encapsulation and de-coupling with the intent of minimizing impact when internal design will change later. Have clients talk to an interface rather than ...


4

Data or information hiding just refers to the practice of making your class variables inaccessible from other classes. In Java and many other languages you do that by declaring the variable as "private", in Python you prefix the variable with double underscore. So the examples that you've mentioned are both valid use cases for data hiding.


1

You should download the AutoCAD ObjectARX/.NET API and documentation. The documentation is not on web pages, but is available only in the download, alas. Look at how they implemented the Database and Geometry libraries. Database covers entities like lines, arcs, polylines, blocks, etc. that get stored in a drawing. Geometry is more of an algebra library ...


0

There are valid reasons for why you may choose to replace enums with something that is, in a certain sense, more flexible and better expresses your design. This is not something you'd apply everywhere (even in the same codebase), though; this is acknowledged in the introductory paragraphs of the Microsoft article you referenced: However, this isn't a ...


3

Basically both of the articles you reference are wrong. You should use enums where you have an enum. You should also have change control on your database. If you bypass your change control processes for your code you can add an enum value, recompile the code on your desktop machine, RDP to the server and copy the binaries over the top of the application and ...


0

Apparently you have two different fields. You want to treat them as one, which immediately gets you into trouble. Treat them as separate instead. You have one enum-backed field Whatever with a fixed number of predefined options. Your logic-bound enum is among these. And then you have another field wich you may refer to as CustomWhatever. This is just a tag ...


2

With enums this is fairly easy. We would just perform a very natural check if (cardType == CardType.Visa). The point is: the values are all there on code to be checked. If you are using a CardType class and there is some behavior that has to be different from one instance to another, then allow the CardType object to know it. So, if there are two ways of ...


0

It might help to remember that classes don't really exist in the real world (they exist in our mental models), but objects do exist. So, when I'm using a UML object diagram, I'm showing things that are instances, usually of real-world things. For example, if I say a Student is a Person, it's a generalization relationship. That relationship exists mentally, ...


3

Association and Composition make sense for objects, as well as classes. An object may use another (Association), and an object may contain another (Composition). But you can only Generalize a base class to create a derived class. It's not possible for one object to be a Generalization of another object. If the base class isn't abstract, it's possible to ...


1

Yes, wrapping your functions in a class is probably a good idea. In some other languages the class would be unnecessary, and it would be better to just write stand-alone functions. But PHP classes have one big advantage over functions - they can be loaded on demand via autoloading. Standard practice is to distribute libraries via Composer, which provides ...


3

Assume you were not the one who has written this library, but someone else. You don't know the lib, but you get the task of reusing functions from it if possible. Now you want to check if it contains a specific functionality for your current email-related use case. do you think you would find out easily there is function "email" in it - among 100 other ...


0

Q: Is it ok to have a class contain a list of its parent object? As usual, it depends on the problem you are trying to solve with such a relationship. But it's not bad or wrong per se. What you got here is somewhat a tree structure. That said, these models are a good candidate for circular references, something you definitively want to avoid. This's might ...


6

You could do it that way, but I sense issues. First, What if a user is both an Admin, and a MIS? It might not seem like it now, but eventually your organisation will generate a cross-functional individual. Second, Your current design is a good breeding ground for breaking LSP. How does the page (which knows Employee) call the special administrative ...


0

Inheritance is not the most suitable way to model this. Create an singleton AccessRights object instead and have Employee (singular form is preferred) query it to determine if it can do something or not. Like a method AccessRights.CanView(EmployeeId) that tells whether a particular employee is allowed to view employee data or not. AccessRights could be ...


5

This doesn't seem very object oriented to me So what? Just because you can write OO code, doesn't mean you should. Use the approach best suited to the task. Is it ok to have a static class holding queries that accept and output tables, and that holds no state? Absolutely. This is exactly what static classes are for … in languages that are completely ...


1

I really don't see how simpler can it get than this: class StudentDAO { public Student createStudent(Student studentToCreate) { jdbcTemplate.update(INSERT_SQL, .....); auditService.Created(studenToCreate.AuditInfo()): } public Student deleteStudent(Student studentToDelete) { jdbcTemplate.update(DELETE_SQL, .....); ...


3

Do you mean having a static factory method on the class itself? This is a pattern used in various places, usually combined with a private constructor so that the class can only be created via the factory method. A case I've often seen it used for is where a class can be created in different ways that are insufficiently explicit from constructor ...


Top 50 recent answers are included