Design Patterns : #1 Introduction

Before we start whats design patterns, it is important to get familiar with design principle as well. Since both Design Patterns and Design Principle go hand on hand, we will just touch upon some Design principles before we deep dive into Design Patterns.

There are many types of design principles like SOLID, DRY, KISS, GRASP and etc etc.

Few Design principles statements are,

Identify the aspects of your application that vary and separate them from what stays the same.


Program to an interface, not an implementation.


Favor composition over inheritance. But use inheritance to achieve the type matching, 
but dont use inheritance to get behavior. Here Behavior means adding any member to any class adds new behavior to that class.


Strive for loosely coupled designs between objects that interact.


Classes should be open for extension but closed for modification.

During Design Patterns walk through, we will put focus light on SOLID principles mostly.

Basically we have 23 design patterns thanks to GOF and they are categorized into 3 types. From these 23 design patterns, rest others are evolved.

No alt text provided for this image

Lets start with Creational design patterns

  1. It abstracts the instantiation process which help us make a system independent of how its objects are created, composed, and represented.
  2. It becomes important as systems evolve to depend more on object composition than class inheritance emphasis shifts away from hard-coding a fixed set of behaviors toward defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones thus creating objects with particular behaviors requires more than simply instantiating a class.
  3. Class creational pattern uses inheritance to vary the class that's instantiated.
  4. Object creational pattern delegates instantiation to another object.
  5. It all encapsulate knowledge about which concrete classes the system uses.
  6. It hides how instances of these classes are created and put together.
  7. All the system at large knows about the objects is their interfaces as defined by abstract classes. Consequently, it gives you a lot of flexibility in what gets created. who creates it, how it gets created, and when. They let you configure a system with "product" objects that vary widely in structure and functionality; Configuration can be static (that is, specified at compile-time) or dynamic (at run-time).
  8. Sometimes creational patterns are competitors; there are cases when either Prototype or Abstract Factory could be used profitably.
  9. At other times they are complementary i.e. they can be clubbed together to make S/W design better. Builder can use one of the other patterns to implement which components get built. Prototype can use Singleton in its implementation.

Real World Example with name of Creation Design Patterns

Prototype : A fully initialized instance to be copied or cloned
Example : Coping formatted cells into spreadshet


Builder - Separates the construction of a complex object from its representation so that the same construction process can create different representations
Example : Kitchen is a Factory, Chef is a Builder where waiter tell to chef "pizza with cheese, onion". Here chef exposed attributes but hidden implementation.


Singleton - A class of which only a single instance can exist
Example : President of a country


Factory Method - Creates an instance of several derived classes.
Example : In an organisation HR works as factory method. Here development team request type of resource need to HR. Based on request type, HR provide resource to Development team.


Abstract Factory - Creates an instance of several families of classes
Example : HP, Samsung and Dell laptops are uses Intel and AMD processor.

要查看或添加评论,请登录

Manojkumar Gupta的更多文章

社区洞察

其他会员也浏览了