New answers tagged

0

Use an identifier that has an explict name. Validators with explicit names make your code so much more readable: instead of 'BusinessGroupA' having validators 'v1', 'v2', 'v3' and 'v4' the 'CustomerOrderGroup' is validated by the 'NoOutstandingPaymentsValidator', 'SufficientCreditValidator'...


0

What you are trying to do is the Facade pattern, but by triying to wrap all of a giant API you are incurring in the God Object antipattern. God objects violates Single responsbility principle and interface segregation principle and makes it hard to comply with Open/Closed principle. I don't know what language you are using but some languages support ...


0

In an object-oriented system, you'd normally place the behavior which varies according to its type in the type (AKA class) itself. But since Java doesn't allow you to add new methods to existing classes, unless you can use the toString() output of all or most of the classes in the XML converter and to reify the instances, you'll need to resort to another ...


0

It isn't a pattern, but an anti-pattern, in that the API requires the declaring scope to manage the token and will leak the registration token (and probably the registered object too) if you fail to do so properly. This could be remedied through the use of a destructor function for the object. But given the sample code appears to be Javascript, which has no ...


1

There really is no difference from what you have now and the second option you stated. Once again, you have common members, only they are now encapsulated in one class. This mitigates the problem when you need to add or change something in SharedGameSetupSettings, but if you need to add something else that is common for both SingleplayerGameSetup and ...


2

Let's call your first option option A, and the second one option B. Your only peeve about option A is that SingleplayerGameSetup "is basically empty right now.". I think that doesn't matter because obviously your code will grow with time and chances are SingleplayerGameSetup will not be empty forever. You cannot foresee the future. In option B, you would ...


2

I tend to find it a good idea to use a mapping at each point of conversion, yes. While maybe a hair more work to start, it tends to keep things from having to be split later and nicely isolates functionality into the context areas that it really belongs. Who's to say that a year down the road your external API source won't change? Easier at the outset I ...


2

Second option makes more sense from an encapsulation and class responsibility standpoint. In fact the word "Shared" is not really required - Settings captures a Seed and AiNations (whatever these might be) for any Game Setups. I wouldn't recommend creating abstract classes just to enable code re-use, unless the abstract class/interface really defines the ...


1

Summing up pros and cons of the proposed answers: The problem with the global helper file is the lack of flexibility, for example, in an application I have various enums with a specific enum constant that I would like to have a very specific key, not following the common format. The problem with locating the logic in the enums is the replication of code, ...


0

Keep in mind if the is a network between your program and the database, it is very easy to write code that becomes IO bound. Also, if you are processing transactions you may have to take several trips to the database depending on the mechanism used to track transactions.


0

I think the Builder pattern may help you. public class ConfigBuilder { public void SetPropertyA(string a) public void SetPropertyB(int b) public GeneralConfiguration Build() } The UI may interact with the methods exposed by the ConfigBuilder object. Once the user is OK, the Build method will be invoked to determine if ConfigA or ConfigB will ...


0

The usual procedure for syncing rows in a database would be to initiate a transaction session. Before updating your row you read a second instance of the same row from the database (during the transaction session to avoid changes in the meantime) and compare any column's data for changes. As comparing any column can be a performance intensive task especially ...


1

Just a hunch, but are you writing unit tests? Many small classes over a few large ones become more valuable as you test your code more thoroughly and from more levels of mocked out, unit environments or production-like environments. I don't think having a large number of classes in Java is a problem or is unusual, but I am concerned that you feel so ...


3

Is 20 Java classes for just making... This is entirely the wrong question. Something is wrong or you wouldn't be asking. It sounds like you're looking for something to blame. Anguishing over the number of classes isn't going to fix it. I've felt this same pain before. You step back and look at everything. It works. You can kinda follow it. But you ...


3

Frameworks/SDK's probably use instantance methods because it makes dependency injection possible, which is not really the case for static methods. However, if you don't use DI, a static method is the simplest way. KISS.


4

Static class methods are basically global functions, and are considered a bad idea in OO design. The reason why they are considered bad is hard to see in a simple code example where you are just comparing the difference between calling the same method on an instance or on the class itself. But when you get into slightly more complicated design global ...


1

