Design Patterns - Java

Basic


  • Formalize best practices to write clean codes
  • Implementation of design principles
  • Unified Modeling Language (UML) Diagram
    • The UML diagrams are categorized into structural diagrams, behavioral diagrams, and also interaction overview diagrams
  • Model2 / Model-View-Controller (MVC)
    • It specifies that a program or application shall consist of data model, presentation information and control information
    • The application logic is separated from the user interface while designing the software using model designs
      • Model => It represents the business layer of application. It is an object to carry the data that can also contain the logic to update controller if data is changed
      • View => It represents the presentation layer of application. It is used to visualize the data that the model contains
      • Controller => It works on both the model and view. It is used to manage the flow of application, i.e. data flow in the model object and to update the view whenever data is changed
    • MVC
  • Plain Old Java Object (POJO)
    • POJO is an object which encapsulates Business Logic
    • POJOs basically define an entity
    • POJO
    • Bean
      • Beans are special type of Pojos, All JavaBeans are POJOs but not all POJOs are JavaBeans
      • Fields should be private, Fields should have getters or setters or both, A no-arg constructor should be there in a bean, Fields are accessed only by constructor or getter setters
  • Data Access Object (DAO) Pattern
    • It is used to separate low-level data accessing API or operations from high-level business services
    • DAO
  • Separation of Concern
    • It is a software architecture design pattern/principle for separating an application into distinct sections, so each section addresses a separate concern
  • Patterns in SE
    • EE Patterns
      • Gang Of Four (GOF) patterns
      • High level or Low level
      • Tier
        • Presentation => Requests Handling, Navigation & Dispatch, View Processing
        • Business => Integrates, Separates, Captures
        • Integration => Encapsulates, Hides, Provides, Protects
          • DAO
            • Hides database implementation details
            • Returns transfer objects to the client
    • Architectural Patterns
      • Modal View Controller (MVC)
    • Patterns of Enterprise Architecture (EA)
    • Multi-tier Architectural Patterns
      • Element EA technologies
      • Tier Services

Types


Creational

  • Theory
    • Object Creation
  • Factory Method Design Pattern
    • Define an interface/abstraction and let the subclasses decide which object to instantiate
    • This is used when the Object creation depends on some condition
    • Factory Pattern
  • Singleton Design Pattern
    • It ensures a class has only 1 instance & provides a global access to it
    • Make constructor private and create object by static getter function
      • Needs to be thread safe => Use locking
      • Synchronization => Make getInstance() method synchronized, it isn’t very efficient
      • Double-checked locking => There are two checks for instance == null, one without locking and other with locking (inside synchronized) block
    • Used in logger, db connection
    • Singleton Pattern
  • Builder Pattern
    • Used where complex object with various configuration
    • Object creation but step by step
    • Builder Pattern

Structural

  • Theory
    • Simplifies the structure by identifying the relationship
  • Types
    • Adapter/Wrapper
      • Makes 2 incompatible interfaces, compatible
      • Converts the interface of a class into another interface that client accepts
      • Design Pattern
      • Example => Round hole and Square peg problem
    • Proxy
      • An object representing another object
      • Protective proxy, Smart proxy
      • Proxy Pattern
    • Facade
      • It provides a unified and simplified interface to a set of interfaces in a sub-system
      • It hides the complexity of sub-system from client
      • Promotes loose coupling between sub-system and client
      • Facade Pattern
    • Decorator
      • Attach a flexible additional responsibilities to an object dynamically
      • Uses composition instead of inheritance to extend the functionality of an object at runtime
      • Bridge Pattern
    • Bridge
    • Composite
    • Flyweight

Behavioral

  • Theory
    • Interaction between the objects should be in such a way tha they can easily talk to each other and still be loosely coupled
  • Types
    • Chain of Responsibility (COR)
      • Avoid coupling the sender of a request to its receiver, by giving multiple objects a chance to handle requests
      • COS Pattern
      • Exception handling in Java is based on Chain of responsibility pattern
    • Observer Pattern
      • Just define a one-to-one dependency so that when one object changes, state all its dependence are notified and updated automatically
      • Event based system => Event sent to sns by a Service which is subscribed by another Service
      • One change and all the subscriber gets notified => Broadcast type of communication
      • Low couping between sender and listener
      • Observer Pattern
    • Command Pattern
    • Interpreter Pattern
    • Iterator Pattern
    • Mediator Pattern
    • Memento Pattern
    • State Pattern
    • Strategy Pattern
    • Template Pattern
    • Visitor Pattern
    • Null Object

Questions


  • Split-wise design
  • Food ordering system
  • Cab system
  • Parking system
  • Elevator system
  • Online Seller Service
    • Layers/Models => User, Product, Delivery, Payment, Feedback, Seller, Support, Notification, Search, Tracking, Cancellation
  • Online Code Compilers
    • Models => Compilation engine, Test case, Question bank, User, Payment, Subscription, Discussion
Share: