Tag Info

New answers tagged

0

This works, but also introduces some side consequences of modifying the Group you pass, which you might not expect. But maybe this is the best (and relatively simple) way to do it? From a design perspective, to keep both lists in sync (if you want to handle it this way) is neither the responsibility of the player nor the group. You could design this ...


0

I will take the risk and assume that you have modeled the data as close to the source as possible. In this case: The List messages is just a structure, and yes it has a lot of method that you can use, but this methods are technical methods, normal operations on a List class. You want to create methods in the Command class that are more human readable by ...


0

Your biggest problem is that you're creating circular references which can lead to a world of problems, from endless loops to memory leaks. In a database you'd create a separate entity (table) to contain the references, translated into code that's a class with a reference to both a Player and a Group. Iterating over a collection of those will then give you ...


0

First and foremost, determine what is more common in your system - to list groups of a specific player or to list players of a specific group. Depending on which case is more common, implement it accordingly. If you try to implement both equally, you will take a performance hit for both. So, assumption goes Player belongs to a group, so: public class ...


2

I'd take a step back and ask yourself why you are trying to create a singleton in the first place. Don't get me wrong, I know there's a lot of people hating on singleton, and I think a lot of that hate is unwarranted. That said, it doesn't seem like your singleton is providing any functionality except acting as a global constant to IRCApiImpl. If you had ...


1

Well, as I see it, IRCApiImpl has a public constructor making it difficult to call either way a "classical singleton". Still, I'd say that if you follow the guideline of not creating another instance of IRCApiImpl at any other point or even better, making the class inaccessable, you can use either implementation. From a strict Software-Engineering point of ...


2

The simplest thing to do is write a function that takes a board state and gives you back a score for that board state. You then enumerate over all of the possible boards and chose the best one. That's really at the heart of it. Consider the game tic-tac-toe for a moment. Lets consider the person scoring 1 point for each X in a row, column, or diagonal ...


0

As I said in comments, the question is probably too broad (since there's no right answer, it's a typical "everyone, drop your two pennies" request), and your best course might be to ask more specific questions as you move along. This being said, if you are new to Android development, I'd draw your attention to these matters: Don't keep your business ...


3

