Posts

Showing posts with the label programming

Using EnumSet by Example

In previous example I have explained using enum using color constants. Java util collection has provides EnumSet , it is a specialized Set implementation for use with enum types. In this post I have explained usage of EnumSet using an example. A text can have multiple attributes like bold, italic, underlined etc. Given a text can have these properties, defined a sample Text class with bit fields as text properties public class Text { //defining Text properties using int public static final int BOLD = 1<<0; public static final int ITALIC = 1<<1; public static final int UNDERLINED = 1<<2; private int style; public Text( int style){ this .style = style; } public boolean isBold(){ return (BOLD&style)==BOLD; } public boolean isItalic(){ return (ITALIC&style)==ITALIC; } public boolean isUnderlined(){ return (UNDE...

Eclipse PDE: Everything about Editor

Image
Editors are the most important part of eclipse IDE framework. Editor are the primary framework to create and modify resource like files. Every eclipse plug-in or eclipse based products needs to provide custom editor for editing proprietary resources. I am tried to put important parts of Eclipse Editor Framework in this pos. This is the first post from the series "Everything About Editor" Everything about Editor Document Partition Syntax Highlighting Auto-Edit Strategy Code Completion using Content Assist Eclipse divides the concept of a text editor into two parts: the document and the viewer. While the document holds the actual content of the editor, the viewer is responsible for handling the content display. This nice Model-View-Controller (MVC)-style separation allows you to separate code that manipulates the content of the editor from the part that manipulates its display. The Document represents the "model" part of the text editor framework. It c...

Static Class vs. Singleton Class

I have explained two different implementation for making a class to Singleton, means restricting no of instance of a class to only one.You can refer previous posts Singleton Design Pattern by example & Alternative implementation for Singleton Class . Here by Static Class means a class that’s has only public static methods. Two different implementations as per previous post for restricting class instances to only one are making all constructor private referred as  Singleton  class and declaring all public methods as static.  Singleton class has several advantages over Static class pattern 1. A singleton class can extends class and implement interfaces , while Static class can not. 2. A singleton can be instantiated lazily or asynchronously while a static class is generally initialized when it is first loaded. 3.A singleton class can extended and its methods can be overridden. 4. The most important advantage is the singleton can be handled polymorphic ally withou...

State Design Pattern by Example

Image
State pattern falls under the category of Behavioral patterns. State design pattern comes in picture when a system’s behavior is depends on its own state or some other resource’s state. If an implementation is depends on the states, we end up with snarl of conditional statement every where in code. A very neat approach to deal with such situation is to separate out every state’s behavior and use them where ever they are applicable. The state pattern allows an object to alter its behavior when is internal state changes . The object is appeared to change its class. State pattern is a way to implement the same. Class Diagram : As above class diagram shows, System class holds state instance. As state of system changes , all handle request delegated to respective State’s concrete implementation. Example : Have an example of Vending machine. Vending machine has following states No Coin Inserted Coin Inserted Dispensing Empty Following diagram explain behavior...