Coding patterns that I wish I knew when I first started out
Tags: beginners, programming, tutorial, csharp
Published: 2021-10-17 23:56:59

If you learned something new feel free to connect with me on [linkedin](https://www.linkedin.com/in/albert-bennett/) or follow me on dev.to :)<br/>
Hi there, in this post I'd like to talk about some common coding patterns that I really wish I knew when I had started my career 5 years ago.<br/>
A bit of a disclaimer first though, my career is all over the place. In those five years I have: started my own company, worked on large scalable applications, developed many POC's and I even did some APIM administration for a while. As such I've seen many of the same coding patterns get repeated over and over again. Here are some that I defiantly think that you should understand if you are starting out in your career.
Also if you want to jump into some code samples I have left a link to the GitHub project here. 
https://github.com/Albert-Bennett/DesignPatterns
On my old blog I wrote about many more patterns then I have gone through here in this post which is why there are so many in the repo.<br/>
**Abstract Factory/ Factory Pattern**
Essentially you use it when you want to instantiate objects from the same family of objects. Simple to say, easy to do... ish.

The main reason for using is to help with testing. Ideally the factory itself would be injected into the object that is going to make use of it. The objects that the factory creates would need to inherit from either an interface or an (wait for it...) an abstract class. Here is a uml diagram that goes through how this all fits together:![UML factory pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyw7px9lsnepnh199s04.png)The main difference between the abstract factory and the regular factory is in the abstraction of objects used. One has it the other doesn't. On to the code :), after all that's what we are here for.
The first step is always to define the objects that we will be instating in our factory. This is done by implementing an interface. The concrete creation of our objects will be done in the abstracted factory. ![code for abstract factory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d66cp3w3jchbqq1tjuqv.png)The second step is to define our factory. This again will be abstracted away so that it can be injected in when needed.![code for abstract factory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhjkrlpilcisqprt4tks.png)Next on to testing so that we can prove our stuff works.![code to test abstract factory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvtakuerdcmewk0ooc1x.png)Cool it works :) and there you have it an abstract factory that can be injected and used for creating objects. Neat!<br/>
**Singleton Pattern**
Have you ever had one of those moments where you need to have just one instance of an object and pass it around. Like a Frisbee? Well then the singleton pattern is the right one to use. It is an object where, yeah I've already explained. One and only one instance. Now inside of this pattern there are a few different ways of constructing it.
- Regular (normally creates the object on start-up)
- Lazy (this is used to create the object when it is first being used)

Here is a diagram of how it all fit's in together:![Singleton UML](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er92qykxb8v6pa71mc6r.png)The very first thing that we need to do is to create an interface to abstract our object.![Singleton code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0ze45jbb27r0fx1mvbw.png)Our next step is to turn this normal everyday class into a singleton. We do this by creating an instance of our MySingleton class inside of our MySingleton class (Singletonception...). This makes sure that there can only ever be one instance of this object as static objects point back to the same reference point in memory.![Singleton code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20m6fg2ao2fkmu0t1kkc.png)WOW!! Big changes essentially by creating an instance of a static object we have. Wait for it... we made sure that there is only one instance of the object. We can now call MySingleton.Instance to essential pass around the object. Neat! But... we can be lazy, which in this context is fantastic. Basically what we have done with creating an instance of a static object is created an object way, way before we may need it. Program executes... baam, object created. This is less than ideal, so we are going to be lazy about it (pun intended).![Singleton code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4vzqlab7upjoq56luar.png)By creating the object lazily we have said is that when, we first need the object create it. Instead of you at the start when no-one needs it. At all. Full stop. 
The line "static Lazy<MySingleton> instance = new Lazy<MySingleton>(() => new MySingleton());" looks scary but, essential all that we are doing is creating a new lazy object of type MySingleton and then when instance first gets called, create a new instance of MySingleton.
And there you have it. A lazy loading singleton.<br/>

**Builder Pattern**
Essentially this pattern is used when you need to build up a large and complex object. Instead of passing in all of the parameters at the beginning (Don't do it, try to stick to three-four at max). This pattern enables you to create a complex object bit by bit, to separate away the hassle of trying to manage a large constructor/ method signature. 
Here is a diagram of how the pattern is constructed:![Builder pattern UML](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uj1g4sxrpzl7s33u8ex7.png)This project is a little in-depth. So we need a situation. Our client is a Car manufacturer that needs us to help them build cars. First off lets design our car object and our builder's interface.
Codeward we go!!![Builder pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21aywitg9tbq4yffyq37.png)Cool, now on to implementing our builder and creating our client, that will encapsulate our builder.![Builder pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bapyw8gakqmoz54olp2i.png)Great, what we have done is created a builder that builds our Car and a client that oversees the build process. So what now, well now we see how to implement it. The important part of this bit of code is in the cloning/ clearing of the built Car before it has been returned. The reason for this is that now, the constructor doesn't have to be called again to start of with an entirely new Car.![Builder pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5wu8vf8ndhtapsoq4bl.png)Simples! Right well... sorry to break it to you but, we can improve on this by making it fluent. What's fluent I hear you ask. This basically means that we are going to be chaining our build calls together so that it's not so disparate. ![Builder pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehkhzz1f5c6malbcjt18.png)![Builder pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jul1pj23zp4m1l2p1c3h.png)And there you have it job done. A builder object that can be used to build objects.<br/>
**Adapter Pattern**
The adaptive pattern is all about converting one interface/ object to another. Simple as...
Here is a diagram of how it all works together.![adaptive pattern UML](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szdcsl8vam35ccvpi5k7.png)Target (ITarget): This defines the object that the client is expecting to get.![adaptive pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j1q86mfih57af4smj4b3.png)Adaptee: This is the object that needs to be adapted.![adaptive pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8cx5wvd0lzg635cm0tk.png)Adapter: This object holds a reference to the adaptee. It is responsible for converting the adaptee methods into ones that the client expects to see.![adaptive pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87ul6ic3batukv7rrrih.png)Here is an example of how this pattern can be implemented. As you can see our client class is only compatible with the ITarget interface and not the Adaptee.![adaptive pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oftbjz14r54tibvbhejr.png)<br/>
**Strategy Pattern**
First of, this pattern is used for when you want to change the behavior of an object without extending it. This is done through the use of a context object that holds a reference to an implementation of IStrategy. Here is a diagram to explain what it is:![Strategy pattern uml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8zcgz5rwho2xbqvvrzj.png)Context: This class is used to access and call the DoSomething method of our strategy objects.![Strategy pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dz3coq2vvm429j0lj79.png)StrategyA/ StrategyB: Theses classes are children of the IStrategy interface and where the core functionality is held.![Strategy pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8ugt2q9pmt8m22m79fl.png)As you can see from the image below, when we change the IStrategy object the behavior of the Context object changes.![Strategy pattern code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4l8f149kdak4g876tc9v.png)<br/>
I really do hope that this helps someone out or you learn something new from it. For me when I was starting one of my biggest failing was not knowing the names of the patterns. Sure I had used some of them before but, no one ever told me the name of them. It made for some... interesting interviews to say the least.
Thanks for reading my post and I hope to see you in the next one!