Tuesday 5 May 2015

Observer Pattern

 Observer pattern

 

Defined

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

When you're trying to picture the Observer Pattern, a newspaper subscription service with its publishers and subscribers is a good way to visualize the pattern.

In the real world however, you'll typically see the Observer Pattern defined like this.

The subject (the observed object) and the observers define the one to many relationship.
The observers are dependent on the subject such that when the subjects state changes, the observers are updated with new values.


 Strive for Loose coupled design between objects that interact.


Loose coupling is an approach to interconnecting the components in a system or network so that those components, also called elements, depend on each other to the least extent practicable. Coupling refers to the degree of direct knowledge that one element has of another.

The goal of a loose coupling architecture is to reduce the risk that a change made within one element will create unanticipated changes within other elements.Don't wish to code concrete update method implementations, as we have to way of add or remove without making changes to the program.

Diagrams

 




Code Example


 




 

 





Bullet Points

  • The Observer Pattern defines a one to many relationship between objects.
  • Subjects, or as we also know them, Observables, update Observers using common interface.
  • Observers are loosely coupled in that the Observable knows nothing about them, other than that they implement the observer Interface.
  • You can push or pull data from the Observable when using the pattern (pull us considered correct)
  • Dont depend on a specific order of notification for your observers.


Sunday 3 May 2015

Deisgn Patterns Principles

Design Patterns Principles

Design Principles List

  • 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. (Has-a instead of Is-a)
  • Strive for loosely coupled design between objects that interact. (minimal interdependency on other objects)
  • Classes should be open for extension, but closed for modification. that is, such an entity can allow its behavior to be extended without modifying its source code.
  • Depend upon abstractions. Do not depend on concrete classes.
  •  Principle of least knowledge - talk only to your immediate friends.

 Bullet Points

  • Knowing the OO basics does not make you a good OO designer 
  • Good OO designs are reusable, Extensible and maintainable.
  • Patterns re proven object orientated experience.
  • Patterns don't give you code, they give you general solutions to design problems.
  • Patterns aren't invented, they are discovered 
  • Most patterns principles address issues of change in software
  • Most patterns allow some part of a system to vary interdependently of all other parts.
  • We often try to take what varies and encapsulate it
  • Patterns provide a shared language that can maximize the value of your communication with other developers.

Friday 1 May 2015

Strategy pattern

Strategy pattern 

Defined

Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
The Strategy Pattern lets the algorithm vary independently from clients that use it,

Use when client of inherited base class may have varying behaviors and dependent of scenario you may want to use one or the other interchangeably.

Identify the aspects of your application that vary and separate them from what stays the same.
Take what varies and "encapsulate" it so it wont affect the rest of your code.

Diagrams


Code Example



i.e a CARs Exhaust Noise varys dependent of what type of Car your driving



interface IExhaustNoiseBehavior()
{
   private string noise;

    public string Noise()
}

class SportsCarExhaustNoiseBehavior :  IExhaustNoise
{
    private string noise = "brrrrm"
 
    public string Noise
    {
      return noise;
     }
}


public class Car
{
    IExhaustNoiseBehavior exhaustNoiseBehavior
      public performExhaustNoise() {  return ExhaustNoiseBehavior.Noise };

}

public class  Ferrari : Car
{

     IExhaustNoise ExhaustNoise
 
      public Ferrari ( IExhaustNoise exhaustNoiseBehavior)
      {
         ExhaustNoise =  new SportsCarExhaustNoiseBehavior();
       }
}

Bullet Points


  • defines a family of algorithms,

  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family