design-patterns

Is this hardcoding? How can I avoid it?

I'm creating a parser for a specific XML structure and I'm facing a possible hardcoding issue. Here: private function filterDefaultParams($param){ #FIXME Hardcoding? return array_key_exists('default',$param); } The literal 'default' is a valid tag in the Xml structure, is this hardcoding? May I use another technique to search ...

Diff collections of objects in PHP

I often come across a scenario where I have two collections of objects (either array or IteratorAggregate class) and need to diff the two lists. By diff, I mean: Detect duplicate objects (logic for detecting duplicates would vary case-by-case) Add new objects Remove objects that aren't in the other list Essentially, I'm looking for ...

Javascript: How do I retain a reference to a request initiator in a handler?

I'm not a Javascript person normally, but I've been diving in, reading Douglas Crockford's book, and writing some trivial, useful tidbits as Chrome extensions and Node.js (note that this question isn't about either of them). Right now, I'm trying to figure out how to retain a reference to an object that's initiating an AJAX request, th...

Class design: Passing values via Property(Set var) vs Function vs Constructor argument

This is a kind of open question. I always keep scratching my head when deciding between these. I can pass values to a class by: Passing an argument to class function: MyClass m = new MyClass(); m.DoSomething(arg); Passing argument when creating object: MyClass m = new MyClass(arg); m.DoSomething(); Setting the value using a differ...

How Exceptions are handled while designing DataLayer or any other layered architecture.

I am creating a data access layer where I want to handle exceptions that should be catched by Business layer with clear idea of the source of exception. I am doing something like this.. EDIT private void OpenConnection() { if (ConnectionState.Closed == _connection.State) _connection.Open...

Factory pattern implementation using ANSI C

Can anyone point me to a reference on how to implement the factory pattern using ANSI C? If more patterns are covered to that would just be a bonus. Doing this in C++ i trivial for me, but since C does not have classes and polymorphism I'm not quite sure how to do it. I was thinking about having a "base" struct with all the common data t...

"Independent" GUI window launching

I'm fairly new to GUI programming and I'm trying to write a plotting lib in D to use with some otherwise console-based scientific apps. I'm using DFL as my GUI library. Assume my plot form has a method called showPlot() that's supposed to display the plot on the screen. I would like to be able to have any thread in my app throw up a p...

.Net - Strategies to avoid magic string

In the code where I work, we got many think like that : if (user.HasRight("Profile.View")) {} So, there is many place where we pass a string as parameter to see if the user have a specific right. I don't like that because that generate a lot of magic string. What would be a better way of doing it ? Enum, Constant, class ? ...

Determining the representation-class for a data object based on its specialized type?

I tend to have a class to describe a general concept and subclasses to describe variances in that concept. For example, Polygon <|-- {Rectangle, Triangle, etc.}. However, I often find I have various representations of these hierarchies. For example, I want to keep the graphical representation (eg, a QPolygon), or the physical represen...

Pattern for client-side update in SOA

I want to develop a data-driven WPF application, which uses WCF to connect to the server-side, which itself uses NHibernate to persist data. For examle there is a domain-object called "Customer" and there is also a flattened (with Automapper) "CustomerDTO" which is returned by a WCF-operation called "GetCustomer(int customerId)". I don'...

Questions about Command Pattern (PHP)

i did up a minimalistic Command Pattern example in PHP after reading up about it. i have a few questions ... i'll like to know if what i did is right? or maybe too minimal, thus reducing the point of the command pattern interface ICommand { function execute($params); } class LoginCommand implements ICommand { function execute($par...

How do you handle Hibernate Session in Business Layer?

How do you handle Hibernate Session in Business Layer? Do you tie your Business Layer to native Hibernate API? (e.g. use session.load() in UserService.java) Any design pattern for Business Layer? Best Practices? I'm using hibernate-core 3.5.3-Final, Spring MVC 3.0.3.RELEASE. ...

What is the recommended approach to providing user notifications / confirmations in MVC?

A common scenario I encounter is providing notifications / confirmations to users after they have performed an action to inform them of success. For example, suppose a user provides feedback on a feedback form and then clicks Submit Feedback. You may want to display a 'Thanks for your Feedback' message after you have performed some vali...

clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)

Why does C++ have public members that anyone can call and friend declarations that expose all private members to given foreign classes or methods but offer no syntax to expose particular members to given callers? I want to express interfaces with some routines to be invoked only by known callers without having to give those callers comp...

Is this key-oriented access-protection pattern a known idiom?

Matthieu M. brought up a pattern for access-protection in this answer that i'd seen before, but never conciously considered a pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly make it non-copyable too }; class Bar { public: void protectedMethod(SomeKey); }; Here only a friend of the key class has a...

Creating custom events - Object Sender or Typed Sender?

I searched through the archives and I found lots of questions about what sender is and why you should use the pattern but I didn't see anything about a custom event and the type if sender. Say I am creating a custom class called Subscription and it implements ISubscription and I have some event args called SubscriptionEventArgs. If Subs...

JavaScript pattern for multiple constructors

I need different constructors for my instances. What is a common pattern for that? ...

Where to delete an object created by factory?

If I have a factory, that creates an object and returns a pointer to it, what will be a better way to delete it: By delete call in the "user" code, or by a new DestructObject function which I should have together with the factory? ...

Bridge Pattern - Composition or Aggregation?

I'm reading some books about Design Patterns and while some describe the relation between the abstraction and the implementation as a composition, some describe it as an aggregation. Know I wonder: Is this dependant on the implementation? On the language? Or context? ...

How to make a "plugin" comment system

i am wondering how are plugins like Disqus developed. they are like tagged to a URL (but abit more advanced, as they have to work with say different query strings, server side language, use of short urls etc). i want to ask this as i feel that it is a good idea to keep separate functionality like comments/ratings/reviews in separate co...