Codementor Events

Important Concept maps and patterns for software developers

Published Apr 29, 2023

Good for refreshing your tech knowledge or for interview preparation.

This is an unstructured collection of useful concepts maps and notes about technologies, tools/frameworks and principles around software development, with more focus on Java development. It is very helpful if you’re preparing for an interview or for refreshing your knowledge.

Design pattern types
designPatterns.jpeg

Design patterns used in Spring framework
designpatterns2.webp

Creational Design Patterns
Abstract Factory. Allows the creation of objects without specifying their concrete type.
Builder. Uses to create complex objects.
Factory Method. Creates objects without specifying the exact class to create.
Prototype. Creates a new object from an existing object.
Singleton. Ensures only one instance of an object is created.

Structural Design Patterns
Adapter. Allows for two incompatible classes to work together by wrapping an interface around one of the existing classes.
Bridge. Decouples an abstraction so two classes can vary independently.
Composite. Takes a group of objects into a single object. A tree like hierarchical structure where different parts are treated as a single object. The component framework has four participants, namely, component (one which declares interfaces), leaf, client and composite.
Decorator. Allows for an object’s behavior to be extended dynamically at run time.
Facade. Provides a simple interface to a more complex underlying object.
Flyweight. Reduces the cost of complex object models.
Proxy. Provides a placeholder interface to an underlying object to control access, reduce cost, or reduce complexity.

Behavior Design Patterns
Chain of Responsibility. Delegates commands to a chain of processing objects.
Command. Creates objects which encapsulate actions and parameters.
Interpreter. Implements a specialized language.
Iterator. Accesses the elements of an object sequentially without exposing its underlying representation.
Mediator. Allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
Memento. Provides the ability to restore an object to its previous state.
Observer. Is a publish/subscribe pattern which allows a number of observer objects to see an event.
State. Allows an object to alter its behavior when its internal state changes.
Strategy. Allows one of a family of algorithms to be selected on-the-fly at run-time.
Template Method. Defines the skeleton of an algorithm as an abstract class, allowing its sub-classes to provide concrete behavior. The template method pattern is a technique that defines the steps required for some action, implementing the boilerplate steps, and leaving the customizable steps as abstract. Subclasses can then implement this abstract class and provide a concrete implementation for the missing steps. Spring provides JDBCTemplate, RestTemplate, JMSTemplate, and some other templates.
0*_jYXYv9yTEPPqT0c.webp
Visitor. Separates an algorithm from an object structure by moving the hierarchy of methods into one object.
Hibernate framework and its design patterns
Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs.

0*9GRwUzXevclx_ecj.webp

Configuration Object
It represents a configuration or properties file required by the Hibernate. It is instantiated once per application start. The Configuration object provides two keys components −

Database Connection − This is handled through one or more configuration files supported by Hibernate. (hibernate.properties and hibernate.cfg.xml).
Class Mapping Setup − This component creates the connection between the Java classes and database tables.

SessionFactory Object
Configuration object is used to create a SessionFactory object which in turn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application.

The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So, if you are using multiple databases, then you would have to create multiple SessionFactory objects.
1*Q6tDpTruTtbeeryMuqAupw.webp

Below is a code snippet depicting how we can create a session factory and session object.
1*V-Gko2QslH5wIOcLd_S_Dg.webp

Session Object
A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.

Transaction Object
A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).

This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.

Query Object
Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

Criteria Object
Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.

Hibernate cache levels
Hibernate supports two cache levels:

The first level cache is the session cache. Objects are cached within the current session and they are only alive until the session is closed.
The second level cache exists as long as the session factory is alive. Many sessions can share the cache. For this we need to configure third-party caching library like EHCACHE and add annotations to our cacheable entities.
1*EjeJSWvyvUCPx7iskC2RDQ.webp

Microservices design patterns
1*K4pkhs7j4ufSaWkWs-EUWw.webp

Spring Bean Scopes
0*P1fH74MbEH6xchkc.webp
Spring Bean life cycle
Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.

Java 8 functional interfaces
All Java 8 functional interfaces are organised in java.util.function package. Each functional interface in this package represents an operation that can be performed by the lambda expression.

Below table shows the list of all Java 8 functional interfaces along with their abstract method, which operation they represent and when to use them?
0*wn26nIEb96W6nSbp.webp

Commonly used functional interfaces
0*Gv9_d78mqyfRadYG.webp
7. Right data structure selection framework

How to select the right collection class (data structure) in Java
1*Fxy0kRVwNySHtmn8WhW-sA.webp

Additional characteristics of the collection classes i depicted below.
1*9xS_90AgSk5sw59iHa-PJw.webp

  1. Components of the JVM architecture
    1*6f9rAobX08Nr8hDs2AGmEA.webp

Front-end architectural/design patterns
Modular Architecture — We can see this pattern in ES6 with import/export syntax.
Complexity:
Component Architecture
Micro front-ends
State management — Redux, NgRx, RxJs, Flux
Dumb-Smart Components — dummy components can get data from smart components and display that. This happens mainly through two-way data binding based on @Input, @Output (EventEmitter<T>) in Dumb Component. With this annotations Dumb Component gets relevant data from Smart Component, or sends data to Smart Component. Smart Components generally injects Service or Facade and copes with data flow.
The Observer Design Pattern
If you’ve ever used the MVC pattern, you’ve already used the observer design pattern. The Model part is like a subject and the View part is like an observer of that subject. Your subject holds all of the data and the state of that data. Then you have observers, like different components, that will get that data from the subject when the data has been updated.

The goal of the observer design pattern is to create this one-to-many relationship between the subject and all of the observers waiting for data so they can be updated. So anytime the state of the subject changes, all of the observers will be notified and updated instantly.

If you want to see how the observer-observable pattern is used in the Spring framework, have a look here

  1. The Decorator Design Pattern

Using the decorator design pattern is fairly simple. You can have a base class with methods and properties that are present when you make a new object with the class. Now say you have some instances of the class that need methods or properties that didn’t come from the base class. The decorator pattern lets you dynamically change the base class without affecting it or any other classes.

class Sandwich {
  constructor(type, price) {
    this.type = type
    this.price = price
  }
  
  order() {
    console.log(`You ordered a ${this.type} sandwich for $ ${this.price}.`)
  }
}

class DeluxeSandwich {
  constructor(baseSandwich) {
    this.type = `Deluxe ${baseSandwich.type}`
    this.price = baseSandwich.price + 1.75
  }
}

class ExquisiteSandwich {
  constructor(baseSandwich) {
    this.type = `Exquisite ${baseSandwich.type}`
    this.price = baseSandwich.price + 10.75
  }
  
  order() {
    console.log(`You ordered an ${this.type} sandwich. It's got everything you need to be happy for days.`)
  }
}
  module.exports = { Sandwich, DeluxeSandwich, ExquisiteSandwich }

8. Strategy pattern
The Strategy design pattern is very used when we have similar tasks and we need to change between tasks at runtime.
This pattern helps us to eliminate a lot of if-else sentences, to do that simply we have to encapsulate the tasks in small chunks and use an object literal to access our concrete strategy.

Use case: Imagine that we have a dropdown with different user types (normal user, admin, and guest), and we want to display a form on the same page dependence on which user type was selected.
0*v8kNm-ISl9nV7omR.webp

Database selection strategy simplified
1*7M6EeWMXB0FB8Rwop3M2fg.webp

Discover and read more posts from Tewelle Welemariam
get started