Saturday, 27 July 2019

Design patterns in java

Design Pattern is a reusable and named solution to a recurring problem in a context. There are 3 categories of patterns when classified by the purpose:
  • Structural Design Patterns
    • Strategy Pattern: Defines a family of algorithms, encapsulates them and makes them interchangeable thus Strategy lets algorithm vary independently from the clients that use it. Example: Collections Java, List
  • Creational Design Patterns: Allows creational of the objects
    • Abstract Design factory pattern
  • Behavioural Design Patterns
Reference Books for Design patterns:
  • The Gang of Four: The Definitive Design Patterns Book
  • Head first Design patterns

Thursday, 25 July 2019

Object Oriented Programming

Building blocks of OOP:
  • Abstraction:
    • Showing the essential details by hiding the complexity.
    • In Java, achieved by Interfaces, Abstract classes.
  • Encapsulation:
    • Hiding the information. Can be field level or behavioural level.
    • In Java, achieved by access specifiers.
  • Inheritance:
    • Inhering properties, methods of another class to avoid code duplication.
    • In Java, achieved Extending a class or implementing any interface.
  • Polymorphism:
    • Allows the actual object to be decided at runtime, basically a subclass/interface can stand in for super class.
    • In Java, achieved Extending a class or implementing any interface.
SOLID Principles
  • Single Responsibility Priniciple
    • One class should own a single responsibilty.
  • Open-Closed Priniciple
    • classes should be open for extension, closed for modification.
    • Can be achieved by inheritance.
  • Liskov Substitution Principle
    • When you inherit a class, you should be able to substitute super class methods on sub class methods without any hussle.
  • Interface segregation Principle
    • Seggregate the interfaces in a way client can use.
  • Dependency injection Principle
    • High level module should not depend on low level module.
Other OOP principles:
  • Favor composition over inheritence.
  • Program to an interface not implementation.
  • DRY: Donot repeat yourself