Java Design Pattern: Command

Command design pattern takes an operation and its arguments and wraps them up in an object to be executed, logged, etc. In the example below, Command is an operation, its argument is a Computer, and they are wrapped in Switch. In another perspective, Command pattern has 4 parts: command, receiver, invoker and client. In this … Read more

Java Design Pattern: Composite

Composite pattern is relatively simple, but it has been used in many designs, such as SWT, eclipse workspace, etc. It basically produce a hierarchical tree which can be accessed by using a uniform method. Class Diagram The following code implements the following tree structure. Java Code import java.util.List; import java.util.ArrayList;   //Component interface Component { … Read more

Java Design Pattern: Abstract Factory

Abstract Factory pattern adds another layer of abstraction for Factory pattern. If we compare Abstract Factory with Factory, it is pretty obvious that a new layer of abstraction is added. Abstract Factory is a super-factory which creates other factories. We can call it “Factory of factories”. Abstract Factory class diagram Abstract Factory Java code interface … Read more

Java Design Pattern: Factory

1. The story for Factory pattern Factory design pattern is used for creating an object based on different parameters. The example below is about creating human in a factory. If we ask the factory for a boy, the factory will produce a boy; if we ask for a girl, the factory will produce a girl. … Read more

Eclipse Design Patterns – Strategy in SWT

Design patterns used in SWT is relatively straightforward. SWT is an independent module in Eclipse platform. In brief, Strategy let client dynamically set the strategy it should use. Strategy is different from State. First of all, Strategy is simpler since it is only about using interface instead of concrete class. Secondly, State involves changing the … Read more

Eclipse Design Patterns – Composite in Workspace

Composite in Workspace Composite pattern defines a tree hierarchy which lets clients treat objects in the hierarchy uniformly. In Eclipse Workspace, IWorkspace is the root interface and it is a Composite of IContainers and IFiles. Here is the interface hierarchy diagram. Code Example Here is an example to show how to get projects under Workspace. … Read more

Eclipse Design Patterns – Proxy and Bridge in Workspace

1. Proxy and Bridge pattern in Core Workspace The most important design patterns used in Core Workspace is called “Proxy and Bridge”. The most confusing question is about which part is proxy or which part is bridge. The following diagram use IResource for demonstration, others are similar such as IFile, IFolder, IProject, IWorkspaceRoot, etc. In … Read more

Share an Eclipse Project to GitHub in 2 Steps?

If you have a project in your eclipse workspace and you want to share it on GitHub, how to do it? Very simple! Assuming you already registered a GitHub account and you have already installed git on your computer, you can share your eclipse projects to GitHub in 2 easy steps. Note: The following approach … Read more

Longest Substring with At Most K Distinct Characters

This is a problem asked by Google. Given a string, find the longest substring that contains only two unique characters. For example, given “abcbbbbcccbdddadacb”, the longest substring that contains 2 unique character is “bcbbbbcccb”. 1. Longest Substring Which Contains 2 Unique Characters In this solution, a hashmap is used to track the unique elements in … Read more

LeetCode – Palindrome Number (Java)

Determine whether an integer is a palindrome. Do this without extra space. Thoughts Problems related with numbers are frequently solved by / and %. No need of extra space is required. This problem is similar with the Reverse Integer problem. Note: no extra space here means do not convert the integer to string, since string … Read more

LeetCode – Longest Substring Without Repeating Characters (Java)

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. Analysis The basic idea to solve this problem is using an extra data … Read more

LeetCode – Permutations II (Java)

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Java Solution 1 Based on Permutation, we can add a set to track if an element is duplicate and no need to swap. public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> … Read more