by David Loo | Mar 7, 2014 | Design Patterns
What is a Factory Pattern?
Factory Pattern take cares of the creation of objects and hides the implementation from the client. Think of a real factory that make cars and there are different type of cars eg. sedans, hatch backs, and suvs.
When do You Use it?
You should use this pattern when you have a code that creates new object of different type but each object has the same method. The code below is a client method called Buy() that has a switch statement that determine which type of car object to instantiate to get the price. (more…)
by David Loo | Sep 22, 2013 | C#, Design Patterns
What is an interface?
Interface is a similar to a class it can contain properties, methods, events and indexers. Each member in an interface must have the public access modifier, and other access modifiers will not work with an interface. Interface do not contain any implementations, the only way to use an interface is by creating a subclass that implements the interface members.
public interface IDisplayMessage {
public void Show();
}
How do you use an interface?
To use an interface you must create a subclass that inherits the interface and implement it’s members. For example below I have created a subclass called MyClass the implements the IDisplayMessage interface and it’s members. Inside the Show() method I write my own piece of code to output a text message to the console:
public class MyClass : IDisplayMessage {
private string message;
public MyClass() {
message = "Hello, World!";
}
public void Show() {
Console.WriteLine(message);
}
}
void Main() {
MyClass myClass = new MyClass();
myClass.Show();
}
You can implicitly cast an object to any interface that it implements. For example below I am casting myClass to IDisplayMessage and still will be able to call the Show() method.
IDisplayMessage displayMessage = new MyClass();
displayMessage.Show();
Summary
Interface is similar to a class, it contains public properties and methods with no implementation. Subclass my using an interface must implement all the members and object can be implicitly cast to an interface that it implements.
by David Loo | Sep 22, 2013 | C#, Design Patterns
If you are like me and you don’t like to repeat code in your project, I am going to talk about reusing code by creating an abstract class using c#.
What is an abstract class?
Abstract class is a class that must be declared with the keyword abstract and it can not be instantiated and must be inherited by another class. The abstract class can have abstract methods, abstract properties and virtual methods.
public abstract class MyAbstractClass {
public abstract string MyProperty { get; set; }
public abstract void MyAbstractMethod();
public void MyMethod() {
Console.WriteLine("Hello World!");
}
public virtual void MyVirtualMethod() {
Console.WriteLine("This method can be overridden.");
}
}
Why do we use abstract class?
We can use abstract class to implement a common method which will contain reusable code, which then the class can be inherited. For example you I can create a simple abstract class with a method that implements code to print a message to a console and protected variable to hold a string message:
public abstract class DisplayMessage {
protected string message;
public void Show() {
Console.WriteLine(message);
}
}
Now I will create a two classes that inherits my abstract class and define a two different text message inside their constructors:
public class MyClass1 : DisplayMessage {
public MyClass1() {
message = "This is my class 1";
}
}
public class MyClass2 : DisplayMessage {
public MyClass2() {
message = "This is my class 2";
}
Now in the main method I am going to instantiate the two classes and call the Show() method to display each of the message on the console.
void Main() {
MyClass1 myClass1 = new MyClass1();
myClass1.Show();
MyClass2 myClass2 = new MyClass2();
myClass2.Show();
}
What is a Virtual Method?
A virtual method can contain implementation and must be declared with the keyword virtual. You can override a virtual method from the inherited class to implement code that is completely different from the one in the abstract class. To demonstrate this I will use the DisplayMessage abstract class and modify the Show() to a virtual method:
public abstract class DisplayMessage {
protected string message;
public virtual void Show() {
Console.WriteLine(message);
}
}
Now I will create two classes that inherits the DisplayMessage class, but one of the class will override the Show() method to display the text message four times:
public class MyClass1 : DisplayMessage {
public MyClass1() {
message = "This is my class 1";
}
}
public class MyClass2 : DisplayMessage {
public MyClass2() {
message = "This is my class 2";
}
public override void Show() {
for(int idx = 0; idx < 5; idx++)
{
Console.WriteLine(message);
}
}
}
Summary
Abstract class are very useful for implementing reusable code that uses a common method that will be inherited by other classes. Abstract methods and properties has no implementations but can be overridden by the inherited class. Virtual methods can have implementation and can be overridden by the inherited class.
Remember not duplicate code in your project, reuse code by implementing appropriate design patterns.
by David Loo | Mar 17, 2013 | Design Patterns
Composite pattern is a collection of objects where one of the objects in the collection can branch out. When using this pattern it usually consist of the following elements a component, leaf and composite.
Component:
- is an abstraction of all components, including the composite ones.
- declares the interface for the objects in the composition.
Leaf:
- represents a leaf object in the composition.
- implements all the Component’s methods.
Composite:
- represents a composite component.
- implements methods for manipulating children.
- implements all Component’s methods
Below are some code snippet taken from the design-patterns repository from my github account, the console application is called Composite and uses a Parent (Composite) and Child (Leaf) objects to represent the use of the pattern.
IMember Interface:
The IMember interface represents the Component and it consist of a method signature called Print(). This interface will be implemented by the Parent and Child class and they will also implement the Print() method for outputing information, but I will explain this later.
using System;
namespace composite
{
public interface IMember
{
void Print();
}
}
(more…)
by David Loo | Sep 10, 2012 | Design Patterns
Facade Pattern is a class (complex system) that contains multiple instances of objects (subsystem) each of these objects has its own methods that perform certain tasks. Inside the complex system class it can have one or more methods that wraps one or more subsystem object methods.
To demonstrate this I will use a Car as an example, building car consist of several process or subsystems to build the parts such as wheels, seats, engines and the body. The main system or complex system is to assemble the car.
Below I have a class called Body, Engine, Seats and Wheels, and each of these class contain a method to add certain parts.
The Body class contains a method called AddBody() which takes a enumerated type of BodyType, the body type can be either a sedan, hatch or a suv.
public class Body
{
public Body ()
{
}
public void AddBody(BodyType bodyType)
{
Console.WriteLine ("{0} Body Added", Enum.GetName(typeof(BodyType), bodyType));
}
}
The Engine class contains a method called AddEngine() which takes an integer value to specify the engine’s cyclinder.
public class Engine
{
public Engine ()
{
}
public void AddEngine(int cyclinders)
{
Console.WriteLine("{0} Cylinder Engine Added", cyclinders.ToString());
}
}
(more…)
by David Loo | Sep 9, 2012 | Design Patterns
An Object Adapters is a wrapper class the wraps an old object that needs data manipulated before the new object can use it, just like you would with a real life object such as an electronic device where main power plugs are different in each country, so you will need a power adapter to plug it in. This post I will demonstrate one type of adapter which is the object adapter, this type of adapter basically contains an instance of the object that needs adapting or also known as the adaptee.
Lets put this into a real word situation where you have an old class called OldPerson which only allows you to set and get a person name by calling a member method called SetFullName() and GetFullName(). In the new system you have a new class called Person which only takes First and Last name as strings. So what do we do you ask, we need to write a PersonAdapter class!
The Person class implements the IPersion interface which contains four methods, SetFirstName(), GetFirstName(), SetLastName() and GetLastName(). In PersonAdapter class we will implement the IPerson interface so that we have the same implementation as Person class.
public class PersonAdapter : IPerson
{
// code here.
}
In the PersonAdapter class constructer we are going to pass the OldPerson object as a parameter and retrieve the person’s full name. By using the string’s Split method we can separate the first and last name and assign it to the private member variables called firstName and LastName.
public PersonAdapter (OldPerson oldPerson)
{
this.oldPerson = oldPerson;
var fullname = this.oldPerson.GetFullName();
this.firstName = fullname.Split(' ')[0];
this.lastName = fullname.Split(' ')[1];
}
(more…)