design pattern in java code example

Example 1: java builder pattern example

public class BankAccount {
    public static class Builder {
        private long accountNumber; //This is important, so we'll pass it to the constructor.
        private String owner;
        private String branch;
        private double balance;
        private double interestRate;
        public Builder(long accountNumber) {
            this.accountNumber = accountNumber;
        }
        public Builder withOwner(String owner){
            this.owner = owner;
            return this;  //By returning the builder each time, we can create a fluent interface.
        }
        public Builder atBranch(String branch){
            this.branch = branch;
            return this;
        }
        public Builder openingBalance(double balance){
            this.balance = balance;
            return this;
        }
        public Builder atRate(double interestRate){
            this.interestRate = interestRate;
            return this;
        }
        public BankAccount build(){
            //Here we create the actual bank account object, which is always in a fully initialised state when it's returned.
            BankAccount account = new BankAccount();  //Since the builder is in the BankAccount class, we can invoke its private constructor.
            account.accountNumber = this.accountNumber;
            account.owner = this.owner;
            account.branch = this.branch;
            account.balance = this.balance;
            account.interestRate = this.interestRate;
            return account;
        }
    }
    //Fields omitted for brevity.
    private BankAccount() {
        //Constructor is now private.
    }
    //Getters and setters omitted for brevity.
}

BankAccount account = new BankAccount.Builder(1234L)
            .withOwner("Marge")
            .atBranch("Springfield")
            .openingBalance(100)
            .atRate(2.5)
            .build();
BankAccount anotherAccount = new BankAccount.Builder(4567L)
            .withOwner("Homer")
            .atBranch("Springfield")
            .openingBalance(100)
            .atRate(2.5)
            .build();

Example 2: java design patterns

// double locking is used to reduce the overhead of the synchronized method
public static ThreadSafeSingleton getInstanceDoubleLocking() {
	if (instance == null) {
		synchronized (ThreadSafeSingleton.class) {
			if (instance == null) {
				instance = new ThreadSafeSingleton();
			}
		}
	}
	return instance;
}

Example 3: pyramid pattern in java

// Java implementation to print the 
// following pyramid pattern 
public class Pyramid_Pattern { 
  
    // function to print the following pyramid 
    // pattern 
    static void printPattern(int n) 
    { 
        int j, k = 0; 
  
        // loop to decide the row number 
        for (int i = 1; i <= n; i++) { 
              
            // if row number is odd 
            if (i % 2 != 0) { 
              
                // print numbers with the '*'  
                // sign in increasing order 
                for (j = k + 1; j < k + i; j++) 
                    System.out.print(j + "*"); 
                System.out.println(j++); 
  
                // update value of 'k' 
                k = j; 
            } 
  
            // if row number is even 
            else { 
                 
                // update value of 'k' 
                k = k + i - 1; 
  
                // print numbers with the '*' in 
                // decreasing order 
                for (j = k; j > k - i + 1; j--) 
                    System.out.print(j + "*"); 
                System.out.println(j); 
            } 
        } 
    } 
  
    // Driver program to test above 
public static void main(String args[]) 
    { 
        int n = 5; 
        printPattern(n); 
    } 
}

Tags:

Java Example