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
  • Monday, 27 April 2015

    C# 5.0


    • Object Orientation - C# is a rich implementation of the object orientated paradigm.
    • Type Safety - C# is primarily a type safe language. i.e prevents you from interacting with a string type as though it were a integer.
    • Memory Management - C# relies on the run-time to perform automatic memory management has a garbage collector..


    Monday, 20 April 2015

    9.0 Sub Programs

    9.2 Fundamentals of subprograms
    - allow process abstractions
    - each has a single entry point.
    - the calling program unit is suspended during the execution of the call subprogram.
    - parameters are a way of giving a subprograms access to the data they are to process

    there are two types of subprograms - function and sub procedures

    A Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments, it doesn't require to return a value, but it cannot be used in an expression. (expressed with void in C#)

    A Function
    procedure, is a procedure that can take arguments, perform a series of statements, and change the value of its arguments, but it also must return a value and it can be used in an expression.

    - local variables = Variables that are defined inside subprograms.

    subprograms type check parameters

    9.5 Parameter passing methods

    9.5.1 Pass by value 
    - creates a deep copy of the object passed in from the caller, not a reference to the original object.

    - if the deep copy gets changed with the subprogram, the callers object will not be changed.


    9.5.2 Pass-by-Result
    Pass-by-result is an implementation model for out mode parameters.
    - helps assigning new values to multiple value-types.

    ie. void Fixer(out int x, out int y)

     9.5.3 Pass-by-Value-Result
     - a combination of pass-by-value and pass-by-result

    ie. void Fixer(int x, out int y)


     9.5.4 Pass-by-Reference
     - passes in an memory address of the object (shallow copy) , to the subprogram
    - if the object is modified within the subprogram, the callers object will persist those changes.


    9.7 Overloaded Subprograms
    An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an  overloaded subprogram must have a unique protocol, that is, it must be different from the others in the number, order, or types of parameters or in return type. 


    9.8 Generic Subprograms (Generic Method)

    -Software reuse can be an important contribute to software productivity.

    -One way to re-use, is to lessen the need to create different subprograms to do the same thing for different data-types


    - A polymorphic subprogram takes parameters of different type on different activations.

    - Parametric polymorphism - is provided by a subprogram hat takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram.

    i.e of a generic method
    . public static T doit<t> (t pl)


    11.0 - OOP Principles

    *Inheritance

     the ability to create class/interface which inherits certain aspects from inherited parent

    *Polymorphism


    The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
    Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

    Static Polymorphism

    The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are:
    • Function overloading
    • Operator overloading

    Dynamic Polymorphism

    • C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
      Here are the rules about abstract classes:
    • You cannot create an instance of an abstract class
    • You cannot declare an abstract method outside an abstract class
    • When a class is declared sealed, it cannot be inherited, 
    • Abstract classes cannot be declared sealed.

    * Encapsulation

    Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

    The following are the benefits of encapsulation:
    • Protection of data from accidental corruption
    • Specification of the accessibility of each of the members of a class to the code outside the class
    • Flexibility and extensibility of the code and reduction in complexity
    • Lower coupling between objects and hence improvement in code maintainability
    Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for functionality and add or modify its internal structure (data or methods) to suit changing requirements.

    Encapsulation is also known as information hiding.


    Uses access modifiers
    Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

    * Abstraction

    Abstraction from Object Oriented Programming perspective is extracting the core features of an object, without being specific about the implementation details.

    helps reduce the complexity of the software

    Sunday, 5 April 2015

    8.0 Control Statements & structures.

    Intro 

    Control statements 
     -Satements that allow for alternative control flow paths  and others  causing the repeated execution of sequences of statements.
    - allow programs to be flexible and powerful

    Control Stucture
    - Is a control statement and the  collection of statements whose execution it controls.


    8.2 Selection Statements
    -  A selection statement provides the means of choice between two or more execution paths in a program.

    • two-way/n-way category   i.e (2 -way) if then else,   (n-way) if then if then, else
    • multiple-way selectors (multiple selection) C++, java not so much C#

    8.2.1  Two Way Selection Statements

    If  control_expression
    then clause
    else clause


    *note the expression immediately after the if keyword is called a control expression 

    Nested selectors  - are just selection statements in selection statements. an if statement within an if statement

    8.2.2  Multiple- Selection Statements

    - Allow the selection of one or any number of statements.
    - C+=  java not C# - allow execution of more then one segment

    var a = 9
    switch (a)
        case 9
            a  = a + 3     ' now eqauls 12
       case 11
         break
       case 12  <---while hit here too
        break

    C# with statements different from c-base processors as they disallows implicit execution of more than one segment. each segment has to end with a break statement.



    8.2 Iterative Statements
    An iterative statement is one that causes a statement or collection of statement to be executed zero, one or more times. often called a loop.

    ie. for statement, foreach statement, while statement