A pattern is a proven solution to a problem in a specific context.To explain you in simple words it’s like recipe of “grandma’s chicken soup “.After so many trails she found a perfect way , proper ingredients which makes a delicious Chicken soup.
So Other Hungry people can use the same recipe or I can say a proven solution to make a perfect Chicken soup.
In this article we will be discussing few most frequently used patterns In Object oriented programming like:
1).. Singleton Pattern
2).. Factory pattern family
3)..Facade pattern
4)..Proxy pattern
1).. Singleton Pattern
2).. Factory pattern family
3)..Facade pattern
4)..Proxy pattern
Let’s Talk about each one by one :
1…Singleton Pattern:
Yes you read it right. There can be only one!!!!
To explain you Singleton pattern let me take an example : Let’s say you are developing an application with model view controller framework .in this case The controller is a central part which will be handling everything.
Like: Calling a proper screen, informing model to fetch the data , navigation between two screens etc..etc…
So there should not be another instance of a controller class exist though out the application.
So how you can manage to do that?? I mean if you have class anyone can create an object of that class. The simple solution is making a constructor private!!!! (Now who dares to create an object of me )
So how do I get the instance in my code??
Here comes Factory patterns into picture …
So there should not be another instance of a controller class exist though out the application.
So how you can manage to do that?? I mean if you have class anyone can create an object of that class. The simple solution is making a constructor private!!!! (Now who dares to create an object of me )
So how do I get the instance in my code??
Here comes Factory patterns into picture …
2..Factory pattern family
Factory pattern family has 4 members (4 ways you can achieve the instance of a class (Singleton class))
Simple factory Method: Encapsulates object creation in a static method.
A static method in class which creates an object and returns the instance of a class!!
Simple factory Method: Encapsulates object creation in a static method.
A static method in class which creates an object and returns the instance of a class!!
So instead of creating an object you will call this method which gives you the instance of controller class.
Simple factory: Encapsulates object creation in a separate class (not necessarily static!).
Use this pattern when you want object creation is handled by central class.so for the same model view controller your required objects will be provided by static methods of factory class.
Use: you can use this pattern whenever you have many single ton classes and you want a single factory class holds all the objects which are present during execution.
Factory method: Defines an interface for creating an object, but lets subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses.
Uses : Handle object creation , Can be parameterized ,Can deny object creation when conditions are not met , Can keep track of objects.
Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
3..Facade pattern
I love this pattern, it’s like make the coding look as simple as possible for outside world.
Keep a simple class with set of methods in front.
Our favorite Google is another example of facade. Its look so simple in front but that single page made it to the world’s most accessed site for searching ( Unless you don’t use it for checking internet connection !!! )
4..Proxy pattern:
Let’s say you are working in SAP system and you want to use certain methods of a class which your friend has developed in java system. How can you use it ??
Did it ring your bells (Web Service proxy???)
Ya calling a Web service using proxy pattern is the same stuff I am talking about. In sequence diagram this is how it will look like:
Client does not know anything about realSubect (JAVA method) .Proxy handles that part of calling Request () method in java system and authorization part .Proxy is part of SAP system here.
Did it ring your bells (Web Service proxy???)
Ya calling a Web service using proxy pattern is the same stuff I am talking about. In sequence diagram this is how it will look like:
Client does not know anything about realSubect (JAVA method) .Proxy handles that part of calling Request () method in java system and authorization part .Proxy is part of SAP system here.
Huh…Tired??? Let’s discuss other patterns like: Observer , Strategy, Template Method , Interceptor which are famous outside non-Object oriented programming in another article.
No comments:
Post a Comment