1.  Creational Patterns
    1. Prototype :  A fully initialized instance to be copied or cloned. For example  in a chess, we always start with the Fully setup chess board. One way is to  instantiate a Fully set-upped initial chess board every-time a game starts. And let’s say in your game, there is one game starting every minute or that the initial setup is computationally intensive, why waste all that time and effort.Prototype is where the solution comes in. With prototype, you create a initial chess board once and for every game , you clone the object, thus removing all the hassles, time and computational complexity.
    2. Builder :  e.g A Builder which builds a full course meal. It abstracts complex objects  for a single object. For example a Full course meal will contain drink, starters, main course and desert. When the details are complex i.e 4 diff objects , then it will be easier for the users to abstract all those complexities with a single interface. That is where the builder patterns comes in.
    3. Singleton : e.g President in a country. There can be only one instance of that particular class in  your system. In java System class, you can only have one instance of.
    4. Factory : e.g Person Factory generator generating male or female person type.Factory pattern allows for extension. For example if you want to introduce the “third Sex” Gender, then you can simple do so, without affecting the earlier male , female codes.
  2. Structural Design Patterns :
    1. Proxy pattern :  e.g a credit card  is a proxy to bank. An object representing another object.   e.g Remote object and home object. Home object is used as a proxy to some resource in a network. Its an impostor to the real object. hides the complexity.
    2. Decorator Pattern :  e.g Toppings in a pizza. There might be 10 diff toppings to 10 diff types of pizza. It is not feasible to create 10 * 10 = 100 classes for each type of pizza with different toppings. This is not feasible. Hence what we do is, use the topping class and pass it to  the pizza class  as a parameter which will decorate the pizza with the topping as defined dynamically in the Topping class.This is similar to discount on top of a order. We add a discount object to a order object as a parameter.
    3. Facade Pattern :  e.g event manager. This object takes care of all the things i.e music, flower, food, place. It is an abstraction to the object complexity going in behind the scenes. e.g Buy a book online. Check book , place order, update stock generate invoice. All this can be abstracted to a single object.
      It  reduces the network traffic, reduces the coupling. We can change the internal implementation without worrying  about the ordering process.It also helps in establishing the transaction boundary i.e if book  payment fails then reverse the book order. Frequently used in the web application
    4. Adapter : e.g Three – two point converter. Connect two different objects which have two different interfaces. We will use the adapter pattern to establish connections  between them.
    5. Fly weight : Fly the weight through common resource which might be limited e.g Fly all the HTTP request through the single port rather than creating one request per port. It makes the communication manageable and saves the expensive resource mis-utilization. Resource pooling, save computation.
  3. Behavioral Pattern: About interactions between the objects
    1. Chain of responsibility e.g loan request from bank staff to junior manager to senior manager based on the load amount and complexity. The object is passed across different chains of responsibility, as per the regulations or rules. Practical example include  exception handling from child to parent classes. It is a way of passing requests to chain of objects. Loose coupling, more manageable.
      Add or remove  chain objects as necessary
    2. Iterator  :  TV channel’s next and back button. Without knowing the technical details , complexity we can loop through the  object. e.g Array or array list or customer List, or order list, using iterator pattern we can simply use the next or prev to loop through.
    3. State : Alter a state’s behavior when the objects state is changed. Level 0 – fan does not rotate,. Level 1 – fast, level 2 – faster, level 3- fastest. We can do a if else on the state, but what  if we want to add a new level, we have to change the code. With state pattern we can simple add a new level, without having to change through the orig classes.
    4. Strategy : e.g sort using diff strategy , bubble or merge. In this case we can define a strategy class and pass its object to the  Sorter class. If we want to switch to diff strategy then in that case we define a diff Strategy and change the sort strategy from old to new one during the call and the strategy is dynamically changed.
    5.  Observer :  e.g Online bid e.g any change to bid price, then all the bidders are notified. All the bidders are observers of the bid.
Advertisements