Monthly Archives: March 2014

Pattern of the Week: Prototype

This is the seventh in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational

This week’s pattern is called the Prototype pattern. This pattern is used when you need to create exact copies of objects. Let’s look at our example Prototype class:

This StreetSign class has two properties – Text and Shape, and a method called Clone. The prototype pattern is fully represented by the “Clone” method. Basically, the Clone method should be implemented so that it returns an exact copy of the object on which it is called.

There are many ways to implement this pattern. A most ideal way would be if you could have the method reflect over the entire object. This would mean that you could add properties to the class without having to change the Clone method. The simplest way is the way that I implemented it in the code sample. A new instance is created and the properties are set on the new object (in this case, via the constructor). Either way is valid.

It should also be noted that there is a method on System.Object in .NET called MemberwiseClone, which essentially implements the Prototype pattern. The only thing about this method is that it creates a shallow copy, so if you have objects within your objects, those won’t be copied. Instead, the same instances of those sub-objects will be referenced by the clones.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of another pattern.

View Sample Code

Pattern of the Week: Proxy

This is the sixth in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Structural

This week’s pattern is called the Proxy pattern. This pattern is primarily used to add security or to implement caching for objects. For my example, I’ll be adding security to this class:

This is a pretty simple class, which implements the following interface:

Now, suppose I want to make sure that not everyone has access to the GetStrings method. Well, I could modify the GetStrings method itself, and add some complex code to check a user’s permissions, or I could implement a Proxy to do that for me. The main benefit to using this approach is that I don’t overly complicate GetStrings, and I can separate these two concerns (permissions and data access). Here’s my Proxy class:

The proxy implements the same interface as the original class. This means that you should be able to swap the proxy for the original object with minimal code changes. So how does this all work? Basically, we would use the Proxy to access the object, and if the user doesn’t have access (which we’re not checking here), a SecurityException will be thrown, preventing access to the underlying resource. Obviously, in a real-world use of this pattern, you would actually check the user’s permissions in the ValidateAccess method (and always remember to default to least privilege on errors).

You may notice that this pattern is fairly similar to the Decorator pattern. The difference between the two, as far as I can tell, is that the Proxy actually owns the object that it is adding functionality to (it creates it), while the Decorator has the object passed in as a parameter in the constructor.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of the Prototype pattern.

View Sample Code

My Dog, Chuckie

I spoke with my sister and dad on Monday night, and they told me that they had to put my dog, Chuckie, to sleep. I say “my dog” in the sense that for the last eight years, I didn’t live with him, I didn’t feed him, I didn’t take him outside when he needed it, but every single time I went home, I was the only person he was concerned with.

ChuckieWe got Chuckie, a light-brown cocker spaniel, when he was about 2 years old. He was, prior to that point, an outside dog. (This is something that I personally disagree with, to be honest, but I’ll save that rant for later.) There were several large dogs that also lived at that old house, and he had to fight for food and water. My mom joked to the woman who lived at the house that, “if you don’t want that dog, we’ll take him!” A few days later, they gave him to us. We already had another cocker spaniel, Jumanji, and my Dad didn’t want more than one, so we gave him to my sister’s boyfriend. Chuckie lived there for a couple weeks, until my sister took him. You see, her boyfriend’s family grew angry at Chuckie’s in-home restrooming, but instead of trying to train him, they kicked him. My sister saw this, and literally put the dog in her car, and left.

Once she got home with him, we decided to keep him. It was at this point, that Chuckie started to gravitate towards me.

Jumanji definitely considered my dad to be #1. Chuckie considered me to be #1. Every single night for most of my teenage years, Chuckie would climb under the covers and sleep next to me. He would always defend me against “I’m gonna get him”‘s and tended to end up wherever I was in the house. He sat beside me when I spent countless hours learning things about computers, or reading books, or searching the Internet for the latest anime, or playing PlayStation. It’s cliché to say that he was my best friend, but he was. There’s nothing like having a friend like that. He didn’t care about how cool I was or anything, he just wanted to be with me all the time.

Years have passed, and more recently, Chuckie had started not responding when we called his name. He had lost his hearing. His eyes also started getting cloudy. As it turned out, he was completely blind in one of his eyes, and the other one wasn’t doing well either. We had discussed what we would do if he did go completely blind and deaf, but I never really wanted to face that reality. Chuckie was my dog, and I didn’t want him to leave us.

On Monday, the veterinarian told my sister that a growth that was on his mouth was cancerous. It wasn’t something that could be removed, and it was an aggressive form of cancer. He was destined to decline quickly and painfully. They decided that in order to ease this suffering, they would have him put to sleep. While I wish that my family would have given me an opportunity to say goodbye, I am glad that he is no longer suffering from it. It would have been selfish of me to have made him suffer just so I could see him again.

The times that I shared with him will be with me for the rest of my life. Whether it was throwing the ball for him (and the way he couldn’t figure out what direction the ball would be bouncing), or just the way he HAD to sleep in my bed with me (and many other times when I wasn’t there, and he still slept in my bed because he missed me).

You were my dog. Rest in peace, buddy.