design-patterns

When and why should the Strategy Pattern be used?

When would the Strategy Pattern be used? I see client code snippets like this: class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrateg...

Refactoring Two Levels of Switch Statements

I've inherited a code base that makes heavy use of switch statements (C#, FWIW) to drive some logic. It's a multi-tenant web app where one set of switch statements pertains to how content displays and the other pertains to links to features, at least in most cases. I have an opportunity to refactor, so I'm taking the content-related sw...

Datepicker in Javascript.

What is the best way(pattern) to Create datepicker in Javascript. I have created one using Singleton pattern, but am not satisfied. ...

Looking for a Design Pattern

I have 80-90 classes which are all a type of calculation. But each class uses one method compute which is the same in each in class. The items that are different in each class are the are the instance variables used inside the compute method. The reason I am looking for a pattern is because what I am doing seems extremely repetitive.Be...

Design patterns for library-independent code

I have to use a 3rd-party library and I want to write code in which business logic is decoupled from the library vendor, so that in the future, if I change the library that I am using, there is minimal impact on my business logic. Please suggest me Design pattern that will be highly effective. I would appreciate if you can suggest me s...

Abstraction of behavioural logic - is there a design pattern?

Hi all, I need to abstract some behavioural code and have a problem trying to reference the objects in the class that is calling these behaviours, let me try to explain: My "parent" class, has a property called CurrentPage. I also have some behavioural logic, that modifies the CurrentPage property, currently this is written in the same...

Is this a design flaw?

Consider two classes class A{ public: A(){ } ~A(){ } }; class AImpl : public A{ public: AImpl(){ a = new AInternal(); } AImpl(AInternal *a){ this->_a = a; } ~AImpl(){ if(a){ delete a; ...

MVVM where to put Data Access Layer?

I am investigating WPF's MVVM design pattern. But am unsure where to put the Data Acess code? In some examples I have looked at, data access is performed directly in the ViewModel. It seems odd to put something like linq to sql in the ViewModel? Other examples have a seperate project for Data Access, this seems more like it? Is this t...

How can I manage a group of derived but otherwise Unrelated Classes.

It seems the more I talk about this problem the better I understand it. I think my previous question didn't convey what I am trying to do correctly. My apologies for that. In my design I have GameObjects which are essentially an aggregation class, all functionality in a GameObject is implemented by adding various "Features" to it. A Fea...

How to implement CRUD operations in UML

I was wondering if any analysts/architects have a solution for the hundreds of CRUD operations I would have to add to nearly every class in my UML diagram. Do you let each class inherit from a base class that implements the CRUD operations? Best practices and design patterns about this are very much appreciated! ...

InnoDB Transaction Help - Select then if Not Found Add Unique Row

Hi all! I have a mysql table that contains IPAddress parts TABLE `EndPoints`( `EndPointId` BIGINT(19) UNSIGNED NOT NULL AUTO_INCREMENT , `IpPart1` TINYINT(3) UNSIGNED NOT NULL , `IpPart2` TINYINT(3) UNSIGNED NOT NULL , `IpPart3` TINYINT(3) UNSIGNED NOT NULL , `IpPart4` TINYINT(3) UNSIGNED NOT NULL , PRIMARY KEY ...

Patterns for declaring functions for greater readability

In C++ functions needed to be declared before they were called. This could be worked around with function signatures but for the most part this is no longer required in newer programming languages, C#, Python, ETC. However, while reading other peoples, code and when having to structure functions in a class, I find that I miss the consis...

How to compare two distinctly different objects with similar properties

This is all in C#, using .NET 2.0. I have two lists of objects. They are not related objects, but they do have certain things in common that can be compared such as a Guid based unique identifer. These two lists need be filtered by another list which just contains Guid's which may or may not match up with the ID's contained in the firs...

Help me remove a Singleton: looking for an alternative

Background: I have some classes implementing a subject/observer design pattern that I've made thread-safe. A subject will notify it's observers by a simple method call observer->Notified( this ) if the observer was constructed in the same thread as the notification is being made. But if the observer was constructed in a different thread,...

Recurring Order pattern

I'm involved in a project that will feature daily or monthly recurring orders. Rather than reinvent the wheel, can somebody point me toward a pattern for such an animal? I'm thinking of some classic pattern like orders with line items. Something that helps us clarify our thinking and design. Thanks! ...

lazy initialization of singletons

While reading Jon Skeet's article on singletons in C# ( http://www.yoda.arachsys.com/csharp/singleton.html ) I started wondering why we need lazy initialization in a first place. It seems like the fourth approach from the article should be sufficient, here it is for reference purposes: public sealed class Singleton { static readonly...

How Much Designing Should Go On Before Any Coding Takes Place?

I'm currently in school and for my Senior Project we have to spend 1/3 terms just doing UML diagrams and other tedious documentation for our project. This involves alot of designing and planning for future problems that have yet to occur. For some reason this seems to be to encourage over designing. I've spent the last hour writing stu...

combining decorator and state pattern in java - question about OO design

I am in the middle of solving a problem where I think it's best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set amount of ingredients and a few different types of sadnwiches i can make. Each ingedient has a cost associated with it. The client would be...

While designing ORM, what is the best approach to represent relationship, performance-wise ?

While designing ORM, what is the best approach to represent the relationship, performance-wise? I mean, out of the following two, which approach is best considering performance? class Employee { int ID { get; set; } String Name { get; set; } int DepartmentID { get; set; } //This approach uses DepartmentID } --- OR ---...

Reducing memory of similar objects

I'm looking at reducing the memory consumption of a table like collection object. Given a class structure like Class Cell { public property int Data; public property string Format; } Class Table { public property Dictionary<Position, Cell> Cells; } When there are a large number of cells the Data property of the Cell clas...