What are the design patterns in Java

Concepts: The design pattern of Java

Scope conflicts

When you define a variable with the element in JavaScript, it is set within the function that defined it. If you define a variable without use, it is assigned the global scope. This means that global variables are prone to collision with other scripts on your part.

Let's look at a code example. In the following code, the function and the variables exist in the frame of the page.

Suppose there is a function outside of your script that also changes the global variable. This collision of scripts can produce unexpected results.


  1. The user selects the button twice and increments the variable in.
  2. The function is called that exists in, but also on your website. Let's say that there are 10 elements in it. Now the global variable has a value of 10.
  3. The next time the user selects the button, the variable will return unexpected results.
    • Expected: = 3
    • Current: = 11

You can try to avoid conflicts in your scripts, but there is no guarantee that third-party scripts included on your page will not use similar function and variable names.

Anonymous functions

One solution is to wrap your code in an anonymous function (also called a shutdown) that runs immediately. Your code within a lock is not accessible to other scripts. This is a great way to create private functions and variables.

Here is the syntax for an anonymous function:

  • Line 3: contains an additional set of parentheses that tell JavaScript to run the function immediately after parsing, rather than waiting for other code to call the function.

Locks can be powerful because they provide privacy and status for the life of the application. For the code within the closure, all variables and functions are only located in the closure area. However, your code within the closure can still access global variables or functions.


Although JavaScript has a function known as implied globals, it can make your code difficult to maintain because it is not easy to determine which variables are global. In order to determine whether a variable is global, the interpreter has to go backwards through the range chain and look for a statement that matches by name. If none is found, the variable is assumed to be global.

Pass globals

With the anonymous function, you can explicitly pass global parameters. This is known as importing parameters into your code.


  • Line 1: defines the name of the parameters that are passed to the function. Note that they do not have to match the names on line 3. Here the object is passed to a parameter with the name.
  • Line 3: passes the object to the function.

Since only one object is passed, but there are two parameters, the value of is undefined.

This can be useful when you want to easily check that other variables are defined.

Export globals

You may also want to pass variables and functions outside of your anonymous function. You can do this by using the value.


  • Line 1: assigns our anonymous function. This value can be anything you choose. In this example we are using BCLS (Brightcove Learning Services).

The object is now globally available with two public properties, a variable named and a function named. They can be accessed outside of our anonymous function as:

    Complete examples

    Here are some complete examples of the JavaScript module's design pattern.

    example 1

    This example shows how to create private and public variables and functions using the module pattern.

    • Private variables:,
    • Private functions:,
    • Public variable:
    • Public function:

    Example 2

    In this example global objects are passed on and a public function is made available.

    Code samples

    Some of our code examples use the JavaScript module design pattern, and you can review them for ideas on how to implement that pattern.