class-design

a better way than casting from a base class to derived class.

I know downcasting like this won't work. I need a method that WILL work. Here's my problem: I've got several different derived classes all from a base class. My first try was to make an array of base class. The program has to select (more or less at random) different derived classes. I had tried casting from a base class to the derived c...

Javascript: How to access a class attribute from a function within one of the class's functions

Within my a certain function of a class, I need to use setInterval to break up the execution of the code. However, within the setInterval function, "this" no longer refers to the class "myObject." How can I access the variable "name" from within the setInterval function? function myObject() { this.name = "the name"; } myObject.pr...

Best design to implement "Default" item in collection

For my example, I have a Person class with an arbitrary number of associated Addresses associated with it. So there will be an Addresses collection as a member of the Person class. In many applications which use the Person class, we will just want to retrieve the "Default" Address object. There are a few design questions regarding the...

How will I know when to create an interface?

I'm at a point in my development learning where I feel like I must learn more about interfaces. I frequently read about them but it just seems like I cannot grasp them. I've read examples like: Animal base class, with IAnimal interface for things like 'Walk', 'Run', 'GetLegs', etc - but I've never been working on something and felt lik...

Pacman game class design.

Hi Everyone, I have to write a multiplayer pacman game in Java for a university assignment and I'm after some feedback for my design so far. So I'm trying to go down an MVC style and this is what I've sketched out. I've never designed anything using MVC, so my knowledge is really only from the pragmatic programmer and a short lecture ...

How to add callback function to a javascript class?

The following code in javascript gives me the error "this.callback is not a function function ajaxRequest() { var httpObject; this.open = open; this.callback = function(){}; function getHTTPObject() { if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRe...

In C#, what is the generally accepted way to refer to member properties within the class?

Have read through the MSDN naming guidelines and could not find a clear answer, other than that you should try to avoid underscores in general. Let's say I have the following: public class Employee { private string m_name; //to store property value called Name public string Name { get { return m_name; } set...

Is there a rule of thumb for when to code a static method vs an instance method?

I'm learning Java (and OOP) and although it might irrelevant for where I'm at right now, I was wondering if SO could share some common pitfalls or good design practices. ...

UML Class Diagram Resources

Does anyone have any good resources for refining my skills in developing class diagrams? Would like any strong tutorials in UML 2.0 ideally, but searches seem to be returning poor results. Also currently revising for a final year exam and really want to try and get my teeth into a practice paper with a model answer, I've searched high a...

How many constructors should a class have?

I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors. A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeMa...

Giving to child access to parent's member by reference - is it OK?

C++ newbie question. Please, verify I'm doing it right. I have a global application class spawning it's little kids and I need to give the kids access to some of the application facilities. So I decided to pass them to children by reference. I tested the idea as show below. It seems to work fine. I just wanted to make sure I'm not doin...

What is the most DRY way to get data out of my database?

I have to write an ASP.NET application that connects to our legacy IBM Universe Database and we are using a product called mv.net which allows us to connect, read, write, select, run server side programs, etc. I want as little code repetition as possible but I also want as little data transfer as possible as well. In order to open ...

Class design: entity ID vs entity reference

If I've got Foo with a property of type Bar. Both are persisted in a database which can be retrieved by an ID. (The IDs are actually used in the line of business by customer service claims. So they're not just index placeholders.) I could take the approach shown with b1 or b2. Chaining entities together scares me since if you push t...

How can I create a class file dynamically?

I want to create a class file dynamically. Here it goes... With the given ResultSet, extracting the metadata I want to build a class file dynamically with getter and setter methods for all the columns that exist in ResultSet. Also I should be able to use this class file generated where ever I want in my later use. Can any body suggest me...

How do I break my procedural coding habits?

I recently read an interesting comment on an OOP related question in which one user objected to creating a "Manager" class: Please remove the word manager from your vocabulary when talking about class names. The name of the class should be descriptive of its' purpose. Manager is just another word for dumping ground. Any ...

How do you keep your object model diagram and code implementation in sync during the project life cycle ?

When designing a new object model I always start with the class diagram function in visual studio. Once I have drafted the first version, with a couple of tweaks based on gathering new info or a change in requirements I start working on the actual implementation. As development gets busy and targets have to be met the diagram goes by t...

Is C++ friendship among peers healthy?

Although class friendship is one of the last resorts of C++, does this pattern make sense? class Peer { public: friend class Peer; void GetSecret(const Peer& other) { const std::string& secret = other.GiveSecret(); std::cout << secret << std::endl; } private: const std::string& GiveSecret() const { ...

C++ UML Class Diagram Autogeneration

Is there a tool which will take a set of c++ headers and output UML class diagrams? ...

Is there a heuristic to determine whether a method or field belongs in a class?

Is there a good rule of thumb or test I can perform to determine whether a method or field belongs in a class? How can identify when a member does not belong? I find that my single biggest stumbling block in object-oriented design is trying to figure out what goes where. It seems like there are far too many cases where the answer is: ...

How would you code an efficient Circular Buffer in Java or C#

I want a simple class that implements a fixed-size circular buffer. It should be efficient, easy on the eyes, generically typed. EDIT: It need not be MT-capable, for now. I can always add a lock later, it won't be high-concurrency in any case. Methods should be: .Add and I guess .List, where I retrieve all the entries. On second thou...