What you are looking for is as some kind of (HTTP) Session. You can uses a cookie to store the reference id within your session store. You already using Redis and Redis is a "perfect" persistence to store session data. All operations are performed in memory, and so reads and writes will be fast. Redis provides a way to set the time to live or the entries ...


1

If the integrate function is only invoked once per object, what you have is a trajector builder, only it doesn't expose the trajectory as a first-class entity. So, you might make a first-class notion of the concept of a trajectory, and have your integrate function be a builder to construct the trajectory entity. Once you have a first-class trajectory ...


0

The data mapper has to know about the unit of work. You would typically pass the UoW to the mapper using DI. I understood why they are both important and when to use them but, I couldn't find anywhere how the Data Mapper can "know" that a save method, for example, was called in the middle of a transaction or not. A single UoW instance represents one ...


7

If you want a TrajectoryPrinter ask for a TrajectoryPrinter. Right now you're only asking for doubles. Something will need to build Oscillator. Something will need to build TrajectoryPrinter. I don't recommend that Oscillator build or even find TrajectoryPrinter. Oscillator shouldn't know TrajectoryPrinter as anything except as something it can call a ...


0

The boundary for a Unit of Work is time. It answers the question: What all are we changing in this one transaction? The boundary for a Data Mapper is space. It answers the question: What all is part of this one data structure? The unit of work is used to prevent hitting the database to often by sending every little chunk of info one at a time. The data ...


2

One approach that might fit is a finite state machine. There are tools that help you build and visualize them but you can also model it with standard OOP practices.


3

An old sage once said: You usually don't create an interface for every class, that would be an afterthought. You create interfaces as a design exercise, then you create classes that implement those interfaces. You have to think that what you are creating is always a foundation upon which someone else can built some bigger. Don't create little programs, ...


3

Interfaces exist (speaking of the interface keyword), so you can define an API for classes, where the implementation does not matter, only the arguments, return types and maybe thrown exceptions. You should treat classes and their public methods exactly the same, whether they implement an interface or not. Once you realize that, you will see even a class ...


-1

You don't need need an interface for every class. What I do is create interfaces for injectables and implement newables without interfaces http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/.


0

There are multiple ways to handle code reuse in Perl. A lot of examples do not make clear the distinction between the approaches and many classes use at least two. I advise using OO style as much as possible and only use the EXPORTER when you have at least three or more classes that need a relatively small cluster of utility functions. So: package Foo; ...


3

You are trying to create a layered design. In the top layer are inputs & outputs that interact with the elevator riders: the inputs are the button panel in the elevator and the displays of what floor it's on along with the up/down indicator and button pressed indicators. Further there are up/down call buttons on each floor (and a duplication of the ...


4

MVC is a paradigm from the Smalltalk world concerned with how object orientated systems could have UIs. Early web frameworks took the general idea (separate out business logic, controlling logic and view logic) and applied the principle to how they structured the web application. Before this it wasn't uncommon to have God awful mess of HTML generation code ...


3

Yes your feeling is good. It's not right to have such methods inside Model. 1a. Models should only contain properties (in my opinion). 1b. Data-related methods like your SQL fetching should be encapsulate within Repositories. Repository pattern helps abstracting out the database connections we are using. Says you have dozens of models like this, and one ...


2

Eugene Philipov ran a benchmark on multiple INSERTs in one query and found out that they are really faster than running many sequential inserts after each other. This really did not come as a surprise because INSERT is a very simple operation. For updates the reason why you'd be taking multiple trips to a database is because the code is simply easier to ...


2

I would go for the clone method if the class involved was polymorphic (so that an instance of the right runtime type was created). Otherwise it's a matter of taste in my opinion.


2