Personally, I used to use final always (these days, from-time-to-time) only to ensure variables are not being re-assigned; nothing more, nothing less. The advantage of not reassigning variables is important to me actually, as it discourages the 'crutch' of temporary variables. This in turn encourages me to re-inspect my code for repetitive code (replacing ...


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


0

I don't think it is a good idea: using final for a method parameter, as you noticed, does not give any immutability guarantees I personally find that it clutters the code unnecessarily People using your code should not have to read your code! final is not part of the method contract / its javadoc so it is not the right medium to convey information to the ...


1

It is possible given enough money but this doesn't seem very practical. The obvious solution is to write the app in html/js/css with one of the single page app frameworks, angularjs or somehing else, and transform it to an android app with apache cordova or phonegap or what have you. That way, desktop users can just access the same code base you deployed ...


0

Every programming language is invented by different mind set, experience and objective, and there is no universal guideline setup for creating a language. So whenever a new language comes up it's their inventor choice to think about best naming/ syntax so this is all up to them what they think is best will add there. For example to add two numbers i will ...


2

The short answer is no, it does not work that way. To clear up the confusion, I find it helps to think of licenses as applying to distributions of software, rather than the software directly (as far as I know, this is actually closer to the truth). I'll pretend your project is called DEFM. If you release DEFM under GPLv3, then someone downloads it and uses ...


0

This is so straightforward I'm not even going to bother writing "IANAL". The fact that you have a project that depends on ABCL can not in any way alter the license of ABCL itself. ABCL does not depend on your project and therefore doesn't need to comply to your license - or even be aware of it. Think of the implications if licenses worked that way - some ...


3

That's not a quirk of your IDE - that's part of the Java Beans convention. There are many other Java tools and libraries(ORMs, serializers, configuration readers etc.) that rely on these conventions and will not work(or work poorly) if you disobey them. That being said - getters should not always be restricted to physical fields. If you have a class Circle ...


4

What matters about programming languages are the semantics, not the syntax. However, syntax is a vehicle for semantics. It is easy to show that two languages can have incompatible semantics (e.g. unrestricted pointers vs. memory safety, or differences in type systems). Let's focus on the syntax and semantics of variable declarations. When we declare a ...


8

Why cant be a global Standard to do all Simple and Common things? We tried that. The concept was called UNCOL, and the idea was that it would be ported to every architecture in the world, everyone would use it for everything. As you can tell, things didn't work out that way. Why not? Because programming is a complex activity, and there is no single ...


5

You are absolutely correct. new operator reserves space in memory for a new object of Type Fruit. Assignment = assigns the reference to your variable x. If you were to write Fruit y = x;, you would only create a new reference, so both y and x would still be pointing to single location on memory. For more in-depth knowledge, check out this article.


1

If you follow the Liskov Substitution Principle you should not have implementations which do not fully implement an interface. I rather have two or three different interfaces which state exactly what they can. Maybe a class will at the end implement more than one interface... The other benefit you gain is that when you have specific interfaces they do not ...


0

I will give a second answer, but you really should have opened a second question... First of all: you rarely ever implement a provider. Guice will automagically create them for you. To explain this: When I bind a class Foo as follows: bind(Foo.class).to(FooImpl.class) You can inject in you class either Foo or Provider<Foo>. Guice will create the ...


0

It's a bad practice to inject an injector, or otherwise pass it round in some way that makes it accessible to code outside of startup. That confuses what's going on; do that and when something goes wrong, you have to debug your code via debugging the dependency injection state. The right, static, way to do things is to inject either a hand-written factory ...


0

Ok, those are many questions you have and I guess some of the points can be approached in more than one way. Creating the injector is not cheap. Guice will do some analysis to detect errors as well as create all singletons. So you should create the injector at startup. I usually have a single injector for my application. Regarding your question 1.: Yes ...


0

The controller has a variable that can have a reference to any object of the interface type. The interface implementation class you speak of implements the interface, therefore it is a type of that interface. This means that variable can have a reference to an object of that class. This tutorial utilizes CDI which is something called a Dependency Injection ...


2

It is interesting that no one has pinpointed that OP' assumption that one has to override equals() when hashCode() is overridden is completely false. Though the opposite holds true - when equals() is overridden you have to override hashCode() as well. The mnemonic is very easy, just use the first letter of each method. equals() is before hashCode() in the ...


1

Best pracrice is to use different classes on both sides of the service. After all if you were an external client consuming the service you would hardly have access to the service classes. However, as you point out, that is a pain. I would share the data classes. perhape compile them and stick on an internal nuget repo?


1

To calculate the height, you have to traverse tree, possibly by using recursion. To check that it is balanced, you have to traverse it exactly the same way. So these ways can probably be combined. I am tired and can't think of a good name for such a function right now, and I have a feeling the solution is ugly and there is probably a better way. But, it ...


2

Generally speaking, an Exception is suited for this and the best practice you should follow is: "If a method can't do it's job, it should throw an exception." Understandably, you may want execution flow to continue without exceptions. The .NET space handles this fairly well and provides a convention for methods that don't throw exceptions. Methods that ...


3

First of all this method is not unit testable. There is nothing in the code you quoted which prevents it from being covered by unit tests. The presence of try/catch is irrelevant: You can test the situation where there is no error and check whether the side effects of this method correspond to the expectations. You can also test the situation where ...


2

If balanced means that the height is at most log_2(number_of_nodes) + 1, I suggest an algorithm could look like this: # define a tree tree := null | (left : tree, right : tree) # check if a tree is balanced is_balanced(tree) { maximum_height, number_of_elements = walk(tree) return maximum_height <= 1 + log_with_base_2(number_of_elements) } # ...


5

If you need to ask the question, you don't need to know the answer. If you had a profiler, profiled your code, and found this to be a hotspot, you could trivially find out the answer yourself by simply changing the code and profiling again. Since you haven't done that, we can readily deduce that in fact, you have no such hotspot and no such profiler, and ...


5

The way you're showing it now ("I instantiate the BallEntity this way"), it is being done by the implementation (code) that calls the constructor to create BallEntity. To be clear, the point is that the way you're showing it, everywhere that you create a BallEntity (whether in separate classes or multiple places within the same class), you're re-specifying ...


2

Maybe i missunderstood the problem, but it looks like these are actually separated entities. I would personally not use inherence but favor composition. I would create different classes for each entity implementing an ITrackable interface and simply define the GPS as an object in these entities. Using an Interface An IReportable interface can also be ...


-1

Assume both A and B have a million members, and you have sorted A and B. How many pairs can you form with A [0]? To find out, you add A [0] + B [0], A [0] + B [1], A [0] + B [2] etc. You stop when the result is too large, and then you know how many. Say A [0] + B [920178] is not too large, but A [0] + B [920179] is too large, then you can form 920179 pairs ...


2

Is there a name for splitting interfaces by accessors and mutators into separate interfaces? There could be a name for this if this seperation is usefull and provides a benefit which i donot see. If the separatons does not provide a benefit the other two questions make no sense. Can you tell us any business use-case where the two seperate Interfaces ...


0

In this case, it is perfectly okay to suppress the unchecked cast warning. It's an unchecked cast because E is not known at runtime. So the runtime check can only check the cast up to Object[] (the erasure of E[]), but not actually up to E[] itself. (So for example, if E were Integer, then if the object's actual runtime class was String[], it would not be ...


2

The key to doing this efficiently is exploiting the fact that if a1 < a2, then pair(a2) \subseteq pair(a1), where pair(a) = {b \in B : a + b <= S}. Example Java code: Arrays.sort( A ); Arrays.sort( B ); int count = 0; int j = B.length - 1; for( int i = 0; i < A.length; ++i ) { while( j >= 0 ) { if( A[i] + B[j] <= S ) { ...


0

I would first break it down into a number of classes with their own SINGLE responsibilities. You would need to apply GRASP principals here first. GRASP Secondly, I would look into strategy design pattern. Strategy Design Pattern


1

Let's tackle this: Would it be most appropriate to return a double value as I've mentioned, or provide public multiply(), add(), etc. methods? Should said class know how to multiply and add? Sounds like you are building a calculator implementation... I would say it really depends on which part of the calculator you are building this for. If this is the ...


1

I would suggest using obfuscation. It doesn't prevent reflection, but it'll make it practically impossible (technically, someone could still do it but it'll take them far more effort to figure it all out) for someone to figure out what part of the code does what, as it'll (amongst other things) scramble the names of functions. You can usually set ...


5

Is there any documented preference, in Java, for either of these conventions? The preference is that the end user has no idea what the backing primitive is for your type. This is encapsulation at its essence. Now, if your object exposes an interface to get a double (regardless of what the backing datatype is), either because you're serializing your ...


1

If you're doing logging, it would often be convenient to log the state of any singletons, as they represent global state that might be required to reproduce a crash. If your singleton can be logged by serializing a complete representation of itself, this is ideal as you might also be able to deserialize that state and rerun your program with the singleton in ...


-1

As far as I remember the point of a Singleton is to have only one instance of the class in existence. Serialisation is a transformation of data into another form(at), so that it can be easily written to a file, database or be transmitted, if I recall that correctly. So let's say you have a a class representing some definition (for example, you have a class ...


1

tl;dr The author seems to think all objects are value objects, but this is not true. Value objects should be immutable because their existence represents a specific value out of some value space. For example, an object that represents a pixel color value could be made immutable. The property of immutability can be used to reason about the problem more ...


1

Without performance overheads, can such implementations(class LinkedList<E>) be introduced by instantiating as immutable objects? Yes, it can. In fact, a linked list is the prime example of a persistent immutable collection. (I assume by "without performance overheads" you are talking about persistence?) public class LinkedList<E> ...


8

As per this link Seeing as rule #1 is horrible, I would caution against taking the rest of the blog post as law. Though really, no blog post should be taken as law. And the quote itself is rather contradictory: A good object should never change his encapsulated state. Be aware that immutability doesn't mean that all methods always return the ...


20

As explained in its javadoc, the purpose of Iterable is to support particular language syntax: Implementing this interface allows an object to be the target of the "foreach" statement As such, it belongs to the lang package, which Provides classes that are fundamental to the design of the Java programming language. Other classes at the diagram ...


1

I think it's easier to reason about an object that doesn't change state once initialized. For example value objects are pretty easy to reason about. Assign a value and query for properties. This is not always possible and I don't think it's a big deal, but it adds a level of complexity. Your methods might rely on a consistent internal state which is ...


5

Because a lot of things implement the interface Iterable or extend it as a sub interface. The implementing classes are: java.util AbstractCollection AbstractList AbstractQueue AbstractSequentialList AbstractSet ... concurrent ArrayBlockingQueue ConcurrentLinkedDeque ... java.beancontext BeanContextServicesSupport BeanContextSupport ... java.sql ...


0

Personally, I would design a property bag of sorts on my base class implementation. It looks like you are using Java so pardon my C#, but I think the idea can still be conveyed. public abstract class FleetUnit { public IDictionary<string, object> Properties { get; set; } public FleetUnit() { Properties = new Dictionary<string, ...



Top 50 recent answers are included