Design Patterns: How to write reusable and tidy software?

Referred from : Python Unlocked 2015

Hello everyone. In this blog post I am going to tell about a important concept in software development which I came through called “Design Patterns”.

In software engineering, problems requiring similar solutions are very common. Therefore people generally tend to come up with a repeatable design specification to deal with such common problems. Studying design patterns helps one to have a basic idea of existing solutions to such problems.

Few advantages of design patterns are :

  1. They speed up the development process by providing tested and robust paradigms for solving a problem.
  2. Improves code readability for programmers
  3. Documenting the code also becomes easy as a lot of solutions are based on common design pattern. Therefore, less efforts are required to document code.

Let’s come to different design patterns used by people in software engineering. They are mostly classified as follows :

  1. Observer pattern
  2. Strategy pattern
  3. Singleton pattern
  4. Template pattern
  5. Adaptor pattern
  6. Facade pattern
  7. Flyweight pattern
  8. Command pattern
  9. Abstract factory
  10. Registry pattern
  11. State pattern

Let’s have a brief overview of the above-mentioned design patterns :

  1. Observer Pattern :  The key to the observer pattern is “Spreading information to all listeners“. In other words, when we need to deal with a lot of listeners ( which always waiting for a particular event to be invoked ) we need to keep track of them and inform them about the occurence of an event ( For example, change of state of variable ). Below code snippet will help to make the situation more clear :
    class Notifier():
        """
        Provider of notifications to other objects
        """
        def __init__(self, name):
            self.name = name
            self._observers = Set()
    
        def register_observer(self, observer):
            """
            Function to attach other observers to this notifier
            """
            self._observers.add(observer)
            print("observer {0} now listening on {1}".format(observer.name, self.name))
        
        def notify_observers(self, msg):
            """
            transmit event to all interested observers
            """
            print("subject notifying observers about {}".format(msg,))
    
            for observer in self._observers:
                observer.notify(self, msg)
    
    class Observer():
        
        def __init__(self, name):
            self.name = name
    
        def start_observing(self, subject):
            """
            register for getting event for a subject
            """
            subject.register_observer(self)
    
        def notify(self, subject, msg):
            """
            notify all observers
            """
            print("{0} got msg from {1} that {2}".format(self.name, subject.name,msg))

    The above code snippet provides a very simple implementation of the Observer pattern. There is a notifier object which provides a method to register the listeners. And in the listeners ( the Observer object ) there is start_observing function to register with the notifier.

Advertisements

Author: scorpiocoder

I am a 3rd year, IIIT-Chittoor student enthusiastic and confused about various software technologies : Python, Machine Learning, Networks, Natural Language Processing and Android Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s