Import Pattern is an actively used design pattern. Many (most?) languages allow you to split code into different files and import the code from one file into a different file.

?Years Old
  • Have a question about Import Pattern not answered here? Email me and let me know how I can help.

Languages without Import Pattern include html, json

Languages with Import Pattern include ballerina, c, clojure, csharp, css, dart, elixir, erlang, f-sharp, go, haskell, java, julia, kotlin, matlab, nodejs, objective-c, perl, php, python, r, racket, ruby, rust, swift, typescript, xtext

Example from ballerina:

import ballerina/http;
import ballerina/io;

Example from c:

//  If a header file is included within <>, the preprocessor will search a predetermined directory path to locate the header file. If the header file is enclosed in "", the preprocessor will look for the header file in the same directory as the source file.
#include <stdio.h>
#include "stdio.h"

Example from clojure:

(load "fun")
(load "files/fun")
(load-file "./files/fun.clj")
(defproject project-a
:dependencies [[org.clojure/clojure "1.5.1"]
              [project-b "0.1.0"]])
(require '[clojure.string :as string])
(use '[clojure.string :only [split]])
(import 'java.util.Date)
(require 'clojure.contrib.def 'clojure.contrib.except 'clojure.contrib.sql)
(require '(clojure.contrib def except sql))

Example from csharp:

using static System.Console;
using static System.Math;
class Program
    static void Main()
        WriteLine(Sqrt(3*3 + 4*4));

Example from css:

@import 'custom.css';

Example from dart:

import 'file-system.dart';
import 'dart:math' as math;

Example from elixir:

# Alias the module so it can be called as Bar instead of Foo.Bar
alias Foo.Bar, as: Bar

# Require the module in order to use its macros
require Foo

# Import functions from Foo so they can be called without the `Foo.` prefix
import Foo

# Invokes the custom code defined in Foo as an extension point
use Foo

Example from erlang:


Example from f-sharp:

open module-or-namespace-name
open System.IO
open List
open Seq

Example from go:

import (
import . "fmt"
import _ "io"
import log ""
import m "math"

Example from haskell:

import Data.Maybe
import Mod as Foo
import Mod (x,y, (+++))
import qualified Mod
import Mod hiding (x,y,(+++))
import qualified Mod hiding (x,y)

Example from java:

import javax.swing.*;
import javax.swing.JOptionPane;
// use fully qualified name without import:
javax.swing.JOptionPane.showMessageDialog(null, "Hi");
// There are 166 packages containing 3279 classes and interfaces in Java 5.
// import*; Input-output classes.

Example from julia:

# Files and file names are mostly unrelated to modules; modules are associated only with module expressions.
# One can have multiple files per module, and multiple modules per file:
using MyModule
using MyModule: x, p
import MyModule
import MyModule.x, MyModule.p
import MyModule: x, p
module Foo

Example from kotlin:

import foo.Bar // Bar is now accessible without qualification
import foo.* // everything in 'foo' becomes accessible
import bar.Bar as bBar // bBar stands for 'bar.Bar'

Example from matlab:

import pkg.cls1
import pkg.pkfcn

Example from nodejs:

const jtree = require("jtree")

Example from objective-c:

// #import ensures that a file is only ever included once so that you never have a problem with recursive includes.
#import <Foundation/Foundation.h>
#include <asl.h>
#include <mach/mach.h>

Example from perl:

use Digest::MD5 'md5_hex';

Example from php:

include 'vars.php';
require 'filename'; // FATAL error if fails

Example from python:

import datetime
oTime =
from my_pkg import my_funcs

Example from r:


Example from racket:

(require (prefix-in tcp: racket/tcp))

Example from ruby:

load 'filename.rb'
require 'filename'
require 'trig.rb'

Example from rust:

use ::std::fs;  // Imports from the `std` crate, not the module below.
use self::std::fs as self_fs;  // Imports the module below.
mod my;
use self::foo::Zoo as _;
#[path = ""]
mod c;

Example from swift:

import UIKit
import UIKit.UITableViewController
let tvc = UITableViewController()
let vc = UIViewController()
let label = UILabel()

Example from typescript:

import { ZipCodeValidator } from "./ZipCodeValidator";
/// <reference path="../typings/jquery.d.ts"/>
/// <reference path="components/someclass.ts"/>
import moo = module('moo');
/// <amd-dependency path="legacy/moduleA" name="moduleA"/>

Example from xtext:

import ""
import "" as dmodel

Last updated August 9th, 2020

Edit Import Pattern on GitHub