Friday, 30 August 2013

Iterator Design Pattern - Implementation


Click here to watch in Youtube : https://www.youtube.com/watch?v=uRiKSJAnmeA

Click the below Image to Enlarge
Iterator Design Pattern - Implementation

















IteratorPatternDemo.Java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class IteratorPatternDemo
{

public static void main( String[] args )
{
ArrayList<String> listOfCountries = new ArrayList<String>();
listOfCountries.add("India");
listOfCountries.add("US");
listOfCountries.add("Japan");
listOfCountries.add("France");

Iterator<String> iterator = listOfCountries.iterator();
System.out.println("Iterator type for the Datastructure ArrayList : "+iterator.toString());
System.out.println();
while( iterator.hasNext() )
{
String countryName = iterator.next();
System.out.println("Country Name : " + countryName);
}
System.out.println();
Set<String> setOfCountries = new HashSet<String>();
setOfCountries.add("India");
setOfCountries.add("US");
setOfCountries.add("Japan");
setOfCountries.add("France");
Iterator<String> iterator1 = setOfCountries.iterator();
System.out.println("Iterator type for the Datastructure HashSet : "+iterator1.toString());
System.out.println();
while( iterator1.hasNext() )
{
String countryName = iterator1.next();
System.out.println("Country Name : " + countryName);
}

}

}

Output

Iterator type for the Datastructure ArrayList : java.util.AbstractList$Itr@12dacd1

Country Name : India
Country Name : US
Country Name : Japan
Country Name : France

Iterator type for the Datastructure HashSet : java.util.HashMap$KeyIterator@30c221

Country Name : France
Country Name : US
Country Name : Japan
Country Name : India

Iterator Design Pattern - Introduction


Click here to watch in Youtube : https://www.youtube.com/watch?v=Pganyj1dVVU

Click the below Image to Enlarge
Iterator Design Pattern - Introduction
























