Up until now I never really used the factory pattern that often in C++. Recently I found a use for it in a project I was working on and since I found it useful for my purposes, I thought I might share a tutorial on how the factory pattern can be used in C++.
Now I’m not entirely sure how closely my model fits to the typical factory pattern but as far as I understand factory pattern, it is pretty close if not exact.
Basically a factory consists of an interface class which is common to all of the implementation classes that the factory will create. Then you have the factory class which is usually a singleton class that spawns instances of these implementation classes.
This looked like a link that was fairly re-post worthy. This is practically a must for any .NET + LINQ + MSSQL developer out there. Very neat way of doing audit trails in MSSQL with LINQ and C#.
A couple months ago I wrote a basic set of extension methods to handle automatic auditing in LINQ to SQL. Well I have received a large number of emails regarding this particular project so I have decided to focus on cleaning up my v2 API and releasing it on CodePlex. There was a lot of room for improvement from version 1 and today I am going to post the all new LINQ Audit Trail code. This new version is significantly enhanced in the previous version.
So I’ve been doing a LOT of Javascript programming in the last few days and I ran into a few limitations of the language or constructs within the language. For example, certain versions of IE do not support the Array.indexOf() method. Another problem I ran into is that the Javascript language does not contain a method for performing deep copies on objects. So after some Googling I found a few ways around the aforementioned problems. The first one (indexOf) is as follows:
The above code will add the ‘findObject’ method to your Array objects. Then you can use that instead of indexOf() and be guaranteed that it will work regardless if the browser supports indexOf or not.
The second one (deep copying) can be circumvented by creating a helper method which will perform the copy for you:
if (typeof(oldObject) == “object”)
for (prop in oldObject)
// for array use private method getCloneOfArray
if ((typeof(oldObject[prop]) == “object”) &&
(oldObject[prop]).__isArray)
tempClone[prop] = this.getCloneOfArray(oldObject[prop]);
// for object make recursive call to getCloneOfObject
else if (typeof(oldObject[prop]) == “object”)
tempClone[prop] = this.getCloneOfObject(oldObject[prop]);
// normal (non-object type) members
else
tempClone[prop] = oldObject[prop];
return tempClone;
},
//private method (to copy array of objects) – getCloneOfObject will use this internally
getCloneOfArray: function(oldArray) {
var tempClone = [];
for (var arrIndex = 0; arrIndex <= oldArray.length; arrIndex++)
if (typeof(oldArray[arrIndex]) == “object”)
tempClone.push(this.getCloneOfObject(oldArray[arrIndex]));
else
tempClone.push(oldArray[arrIndex]);
return tempClone;
}
};
Just thought I’d share those with everyone. Credit for the majority of the deep copy code goes to http://blog.pramatiservices.com/deep-copy-in-javascript/ and credit for the findObject code goes to someone other than me, but I didn’t bookmark the link so I can’t find it again. Sorry
I’ve been doing a decent amount of string manipulation lately and decided to combine that with my newfound knowledge about .NET extension methods. If you don’t know what extension methods are, they’re basically a way to extend a class without needing the source code to that particular class–hence why I can extend the System.String type by simply writing another class. The key is with the parameters of your methods. Here’s the basic gist of it:
So the key here is the “this” keyword before the type identifier in the parameter list of the method.
I’ve wrapped all of my extension methods into a single class simply called “ExtensionMethods” and added it to my project. The namespace doesn’t matter, so I haven’t included it in this example. You can either use the global namespace (though I don’t recommend it) or just create your own namespace called whatever you want.
Without further adue, here’s the code:
/// <summary>
/// Asserts the trailing path delimiter.
/// </summary>
/// <param name="InputPath">The input path.</param>
public static void AssertTrailingPathDelimiter(this string InputPath)
{
InputPath = InputPath.IncludeTrailingPathDelimiter();
}
/// <summary>
/// Trims the extension from a file name.
/// </summary>
/// <param name="InputFile">The input file.</param>
public static void TrimExtension(this string InputFile)
{
int start, count;
if ((start = InputFile.LastIndexOf(".")) > 0)
{
count = InputFile.Length – start;
InputFile = InputFile.Remove(start, count);
}
}
/// <summary>
/// Gets a file extension of a filename.
/// </summary>
/// <param name="InputFile">The input file.</param>
/// <returns>The file extension</returns>
public static string FileExtension(this string InputFile)
{
int start, count;
if ((start = InputFile.LastIndexOf(".")) > 0)
{
count = InputFile.Length – start;
return InputFile.Substring(start, count);
}
return string.Empty;
}
}
Now whenever you use a System.String object, you’ll be able to simply call these methods as if they were part of the System.String class. IE: string MyFileExtension = MyFilePath.FileExtension();
I’m sure I’ll add on to this as I go along.
I’ve added a section to the site called “TI-83″ which contains just a couple of programs I’ve written for my graphing calculator over the years. A lot of these I wrote when I was in high school to assist me on quizzes and homework for algebra II and pre-calc. I’ve been finding myself relying more and more on my graphing calculator recently to solve complex equations–most recently was the Bernoulli Trials equation for my Discreet Mathematics class. Last session in school I damn near wrote a program that solved systems of equations using the Gauss-Jordan method, but then I realized that my graphing calculator already did this for me, so a program was not needed.
They’re all pretty small, but they might be worth grabbing. You’ll have to use your best judgement on using special math characters since keyboards and HTML don’t have them available for input/display. So for example instead of the square root sign that would normally be in a program or on a graphing calculator, I used “sqrt” and for something like the Greek letter Theta, i used <THETA> or (THETA). So the programs won’t port DIRECTLY into your calculator, you’ll have to use your discretion to make any modifications necessary.
Enjoy! *pushes glasses back up on nose with a single finger*