design-patterns

java generics design problem (state machine)

i made a state machine and would like it to take advantage of generics in java. currently i dont see the way i can make this work and get pretty looking code. im sure this design problem has been approached many times before, and im looking for some input. heres a rough outline. class State { ... } only one copy of each distinct st...

Looking for the most elegant code dispatcher

I think the problem is pretty common. You have some input string, and have to call a function depending on the content of the string. Something like a switch() for strings. Think of command line options. Currently I am using: using std::string; void Myclass::dispatch(string cmd, string args) { if (cmd == "foo") cmd_foo(arg...

Unknown amount of input needed in a form, ajax-less alternative?

I'm writing a little webapp for mobile. There is a form asking for a particular item and its details, users should be able to insert one or more items with their details. If it was a normal webapp I'd use ajax with a link "add one" just like gmail does for attachments. On mobile I think it's not a good idea using ajax, so I used "i'm d...

Dependency injection Container per request

Hi, It is very common to create Dependency Injection container for the ASP.NET application so it lives while the application lives. I create the DI container in each request and release it at the end of the request. The main purpose is that any DI container supports Disposing objects when the container gets disposed. Additional: ...

Properties editor design pattern?

Warning: This is super in-depth. I understand if you don't even want to read this, this is mostly for me to sort out my thought process. Okay, so here's what I'm trying to do. I've got these objects: When you click on one (or select several) it should display their properties on the right (as shown). When you edit said properties it ...

Pattern for initializing unknown subclass

I am trying to make a function f that works like this: StorageObject::addItem (const AbstractBase& base) { AbstractBase* storage = new Derived1(base); } However, I would like f to work with classes Derived2, Derived3 etc as well. Is there a way to get f to call the correct constructor depending on the specific subclass of Abstract...

Is this possible in PHP?

Consider the following PHP snippet: <?php class Is { function __get($key) { $class = __CLASS__ . '_' . $key; if (class_exists($class) === true) { return $this->$key = new $class(); } return false; } function Domain($string) { if (preg_match('~^[0-9a-z\-]{1,63}\.[a-z]{2,6}$~i', ...

Password hashing etc in Service Layer or Repository?

As a matter of best practice in MVC, where should the logic go that deals with things such as password hashing/salting or data formatting before it gets sent to the database? I've read that the repository should only be used for logic that deals with data access. Is this something that belongs in a service layer? The controller? Does it ...

Is it OK for a class to be observable and an observer ?

Had a code smell moment and couldn't decided if its OK for a class to be Observable and also an Observer, so thought I'd post here. class ObservableAndObserver extends Observable implements Observer { // Class is ofcourse registering and receving notifcations on different events // effectly actving as a middle man. // Is this a pa...

C# - publisher subscriber solve this?

Suppose there are different groups of scientists (Medical) serving emergency tickets. When an emergency ticket arrives for the common pool (group), at any time only one scientist is allowed to pick up the ticket , while others can or will be notified automatically that this particular guy is working on the ticket. note : A single guy c...

WPF design question (custom control or mvvm)

Here is the scenario: I have a visual that displays some data The data to the visual can come in one of two ways Via user input through the keyboard or mouse Via some backend source Both these data inputs can be of one of two forms Control data or Raw data for simple display Control data causes changes in the visual Raw data is sim...

Applying the decorator pattern polymorphically and coupling problems in C++

I am trying to make a C++ implementation of the board game Carcassonne. I am trying to make a tile object which has four sides and one of three basic terrains(field, road, city). The best interface for tile creation I could think of was of the form: City city; city_city_city_city = new Tile(city, city, city, city); Where a Tile clas...

Scope of View Models when using Service Locator Pattern in WPF app

Hi there, When using a Service Locator class to serve up ViewModels for your WPF pages to bind to. Should the ViewModels be Singleton scoped or Factory scoped? Is one generally a better idea for WPF applications?? I'm aware that in Silverlight, Singleton lends itself better to pages which are User Controls and are only moved into and o...

how to have identity map in doctine ORM

need to use a good PHP ORM that has elements of Datamapper and i am not clever enough to code it myself. chosen doctrine, but after reading through the user guide, cannot find anything that says how to use identity map to lower calls to database. please show me how to have identity map in doctrine ORM? just read and understood stuff ...

Register an object creator in object factory

I have the convenient object factory template that creates objects by their type id names. The implementation is pretty obvious: ObjectFactory contains the map from std::string to object creator function. Then all objects to be created shall be registered in this factory. I use the following macro to do that: #define REGISTER_CLASS(cla...

Database Replication OOD Pattern

Greetings fellow overflowers, After reading on MSDN about correct strategies on how to perform database replication, and understanding their suggestion on Master-Subordinate Incremental Replication. It left me wondering, what OOD design pattern should I use on this... The main elements of this strategy are the Acquirer, the Manipulator...

What is the best way to support multiple databases for a .Net product?

Hi, We are designing a product which could support multiple databases. We are doing something like this currently so that our code supports MSSQL as well as MySQL. namespace Handlers { public class BaseHandler { protected string connectionString; protected string providerName; protected BaseHandler() ...

Design patterns and interview question

When I was learning to code, I read up on the design patterns like a good boy. Long after this, I started to actually understand them. Design discussions such as those on this site constantly try to make the rules more and more general, which is good. But there is a line, over which it becomes over-analysis starts to feed off itself an...

Handling concrete classes explosion in Abstract Factory

As described in the motivation of using the Abstract Factory, i need an interface which i want to be highly flexible, i.e, it should have a number of possible behaviors. So, following the AF design pattern, I define an abstract class with the interface functions as follows: class WidgetFactory{ ... public: CreateScrollBar(); ...

Which design pattern would you consider when Logging is needed?

An application I'm working on requires logging of actions, user who performs the action, and time of action to a database. Which design pattern is most popular/appropriate for logging? I'm thinking of Command pattern that require the current user and an action. Perform the action and write to the log. What do you think? Any other ...