New answers tagged

0

The employee may or may not be a or the user. A user is part of the application domain, maybe not at all of the domain. I would associate a user with an employee. You may want to keep track of a 'former' employee also after they are no longer a user. Inheriting from user would be unfortunate then, there is no is-a relationship there anymore. Further sub-...


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


1

You use common sense. I might care a lot about whether the user is an administrator and add a method for that. I might care a lot about the user service object of the controller and use it a lot, for all the purposes that a user service object is designed to be used for. In that case I'd add a method for the user service object. I suspect that I don't ...


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


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


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.


0

Well, you could do it like this, if you really wanted to: for (int i = 0; i < myObjects.size() - 1; i++) { MyObject currentMyObject = myObjects.get(i); amount = amount.add(currentMyObject.getBillQuantity()); MyObject nextMyObject = myObjects.get(i + 1); if (currentMyObject.getCompletedDate() .compareTo(nextMyObject....


0

You could do something like this, where instead of starting at index 0 and allays comparing one forward, you can start at index 1 and compare one backwards. This method shouldn't go out of bounds, but it's certainty not the end of the world to leave the if statement there. Especially if it helps readability for others working on the project. for (int i = 1; ...


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


0

The road to Damascus was built in sections, and so shall yours. No one builds a 500 mile road all at once, all or nothing. Beware the Dunning-Kruger Effect Define data formats. Sometimes they're not obvious. These will always simplify the using code. Even small and simple structures. Data will be a primary way the software parts will communicate. And I ...


-1

Can anyone help in deciding how do I uniform these two through a single interface having two implementation? Or are they not meant to be uniformed via a single interface and deserve a separate way of implementation? In short, Interface Segregation principle is important because if a certain class becomes the new hotness then one would want to use it ...


2

I would decouple the three responsibilities for a repository (load book from XML, load book from a database, cache book) into three different implementations: public interface BookRepository { Book getBookFromId(string id); } public sealed class XmlBookRepository : BookRepository { private readonly string path; public XmlBookRepository(string ...


3

Instantiating and collecting small, short-lived, temporary objects, is perfectly fine. It is what modern garbage collectors are good at. Modern (generational) garbage collectors are built on a couple of assumptions: most objects die young, most objects are small, most objects don't escape, most objects are immutable, older objects don't contain references ...


-1

Garbage collection can become a performance issue in games, and might require special approaches. However, before you are sure that garbage collection of temporary Location instances is problematic, you would waste your time by trying to limit the amount of instances: “Premature optimization is the root of all evil.” To decide whether you should go down ...


1

A couple of possibilities: The vector form of the equation for a straight line in 2 dimensions is n.x + s = 0, where 'n' is a vector normal (or perpendicular) to the line "." is the vector dot product operation, and s is a scalar constant. This allows you to represent arbitrary lines with two integers and a double. (note that the same representation in 3 ...


0

This sounds to me to be exactly what the ICommand interface and command binding are for. You want to execute a command, with a certain parameter, when a certain UI action takes place. That is precisely what ICommand is designed to do. Just bind the sub-selection's Command to a corresponding ICommand property in your view model, and it's CommandParameter to ...


1

I'd probably have an object model design that looked something like this : public interface ISelectionModel { string DisplayName { get; } List<string> SubSelections { get; } void RunSubSelection(string sub); } public SelectionBaseModel : ISelectionModel { private string _displayName; private List<string> _subSelections; ...


0

One way to do this: Selections in the UI are an ItemsControl with an appropriate ItemTemplate (e.g. ToggleButton), bound to a collection of your objects (or view models representing them). Subselections in the UI are also an ItemsControl, bound to a collection representing methods of the selected object (this time the ItemTemplate could be Button). The ...


1

Two points: I prefer methods like these because they help break up method trains like: myObject.getFoo().doBar().tooManyCalls() (i.e. Train Wrecks) However, If you find yourself writing several of these "shortcut" methods you may very well have a bigger/better refactoring that you can take advantage of. See Bob Martin's book "Clean Code" and code-smell ...


5

Is creating methods such as isUserAdmin() bad practice? It is, though it isn't so much that this method is bad practice, but rather using a parent class as a dumping ground for methods you want to share with the child objects but that don't have any particular relationship to each other in terms of behaviour is a bad idea. If the method was a core ...


0

A single bit saying if the line is closer to horizontal or vertical. If closer to horizontal, slope + Y-intercept. If closer to vertical, inverse slope + X-intercept. That's two doubles, plus a bit. Equality testing is trivial.


9

The equations can be all rewritten into form : a*X + b*Y + c = 0 That means you can store three doubles a, b and c. This doesn't have a problem in representing an arbitrary slope. You can also calculate slope as a/b (or b/a). Two lines are equal if there exists k where a1*k = a2, b1*k=b2, c1*k = c2.


2

Possible methods For convenience of expression, this class will be used to represent any real points in a coordinate system in code snippets below: public class Point{ public double x, y; @Override public boolean equals(Object other){ if(!(other instanceof Point)) return false; Point point = (Point) other; return point.x == ...


1

OK I think some of the other answers touch on this, but I will spell it out. Constructors are not part of the Interface so you can have different ones for each repo. public BookRepoXml(string xmlStringContainingAllBookData) //or a filename to the bookxml? { this.cachedChapters = this.ParseXmlIntoChapterDictionary(xmlStringContainingAllBookData); } ...


0

If I understand correctly, your Book is basically a list of chapters with additional metadata and you want to add it two ways of loading through a unified interface. But depending of the persistance, the get/load operation changes. if I add GetNextChapter method in the interface, Database implementation needs only the chapterID as parameter, whereas Xml ...


1

The way I interpret your idea: You have about 200 strings that you want to search for in your target .txt Find any of these given strings Give the user(?) som kind of feedback Replace it with some other string Both strings returned in step 2 and 3 are mapped to the exact content of the string found in step 1, i.e: 'a' found -> print 'a_feedback', ...


1

Before applying a pattern, clarify what you want to accomplish! Do you want to reduce the side effects of those functions? Make sure your functions have proper input parameters and output their result as a return value. Then let the caller mutate the dictionary. Do you want to reduce duplicate (or similar looking code)? Start with 1, but make sure the ...


5

You could call the class a Facade for the database and key/value dictionary. A facade is a class which encapsulates operations which consist of multiple complex calls to different objects behind a single object with a much simpler interface.


1

Apart from what Alexander Langer said, there are not only good reasons for, but actual security policies for going even further and don't even hold any kind of credentials or temporary tokens in your domain logic. Practically speaking (and I apologise for not knowing DDD or the exact case well): the domain logic (BC, WebService, whatever) rarely has to ...


0

I'd suggest breaking up your design into pieces as follows First, build a communication contract between the clients and the servers. The client should use an abstraction of this contract and not worry that it is actually communicating over a websocket. Next, build an implementation of the contract(s) to communicate over a websocket. This could use a ...


2

The code you have shown and linked to is not a typical implementation of a factory. It seems the factory is here being used as a kind of dependency container, where classes can be registered to satisfy some dependency. The highly unusual parts are here that: Each Product has a createProduct() factory method, implying that it's supposed to create itself. ...


7

I agree that it is totally okay if a class has only one method, but recently I have read many blogs saying this is bad OOP and rather procedural coding. If a class has only one method, it's usually Execute(), or something equivalent. The question you have to ask yourself is, what are you encapsulating by using a class, if you only have one method? That's ...


3

Why nested classes ? Bjarne Stroustrup explains in "The design and evolution of C++", the origin and rationale behind nested classes: original C++ in 1984 had a single name space (page 5 and 102). the use of nested classes was a compromise between the the concept of a class as a scope and the need of compatibility with C (page 102) it was further ...



Top 50 recent answers are included