See also:

  • Iterator Design Pattern - Implementation
  • Iterator Design Pattern - KeyPoints
  • All Design Patterns Links
  • Intercepting Filter Design Pattern - Implementation


    Click here to watch in Youtube : https://www.youtube.com/watch?v=sWtchigtgXk

    Click the below Image to Enlarge
    Intercepting Filter Design Pattern - Implementation






















    Filter.Java

    public interface Filter
    {
    public void processRequest(String request);
    }

    AuthenticationFilter.java

    public class AuthenticationFilter implements Filter
    {

    @Override
            public void processRequest( String request )
            {
    System.out.println("Authenticating the request by AuthenticationFilter : " + request);
           
            }

    }

    LoggingFilter.java

    public class LoggingFilter implements Filter
    {
    @Override
            public void processRequest( String request )
            {
    System.out.println("Request Tracking is done by LoggingFilter : " + request);        
            }
    }

    Target.java

    public class Target
    {
    public void processRequest( String request )
    {
    System.out.println("Process the Request by Target Class: " + request);
    }
    }

    FilterChain.java

    import java.util.ArrayList;
    import java.util.List;

    public class FilterChain
    {
    private List<Filter> filters = new ArrayList<Filter>();
    private Target       target;

    public void addFilter( Filter filter )
    {
    filters.add(filter);
    }

    public void execute( String request )
    {
    for( Filter filter : filters )
    {
    filter.processRequest(request);
    }
    target.processRequest(request);
    }

    public void setTarget( Target target )
    {
    this.target = target;
    }
    }

    FilterManager.java

    public class FilterManager
    {
    FilterChain filterChain;

    public FilterManager( Target target )
    {
    filterChain = new FilterChain();
    filterChain.setTarget(target);
    }

    public void setFilter( Filter filter )
    {
    filterChain.addFilter(filter);
    }

    public void filterRequest( String request )
    {
    filterChain.execute(request);
    }
    }

    Client.java

    public class Client
    {
    FilterManager filterManager;

    public void setFilterManager( FilterManager filterManager )
    {
    this.filterManager = filterManager;
    }

    public void sendRequest( String request )
    {
    filterManager.filterRequest(request);
    }

    public static void main( String[] args )
    {
    FilterManager filterManager = new FilterManager(new Target());
    filterManager.setFilter(new AuthenticationFilter());
    filterManager.setFilter(new LoggingFilter());
    Client client = new Client();
    client.setFilterManager(filterManager);
    client.sendRequest("HOME");

    }

    }

    Output

    Authenticating the request by AuthenticationFilter : HOME
    Request Tracking is done by LoggingFilter : HOME


    Intercepting Filter Design Pattern - Class and Sequence Diagram


    Click here to watch in Youtube : https://www.youtube.com/watch?v=5MppAW_lGnQ

    Click the below Image to Enlarge
    Intercepting Filter Design Pattern - Class and Sequence Diagram

    Intercepting Filter Design Pattern - Introduction

    Front Controller Design Pattern - Implementation

    🚀 Master Java Architecture!

    Join our community for clear, practical coding tutorials that get you hired.

    SUBSCRIBE TO RAM N JAVA

    Implementing the Front Controller Pattern

    In software design, the Front Controller Pattern is a powerful way to manage complex web applications. By providing a single entry point for all requests, you can handle common tasks—like security and logging—in one place instead of repeating code across dozens of different files.

    1. The Core Idea

    Think of the Front Controller as a Receptionist in a large office building. Instead of guests wandering around trying to find the right room, they all talk to the receptionist first. The receptionist checks their ID (Security) and then directs them to the correct office (Dispatching).

    2. Key Best Practices

    To implement this pattern effectively, keep these three points in mind:

    • Keep it Lightweight: The controller should delegate the "heavy lifting" to other specialized classes.
    • Centralize Common Logic: Tasks like user authentication or tracking page views should happen here.
    • Use a Dispatcher: The controller should use a separate 'Dispatcher' object to figure out which view or data to return.

    3. Why Developers Love It

    For beginners, this pattern might seem like extra work at first, but it makes your application much easier to scale. When you need to add a new security feature, you only have to change it in one file rather than fifty!

    Check Out More From Ram N Java

    If you're ready to dive deeper into Java and software development, check out these related videos:



    Click the below Image to Enlarge
    Front Controller Design Pattern - Implementation - Class Diagram























    UserView.java

    public class UserView
    {
    public void show()
    {
    System.out.println("Displaying User Page");
    }
    }


    SalaryView.java

    public class SalaryView
    {
    public void show()
    {
    System.out.println("Displaying Salary Page");
    }
    }


    AccountView.java

    public class AccountView
    {
    public void show()
    {
    System.out.println("Displaying Account Page");
    }
    }

    Dispatcher.java

    public class Dispatcher
    {
    private SalaryView  salaryView;
    private UserView    userView;
    private AccountView accountView;

    public Dispatcher()
    {
    salaryView = new SalaryView();
    userView = new UserView();
    accountView = new AccountView();
    }

    public void dispatch( String request )
    {
    if( request.equalsIgnoreCase("USER") )
    {
    userView.show();
    }
    else if( request.equalsIgnoreCase("ACCOUNT") )
    {
    accountView.show();
    }
    else
    {
    salaryView.show();
    }
    }
    }


    FrontController.java

    public class FrontController
    {
    private Dispatcher dispatcher;

    public FrontController()
    {
    dispatcher = new Dispatcher();
    }

    private boolean isAuthenticUser()
    {
    //here you have to write Authentication logic
    System.out.println("User is authenticated successfully.");
    return true;
    }

    private void trackRequest( String request )
    {
    System.out.println("Page requested: " + request);
    }

    public void dispatchRequest( String request )
    {
    // log each request
    trackRequest(request);
    // authenticate the user
    if( isAuthenticUser() )
    {
    dispatcher.dispatch(request);
    }
    }
    }

    FrontControllerPatternDemo.java

    public class FrontControllerPatternDemo
    {
    public static void main( String[] args )
    {
    FrontController frontController = new FrontController();
    frontController.dispatchRequest("USER");
    System.out.println();
    frontController.dispatchRequest("ACCOUNT");
    System.out.println();
    frontController.dispatchRequest("SALARY");
    }
    }

    Output

    Page requested: USER
    User is authenticated successfully.
    Displaying User Page

    Page requested: ACCOUNT
    User is authenticated successfully.
    Displaying Account Page

    Page requested: SALARY
    User is authenticated successfully.
    Displaying Salary Page


    See also:

  • Front Controller Design Pattern - Introduction
  • Front Controller Design Pattern - Class and Sequence Diagram
  • Front Controller Design Pattern - Key Points
  • All Design Patterns Links



  • Front Controller Design Pattern - Class and Sequence Diagram

    🚀 Master Java with Ram N Java!

    Join our community of developers and simplify complex concepts today.

    SUBSCRIBE NOW

    Visualizing the Front Controller Pattern

    The Front Controller Design Pattern is a fundamental concept in Java web development. While the theory is great, understanding how it actually looks in code and how data flows through it is where the real learning happens. In this guide, we dive into the class and sequence diagrams that make this pattern work.

    1. The Class Diagram

    A class diagram shows us the "blueprint" of our application. In a Front Controller setup, you typically have three main players:

    • FrontController: The single entry point that handles every request.
    • Dispatcher: The component responsible for choosing the right view or action.
    • Views: The final pages (like JSP or HTML) that the user eventually sees.

    2. The Sequence Diagram

    If the class diagram is the blueprint, the Sequence Diagram is the movie. It shows the step-by-step movement of a request:

    1. The user sends a request to the server.
    2. The FrontController intercepts the request first.
    3. It asks the Dispatcher to find the correct view.
    4. The Dispatcher returns the view, and the controller displays it to the user.

    3. Why Use Diagrams?

    For beginners, diagrams help bridge the gap between abstract code and real-world logic. Seeing the flow of information helps you visualize where to place your security checks, logging, and data processing without making your code messy.

    Explore More Java Content

    Ready for more? Check out these other tutorials from the Ram N Java channel:



    Click the below Image to Enlarge
    Front Controller Design Pattern - Class and Sequence Diagram
























    Front Controller Design Pattern - Introduction

    🚀 Become a Pro Developer!

    Subscribe to Ram N Java for simple, powerful coding tutorials that make learning fun.

    CLICK TO SUBSCRIBE

    Why You Need the Front Controller Pattern

    When building web applications, especially in Java, managing multiple pages and requests can quickly become a mess. The Front Controller Design Pattern is the professional solution to this problem. It provides a single entry point for all incoming requests, acting as a "gatekeeper" for your entire application.

    The Single Entry Point Concept

    Imagine a large building with fifty different doors. If you wanted to check everyone's security badge, you'd need fifty guards! That's how applications work without this pattern. With the Front Controller, you have one main door. One guard handles security, logging, and directions for everyone. This makes your code much cleaner and easier to manage.

    Key Components

    • Front Controller: The initial handler that receives every request.
    • Dispatcher: The component that knows exactly which view or data to send back to the user.
    • View: The actual page the user sees at the end of the process.

    Why Beginners Should Use It

    As a beginner, using this pattern helps you learn Separation of Concerns. By keeping your request logic separate from your page content, you can change your security rules or site-wide settings in just one file instead of updating every single page on your website.

    More from Ram N Java

    Check out these other videos to continue your learning journey:



    Click the below Image to Enlarge
    Front Controller Design Pattern - Introduction

    Saturday, 24 August 2013

    Session State Design pattern


    Click here to watch in Youtube : https://www.youtube.com/watch?v=js-OlYuXRi4

    Click the below Image to Enlarge
    Session State  Design pattern















    See also:

  • All Design Patterns Links
  • Base Design Pattern


    Click here to watch in Youtube : https://www.youtube.com/watch?v=73iG2MSAY30

    Click the below Image to Enlarge
    Base Design Pattern














    See also:

  • All Design Patterns Links
  • Offline Concurrency Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=9UBuUDL76fI

    Click the below Image to Enlarge
    Offline Concurrency Patterns














    See also:

  • All Design Patterns Links
  • Distribution Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=cDgB46gx9V4

    Click the below Image to Enlarge
    Distribution Patterns














    See also:

  • All Design Patterns Links
  • Object-Relational Metadata Mapping Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=T7Dqys234X8

    Click the below Image to Enlarge
    Object-Relational Metadata Mapping Patterns















    See also:

  • All Design Patterns Links
  • Object-Relational Structural Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=icwcqXz7t3o

    Click the below Image to Enlarge
    Object-Relational Structural  Patterns














    See also:

  • All Design Patterns Links
  • Object-Relational Behavioral Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=7XOkyPNg4IU

    Click the below Image to Enlarge
    Object-Relational Behavioral Patterns














    See also:

  • All Design Patterns Links
  • Data Source Architectural Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=nUS7GdFcss4

    Click the below Image to Enlarge
    Data Source Architectural  Patterns















    See also:

  • All Design Patterns Links
  • Domain Logic Patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=R4vJK2zvUHc

    Click the below Image to Enlarge
    Domain Logic Patterns














    See also:

  • All Design Patterns Links
  • Behavioral design patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=kiTDR0YoIqA

    Click the below Image to Enlarge
    Behavioral design patterns

















    See also:

  • All Design Patterns Links
  • Structural design patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=CJN3D4BfKCk

    Click the below Image to Enlarge
    Structural design patterns

















    See also:

  • All Design Patterns Links
  • Creational Design patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=bTQ3owJOMFM

    Click the below Image to Enlarge
    Creational Design patterns

    Design patterns - catalog


    Click here to watch in Youtube : https://www.youtube.com/watch?v=JjwazgrE2LY

    Click the below Image to Enlarge
    Design patterns - catalog

















    See also:

  • All Design Patterns Links
  • Design patterns


    Click here to watch in Youtube : https://www.youtube.com/watch?v=EYOKqb2Mf7k

    Click the below Image to Enlarge
    Design patterns





    See also:

  • All Design Patterns Links
  • Messaging Design Pattern(MDP) - Implementation of Webservice


    Click here to watch in Youtube : https://www.youtube.com/watch?v=UdpAI8VBKYs

    Click the below Image to Enlarge
    Messaging Design Pattern(MDP) - Implementation of Webservice














    See also:

  • All Design Patterns Links
  • Tutorials