Tuesday, January 27, 2015

Design pattern - Factory method pattern


What is a factory method pattern?

A factory method pattern is a constructor of objects. Basically, it defines an interface for creating an object, but let subclasses decide which class to instantiate.

If you are still having a hard time understanding this method pattern, just think of this. Factory design pattern is very similar to the real factories such as toy manufacturer where they produce toys. Of course they have their machines to do the job over and over again with a lesser risk of defects. And these machines are similar to your generator class. That's it.

Time to reflect

Imagine that you are in a factory where people create stuffs manually. How do you feel about it?

When to use factory pattern?

• generate objects of the same kind with common set of behaviour
• a class that defer instantiation to subclasses
• localise specific object to return

Below is the illustrated factory pattern in UML class diagram.



Sample code in Objective-C language


MyProduct.h

/* My product is the kind of component that we will return */
@interface MyProduct : NSObject
/* Product that will be returned */

@end

ConcreteComponent.h and ConcreteComponent.m

@interface ConcreteComponent : MyProduct
@end

@implementation ConcreteComponent
/* Implement me */
@end

AbstractFactory.h

@interface AbstractFactory : NSObject
  - (MyProduct*) generateComponent;
@end

ConcreteFactory.h and ConcreteFactory.m

@interface ConcreteFactory : AbstractFactory
  @property (nonatomic, strong) MyProduct* product;
  - (MyProduct*) generateComponent;
@end

@implementation ConcreteFactory
  + (MyProduct*) generateComponent
  {
    /* You can add properties if you want */
    return [ConcreteComponent new];
  }
@end

Main method

- (void) viewDidLoad
{
  [super viewDidLoad];
  
  AbstractFactory* factory = [ConcreteFactory new];
  id component = [factory generateComponent];
}


Factory method is one of the commonly used pattern in software development. I hope you can use them effectively.

Saturday, January 24, 2015

Design patterns - Introduction

What are design patterns?

"One day, your boss wants you to create a web application for the inventory system. You realized the problems during the development of the project and managed to solve them before. Now, you can't remember how you did it and which project is it. Isn't it frustrating?"

Well these problems are not new anymore and GOF has a suggested solution on how to handle things in particular context. Here I present "design patterns".

Design patterns solve almost all of the common design problems that happens in real-world application.

Why do we need to know / use design patterns?
  • Design patterns make your life easier that it removes a lot of constraints when designing specific requirements
  • It helps you to easily communicate with other intermediate or senior developers
  • Most of the design patterns makes your code organized and flexible

Okay, enough talking. What are these design patterns that you are talking about?

There are lot of design patterns.

Few of them are:
  • Factory pattern - responsible in creating an object in uniform
  • Memento pattern - commonly used in state-based situation
  • Adapter pattern - commonly used to support third-party or plug-able modules 
  • Strategy pattern - makes your action interchangeable

Don't worry, we'll discuss them one by one on my next post. Stay tuned!