Tag Info

New answers tagged

0

I think your best bet is to simply use the catch block to log the exception and any other information that will help you trace the error, including a stack trace. This satisfies your callers, who don't have to change their method signatures, and further satisfies the developers, who now have a way to diagnose problems involving thrown exceptions.


2

You don't need a "controller", you need a "service." A Controller controls the flow of the application based on user input and other events that occur in lower layers. A Service just naively goes about its business doing what it was told. The migration service object would have the intelligence for pulling data from the old schema and persisting it to the ...


3

OOP is not always the best choice for ETL tasks, thus I would not stick to "pure" OOP here. And as long as your model is as simple as shown in your example with just three tables to migrate, I would avoid to overdesign this - having simply one Controller which orchestrates the migration of just three tables, and which uses different "Row Migrator" classes, ...


2

Another approach would be to use composition instead of inheritance. We can look to the command pattern for inspiration. We would like to have a common interface while allowing variable actions. Rather than write the behavior into the Item itself (as durron597 does), we could instead create an intermediary object to hold the interactive behavior we want. ...


0

It's a bit of a code smell, and your gut feeling is right about this not being a good solution. But instead of passing the arguments, extract the method and the three parameters into a separate class. This class takes the arguments in the constructor and exposes the one method. Something tells me that this one method is actually doing something that can be ...


0

I don't fully understand your requirements, but to me it seems you hit a point where C#'s type system is more going to get in the way than actually help you. I would simply drop the idea of using classes and inheritance and just create a generic key-value store where key it name or type of the propery and value is an object containing the value of the ...


1

Using strategy objects in the described way is a good start to simplify your PointBase class, I would not hesitate to introduce them even if they do not solve your problem with the BreakEven property. For allowing custom properties, you could provide some kind "extension mechanism" in your PointBase class (which I would rename to Point after the redesign). ...


2

I posted a question here that was brought about by the same problem: trying to both take advantage of the convenience of referring to objects by their base class, while at the same time using additional properties that their subclasses may contain. The solution that was most useful to me was to use the visitor pattern. In your Point base class, you define ...


0

I have done it many ways. The approach you took is effective, though I often find my code is more readable if I use some extra characters on the variable name to help identify that there is one object, and I'm merely connecting to two facets of it: I myTestObject_i = new Test(); // creates a test, casts it to I J myTestObject_j = (J)myTestObject_i; ...


2

Why not this? public class Main { public static void main(String[] args) { Test i = new Test(); foo(i); ffoo(i); } public static void foo(I i) { i.doSomeThing(); } public static void ffoo(J j) { j.print(); } } It looks like, in the context of your code, you need a Test instance. You don't ...


1

Interfaces are a static tool that allows programmers to guarantee what methods an object supports. You are trying to use them as a dynamic mechanism for allowing users - or, more precisely, the user interface - to infer the actions users are allowed to do on the the object. What you need is a uniform way to programmatically access the list of actions at ...


6

I think you are suffering from YAGNI in your design. Think about it like this - when would you ever interact with a clock and a seat in the same way? Answer: you wouldn't! You might name a clock, and name a seat, but you wouldn't sit on a clock, nor would you increment a seat. It doesn't make any sense. Interfaces are for common behavior. For example, you ...


7

If I understand you correctly, it sounds like you're working with something like this pseudocode: void foo(Item i) { if(i instanceof Clock) { (i as Clock).increment(); } else if(i instanceof Chair) { (i as Chair).sit(); } } As you suspect, this is a code smell. A pretty strong one, actually. The immediate problem is that, because ...


1

Other than a few books that I could recommend on object-oriented design/programming in general, I don't think I have something to offer for your particular area. However, I think the reason you may be feeling frustrated is because you may not have realized, at least at first, that OOP requires a different way of thinking when compared to other programming ...


0

I am working on my own Java 3D engine with LWJGL which leads me to my answer below. I don't know much about JOGL. Both LWJGL and JOGL are just Java bindings to accomplish the same thing, which is to call native code that changes the OpenGL state of the current context. For example, in the current thread, a call in LWJGL to glCreateProgram() will call the ...


1

Others mention methods for classes which is the Object Oriented way. Sounds like your task is to safely order the procedures. Well, a procedure is (abstractly) a behavior of an object. Are you locked into a single class to manage these procedures as methods, or can you create two classes? Say, DoerOne withDoerOne::DoSomethingOne(), and DoerTwo ...


0

Though I agree to @Philipp that a more explicit passing of parameters can solve the problem, procedural code tends to work on some "global data" (or some member variables), and changing this might have a too big impact on the exsiting code base to be done easily. For this case, I can think of a different approach to solve this: DoSomething2() assumes ...


0

