Tag Info

Hot answers tagged

25

Single Responsibility Principle, Separation of Concerns and Functional Cohesion. If you read up on these concepts, the answer you get is: Separate them. A simple reason to separate the Student from the "DB" class (or StudentRepository, to follow more popular conventions) is to allow you to change your "business rules", present in the Student class, without ...


7

Both approaches violate the Single Responsibility Principle. You first version gives the Student class to many responsibilities and ties it to a specific DB access technology. The second leads to a huge DB class which will be responsible not just for students, but for any other kind of data object in your program. EDIT: your third approach is the worst, ...


7

JavaScript is a multiparadigm language that you can approach from a pure OOP point of view, but most people don't, for various reasons I'll describe in a bit. In my experience, idiomatic JavaScript tends to be around 50% functional, 30% OOP, and 20% procedural. However, that mix varies a lot depending on what you're doing and who you work with. Some ...


7

In a word Simplicity. When you decouple too much, you get the hello world from hell. void main(String[] args) { TextOutputFactory outputFactory = new TextOutputFactory(); OutputStream stream = outputFactory.CreateStdOutputStream(); Encoding encoding = new EncodingFactory.CreateUtf8Encoding(); stream.Encoding = encoding; SystemConstant ...


6

Yes, what you're referring to is method chaining. You would add these methods to your class, and the return for each method would be the class itself. class Query { public string serverName { get; set; } public string tSQL { get; set; } public Query(string ServerName, string TSQL) { serverName = ServerName; tSQL = TSQL; ...


6

First off, "modern OOP" is not a deviation from Alan Kay's OOP. Kay may have coined the term itself, but Smalltalk-style "OOP" is a significant deviation from actual class-and-object programming as it existed before Smalltalk. The original OOP system was called Simula, and it was an extended dialect of of ALGOL whose OOP concepts would feel right at home ...


6

Since you tagged this with python, I'll give you the Python perspective on this. In Python, this is entirely normal. Attributes are not private, they are merely marked as 'internal', by convention, by using a leading underscore. So _store is something that is 'internal' to the class, just as the implementation of __eq__ is an internal matter. You are not ...


6

what if; what if; what if? YAGNI Seriously. If someone wants to use different implementations of File or Math or Console then they can go through the pain of abstracting that away. Have you seen/used Java?!? The Java standard libraries are a fine example of what happens when you abstract things for the sake of abstraction. Do I really need to go ...


5

The basic class in such an app will be an "Appointment" or "Event" class. It will contain the date/time and place of the meeting, who will be there, whether a reminder alarm should sound, whether the event repeats... All that sort of stuff. There should also be some sort of AppointmentCollection class that holds all the appointments, allows appointments to ...


5

No it isn't a code smell in itself. Having many implementations using only auto properties is a code smell - this is where you refactor common code into a base class and define it as virtual so that the extending class can override it where necessary. every other class implementing this interface uses Automatic properties which renders the getters and ...


5

Is it misleading to label classes or methods as a particular design pattern (Strategy, Mediator, etc.) if they only loosely fit that definition? It depends. If you have a singleton and it does not prevent instantiation, then it would be misleading to label it a singleton, since it's not a singleton. It's a global variable. Doing otherwise leads your ...


5

Kinds of objects For purposes of our discussion, let's separate our objects into three different kinds: Business Domain logic These are the objects that get work done. They move money from one checking account to another, fulfill orders, and all of the other actions that we expect business software to take. Domain logic objects normally do not require ...


4

It depends. If you need to calculate once, and use million times, then the answer is obvious: the first case wins. If you need to calculate every time, or quite often, then pick what you prefer. Your function doesn't read from a file, and has no slow operations, so it will not make a huge difference. Avoid premature optimization. When optimizing, it is ...


4

The key in understanding MVC lies in the separation of the responsibilities, as MVC is simply SRP applied to UI code. It separates what data has to be displayed, from how to display it, from how to handle screen events. But an important (and often missed) detail of the original definition of MVC is that it was designed for a far more granular level. For ...


4

The example is kinda messed up, and looks to me like it just hasn't been thought through. Currencies aren't like scientific measurements, you shouldn't implicitly convert between them on demand. The value of a currency will change on the day. You should really only convert between currencies with the end-users explicit permission, certainly not implicitly ...


3

A member variable is any variable that is scoped to the class. That means that any code within the class can access the variable. How the variable is used or how it is accessed doesn't matter, with respect to it's member status. Member variables and class/object state are the same thing. Example: public class MyClass { // Not a member variable; never ...


3

It's really hard to give good advice on such a broad topic. The honest answer is: it depends on what you want to accomplish. 1) Member variables could represent state of an object. This might or might not be, what you intended to do. If your object lives in a web- / concurrency-context, then that is most of the time not, what you want. In these contexts, ...


3

Technically: no, this is not a violation of the LSP: The type Node gives us some guarantees here: Any method may work as documented below or return null or throw an exception. Any subtype of Node must still live by this contract. In practice: yes, this is a questionable design. The DOM interface seems to use a variant of the Composite Pattern, and the ...


3

What does it mean to say that, class JButton or class JRadioButton is-a class Container? The reason why all Swing components are derived from java.awt.Container is mostly for practical reasons which are internal to the implementation of Swing. AWT and Swing usually will not be mixed. But internally, a Swing component might realize itself using more than ...


3

When talking about a system (or a module, or class, or structure), there are two representations of data : internal and external (I don't want to call them public and private as not to mix their meaning with OOP). The external is representation that is used by every single thing that is using a system. There is a risk, that changing this external ...


3

This sounds like the Visitor Pattern could be helpful. It does not get rid of the dispatching code, but it is a fairly elegantly and standard way to implement dispatching. In Java-style OOP: // an interface for anything calculatable interface FinancialInstrument { // acceptVisitor makes the pattern obvious, // but you might want to pick a more ...


3

If you look at the mergeSort code you will see that it casts each member of array a to Comparable. If any object in the array is not a Comparable, a ClassCastException will be generated unless you have created a Comparator which knows how compare your objects and passed that to the sort method. Example of the two different techniques (comparable v ...


3

if public access level method is also overridden in SubClass then public access level method has also become an implementation dependent method in every new SubClass designed, which breaks Encapsulation. Because user using public level access method addNotify(), should know that it's implementation is different in awt.Component/ awt.Checkbox/awt.Button ...


2

I know nothing about smalltalk, so I'll only talk C++. In C++ (and AFAIK in Java and C#), each thread has its own stack. If you don't know what a thread is, I suggest you read about it. I'll describe it here as the bus your code is taking a ride on. But I suggest you learn more than this horrible analogy (Also, not all C runtimes are multithreaded, so there ...


2

It's because an abstract class isn't complete. One or more parts of its public interface are not implemented. You've said what they're going to be - what they're called, what their name is, what they return - but no implementation has been provided, so nobody would be able to call them. Compilers prevent you from instantiating such incomplete objects on the ...


2

Because an abstract class by design usually has a unimplemented method. What should the program do if someone tried to call it? Having unimplemented methods on an object is usually a bug so the compiler helps you by not allowing them to exist. If you want to throw an error when the method is called then just implement it with just throws new ...


2

What's your use-case? Domain-Driven Design is (partly) about implementing specific models rather than general ones: a model which solves your particular business problem really well is much more valuable than a model which sort-of-solves lots of problems. To me it looks like the most general part of your current design (by which I mean the part which is ...


2

This interface is perfectly fine. I often see this kind of interface used on an object that's responsible for data storage, to hide the data storage code from the consumer. As long as you follow SOLID principles in your class design, it really doesn't matter what your interfaces define. Is the class behind this interface only responsible for one thing? ...


2

What you are looking at in the first diagram is how the AWT classes in Java use the Composite Pattern so that they are able to compose together elements in a larger element of the same type. The second diagram shows the generalized form of the Composite Pattern. The Composite Pattern In order to understand the composite pattern, let's use a more concrete ...


2

Let me start by answering your questions: Data representation is the storage type of the data and what meaning the possible values of that data have. For example, a Boolean value may be stored as a single bit, where the bit being 1 means truth and 0 means falsehood. Data representation is exposed by making something with a type (variable, class member) ...



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