Premature Over-Engineering For example, I assert that you do not need any of those interfaces. If needed later, make them later. An interface (the C# keyword kind) is for giving common behavior to unrelated classes and then handle objects polymorphically. Think more carefully about what constitutes a more specific class. Think of what a shopping cart IS. ...


0

Based on your option 2, why not simply do this? If you want to prevent use of the raw base you can make it abstract: class Vector2 extends Vector { public Vector2(double x, double y) { super(new double[]{x,y}); } public double getX() { return getComponent(0); } public double getY() { return getComponent(1); } }


3

Use copy constructors. Here's why: IClonable semantics are ambiguous. Microsoft never specified whether a clone should be a shallow or deep copy. You can specify custom behavior in your copy constructor, such as giving each copy its own unique ID or only copying some fields and not others. Further Reading Copy constructor vs Clone in C#


0

With respect to modifying records when the user presses "ok" in the dialog, you must compare it with the database record instance. You cannot rely on cloning the original HTTP get because someone may have modified the record before you and you get race conditions. If this is not the case, this has been covered by another thread and I am hoping it helps you ...


0

MVC is pretty straightforward. Martin Fowler would, perhaps, disagree with this: Different people reading about MVC in different places take different ideas from it and describe these as 'MVC'. Moving on... When we create a website, it all come together as 'client sends REST keyword request to server -> the server matches the requested URL to ...


1

Is that how it is should be done? Passing the JSON as a view, or using it as a view model to construct the view does not violate the pattern. I am using the same architecture in the current application I am working on and it is working very good. Together with some nice JS framework you can create some really responsive designs. Or are there any other,...


6

View is a layer responsible for displaying information which may be interpreted by a user/client of your application (it does not say the user has to be an actual person). JSON is completely valid format for a view layer, computers understand that. As long as the view layer publishes information which can be used by a user to affect models in your ...


0

At this point, all you have observed is that in a server-side application, classes have a wide variety of dependency, creation, interaction, and scope management requirements. Some classes have temporal coupling between them; some classes need to Lazy<> all of their dependencies to delay their instantiations to the last possible moment. And threads are ...


1

The other end of the spectrum from the fat service class is the use of commands and a command processor. Basically each of your service methods is broken off into its own command. This article by Ian Cooper gives a good description of refactoring from a fat service to commands: Why use the command processor pattern in the service layer Some of the ...


1

I have a collection of cooperative classes whose behaviors are interdependent upon one another. But I wish to keep them loosely coupled... Careful now! When you are thinking that exact thought it's time to take a step back and look at what you are trying to achieve and why. Loose coupling is great, but it is not a goal of its own. Every time you make ...


1

If it is a little confusing to you, it's a problem. Someone unfamiliar to the code (which will probably be you in 2 months) will be utterly baffled. From the look of your code, you are letting yourself in for a lot of boilerplate. You might be best served looking at a single problem and saying "What's the simplest thing that could possibly work" and ...


4

To answer your questions directly: 1) No, it will not affect performance much. Generics are mostly erased by the time the program gets executed anyway, so the additional cost is really only multiple (up to 3) map lookups instead of 1. I would not expect this to ever be a bottleneck in a scenario where usage of a standard HashMap is acceptable. 2) Not ...


1

I think what you are looking for is a Business Rules Engine. It provides a nice neat reusable way of decoupling your "custom logic" from the "core logic". There are a number of lightweight FOSS BREs out there, hopefully in a language you're comfortable with, as well as some exceedingly expensive ones that are sold to organisations who want to be able to ...


1

Just write the code that works and does what you need. Don't worry about what to call that code.


1

The data belongs to the organization, not the application. While right now you're using Laravel exclusively, in the future there might be several applications interacting with the database. I'm currently working on an app that started in FoxPro, migrated to ASP classic, and was partially upgraded to ASP.NET. There are at least three applications interacting ...


2

You seem to have two different requirements, based on the method calls you provided. Only one (required) engine, only one (required) transmission, and only one (optional) stereo. One or more (required) engines, one or more (required) transmissions, and one or more (optional) stereos. I think the first issue here is that you don't know what you want the ...


3

Foreign keys in your database enables data integrity, as you can't delete a parent row if there is a child row in another table. While you can rely on the framework to handle data for you, the framework will not enable data integrity and you will eventually end up with orphan rows in your database. So, my advice is: design the database properly in order ...


2

Well there is no generic answer, for some it's a matter of opinion so i won't answer in a generic way but i'll take your points one by one. I'm answering considering that all of those will be designed as simple interface. Not implementation. a web service that downloads a PDF of a SQL Server Reporting Services (SSRS) report. This could be just part ...


3

Why not using the null object pattern ? Get rid of this builder, the most elegant code you can write is the one you actually don't have to write. public final class CarImpl implements Car { private final Engine engine; private final Transmission transmission; private final Stereo stereo; public CarImpl(Engine engine, Transmission ...



Top 50 recent answers are included