In my opinion, these methods themselves (as separate entities) mustn't be aware of any change of state. The concern with state should be in the form of a conditional invocation. I may be wrong, I may be under-thinking it, but I'd simply structure it like so: class MeshAlgo1 { void DoSomething(MeshData data) { DoSomething1(); ...


-1

When you are afraid that someone might call the methods in the wrong order, it seems to indicate that each of the private method transforms the data from one state into a different state. When that is the case you could represent each of these states with a different type. Each method would then accept an object of the previous state as input and return an ...


2

As you've found out, code works fine regardless of which file you put it in. So it comes down to organization... where will you put things? What I've found is that any departure from the "standard" conventions makes it very difficult to navigate through a code base of any significant size. You say you have this little helper class, but where the hell is ...


1

In general, the idea of one Class or Module per file is something I first noticed in Java (I believe it is/was a requirement there). C# has continued this as a suggestion, and VB.NET probably now has the same suggestion somewhere. However, as the VB.NET designers decided you can only have extension methods in Modules, you've provided an example where it ...


3

At a high level, you're essentially asking: Is building a Controller cache better than using singletons? I would argue that in your situation, yes, it is. And here's why. From your description it doesn't sound like you need the semantics of a singleton here. Your game would be just fine if you instantiated a new Controller every time one was needed. In ...


1

You should declare and create the object before the try-block. This is not specifically for com, but a general principle when using a resource that must explicitly be deallocated in a finally block. In other words - first create the resource, and if that succeeds you try to use the resource. On the other hand, if resource allocation fails, the try/finally ...


0

Object-oriented programming and algorithm development are orthogonal concerns. A person who understands both will be able to benefit from both. However, there are people who have lopsided understanding but are still able to focus work on just one side of the problem, although not in the most elegant and maintainable way. Your research mentor should have ...


4

Your example 1 is the right way to go because it is logically correct, which is far more important than a minor issue like limiting scope. Your scope is limited to this procedure, so that is good enough. If it really bothered you, you could break it up into three procedures, but I only would bother with this if it seemed like the procedure was getting too ...


2

My understanding of the Liskov Substitution Principle differs from yours, i.e. any implementation of IDoor should behave as a door – do what one expects when Open() or Close() is called – this includes objects implementing ITimedDoor (with ITimedDoor a subclass of IDoor). It does not imply that any arbitrary piece of code should be able to call SetTimer() ...


1

Consider the three pillars of OOP: Encapsulation: The capability to hide data and instructions inside an object Inheritance: The ability to create one object from another Polymorphism: The ability for objects of a derrived class to override/extend/inherit properties and methods from a base class If these benefits do not offer any real value to solving ...


2

There are several ways to look at this. One way is from the point of view of data abstraction. (I'm going to ignore looking at this from the perspective of programming paradigms, since @Robert's answer already does a good job.) The two most popular forms of data abstraction in use today are Abstract Data Types and Objects. (Actually, there is a rather ...


2

First of all, don't confuse OO modeling with OO programming. Most modern programming languages are more or less OO and can be used to implement most designs whether they have been derived using OO modeling or not. I can write a program following a relatively procedural design (or event-driven, or some other style) and still use Java Collections with their ...


0

What you need is CQRS(Command Query Responsibility Segregation) The idea is that what you save and what you load back and edit later is not the same object. This way you have control over what fields are private and what are public. What can be updated, and what can't. In CQRS you have Command Model and Query Model. Command model is responsible for ...


4

The responsibility for getting something done should reside with the object that models the state that changes when you do it. You're right that a lightbulb doesn't turn itself off. However, that's not what calling an object method means. The accepted mental model is sending a message to an object. In the real world you do this by flipping its associated ...


1

I have a data structure which has an add function. When the user instantiates a new data structure object, she can specify an algorithm which will be executed each time the add function is called and alters the newly inserted value based on the previous values in the data structure. The algorithm needs to have access to all the data in the data structure ...


1

One way to approach this problem without extending the tiers of your system is to implement the Command and Strategy patterns together in your design. Encapsulate the actionable method (in your case, "add") as a Command and decorate it with a Strategy that encapsulates your algorithm. Inside the Execute of your AbstractCommand, execute a virtual ...


3

The data structure should contain a link to the algorithm, since it has exactly one algorithm. Right? (as I understand the requirements) But the same algorithm, say, "normalize to the average" could work on many different instances of the data structure. So it should not contain/store an instance of the data structure. Instead, its method should take ...


0

Classes A and B should not have-a instance of each other; that way lies madness. You make no mention of what the post-add algorithms do, but since they must have access to the instance of A, they need to be methods of A, ignoring troublesome constructions like friend functions. class A: member x member y constructor(x, y, post_add_func(pointer or ...


1

If you have a cyclic dependency like this, this hints at one of two problems: the two objects should really be one object the two objects should really be three objects (with the inter-related parts extracted into the third) What you have here, is highly unusual. Adding an element to a data structure is one of the primary responsibilities of a data ...


0

Why can't Client also contain its own id value? Then you will always use the Client object (it is, after all, the data you need) whilst still maintaining the id reference to use to uniquely identify that particular client.


0

Forget about the API to start with. Model your business domain, which means a Project has a Client: not a clientId, a Client. Then, in the API, translate to and from the model and the external (JSON) view. That's one of the standard responsibilities of the API ("controller") layer. Of course, this approach only really makes sense if you're using an ORM ...


0

If a property is dependent only on the User, then it belongs on User; if it depends only on a Role, it belongs on Role. Clearly, if it depends on both User and Role, it belongs on a class that models the intersection of User and Role, i.e. something like Profile. A user can have many profiles; many profiles can refer to the same role, so it's just a normal ...


2

Well, it is possible to avoid the switch statement: You can create an abstract factory class, and have some classes inherit from it (ideally via a template argument) to produce objects of the different derived types. Then you can put all the concrete factories you need in a map and use the strings to index into that map to produce a factory for the desired ...


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


0

What are those role-dependent attributes? That's strange for users to have different sets of attributes, since they can change roles in a blink. What will you do with the attributes from role A when user no longer has the role? Delete them? What if tomorrow that user will change his role back to A? So, one approach is to maintain a single overly-bloated ...


1

This may or may not be a violation of the LSP. Seriously. Hear me out. If you follow the LSP, objects of type ProjectTask must behave as objects of type Task are expected to behave. The problem with your code is that you have not documented how objects of type Task are expected to behave. You have written code, but no contracts. I'll add a contract for ...



Top 50 recent answers are included