design-patterns

Design patterns with PHP

Does anyone have any good book recommendations on design patterns, and applying them in PHP? ...

How to properly use Struts ActionForms, Value Objects, and Entities?

I've inherited a large Java app that uses Struts, Spring, and Hibernate. The classes and interfaces I deal with daily are: Struts Actions, Struts ActionForms, Value Objects, Service Interfaces and Implementations, DAO Interfaces and Implementations, and Entities. I'm pretty clear on the how and why of most of these, except I'm unsure abo...

Class data responsibilities

I have a 'Purchase Order' class. It contains information about a single purchase order. I have a DAO class for database methods. Where should the responsibility reside for the methods that will load and update the purchase order? Should the PurchaseOrder class have '.update', 'insert', 'delete', and '.load' methods that use the DAO cla...

C++ Strategy Design Pattern, making an interface array

After having implemented the strategy pattern, I wanted to make an array of the interface-type, to which I can then add any concrete type. For those who don't know the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern In this particular example I would want to make a StrategyInterface array, which I can then fill with con...

Visitor Pattern + Open/Closed Principle

Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle, but still be able to add new visitable classes? The Open/Closed Principle states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". struct ConcreteVisitable1; struct ConcreteVisitabl...

Where is the benefit in using the Strategy Pattern?

I've looked at this explanation on Wikipedia, specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing some...

Is abstracting data type (sometimes) a good idea?

There are numerous times you have an interface that accepts similar type arguments that have a separate domain logic meaning: public static class Database { public static bool HasAccess(string userId, string documentId) { return true; } } Now it's quite easy to have someone key documentId instead of userId and vice versa. One could...

When you are abstracting your database records and datasets into objects, what does your object model look like?

I'm asking all of you who aren't using a library for this, but are constructing your own objects for managing data flowing to and from your database tables. Do I have a recordset object? one object per row of data? Both? Neither? Any suggestions or experiences welcome. Please don't tell me to use an ORM or other such toolkit. It's ov...

Design Pattern for implementing plugins in your application?

What is the standard way for allowing and implementing a plugin system for your application? In my last application I made a simple interface for all plugins that they must implement. I then load all assemblies in the apps directory and toss out any that don't implement that interface. One of the methods in the interface is a DoWork()...

Design pattern for converting one type of tree structure to another?

I have a sort of tree structure that represent a hierarchy of layers in a map, divided by types of layers and categories. Each node can be a different class for different types of layers (but all nodes implement a common interface). I need to convert that class to an ASP.NET TreeView control. Each node in the input tree is a node in the...

How to apply the MVC pattern to GUI development

I am primary a web developer but I do have a very good understanding of C++ and C#. However, recently I have writing a GUI application and I have started to get lost in how to handle the relationship between my controller and view logic. In PHP it was very easy - and I could write my own MVC pattern with my eyes closed - mainly because o...

Using explicit interfaces to ensure programming against an interface.

I have seen arguments for using explicit interfaces as a method of locking a classes usage to that interface. The argument seems to be that by forcing others to program to the interface you can ensure better decoupling of the classes and allow easier testing. Example: public interface ICut { void Cut(); } public class Knife : ICut...

Re-factoring from Active Record to Data Mapper

have you re-factored from an Active Record to a Data Mapper pattern? what conditions prompted the switch? i'm primarily interested in web based applications, but would like to know the challenges that accompany such a move in any environment. ...

"TryParse / Parse like" pattern: what is the best way to implement it

This question is a follow-up from How to indicate that a method was unsuccessful. The xxx() Tryxxx() pattern is something that can be very useful in many libraries. I am wondering what is the best way to offer both implementations without duplicating my code. What is best: public int DoSomething(string a) { // might throw an excep...

What design patterns have you used in your WPF efforts and which do you like?

I have been looking at the Model-View-ViewModel pattern that is suggested by several people out there (especially John Gossman, take a look at this post and this podcast), but what other patterns (if any) have people used and liked ... and where do they add value? I have also stumbled across: Model-View-ViewModel Presentation Model Da...

What are the differences between the Builder, Factory Method, and Abstract Factory patterns?

A program receives a list of Messages (base type). Each message in the list has to be processed according to it's type (descendant type). However, different messages need different inputs in order to be processed correctly. What is the following technique called? (I haven't checked this code in a compiler) abstract class MessageProc...

Would you consider this a singleton/singleton pattern?

Imagine in the Global.asax.cs file I had an instance class as a private field, lets say like this: private MyClass _myClass = new MyClass(); and I had on Global a static method called GetMyClass() that gets the current HttpApplication and returns that instance. public static MyClass GetMyClass() { return ((Global)HttpContext.Curr...

WeakReference and event handling

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected? As an argument to this: Folks say that if you subscribe to something it’s your responsibility to unsubscribe and you should do it. ...

How do you explain this structure in JavaScript?

(function() { //codehere } )(); What is special about this kind of syntax? What does ()(); imply? ...

Initializing static objects - Code design question

In my webapplication (C#, .Net 3.5), made up of a core class library (containing the business logic, data layer and a couple of utility classes), a windows service project, a webservice project and the website project, I have a couple of static classes in the core library used by all other projects. These classes (for example the Log cla...