Posts

Showing posts with the label strategy

Type Inference in Java

As part of Generics , Java language supports type safe programming. It allows a type or method to operate on objects of various types while providing compile-time type safety. The most common use case is while using Java Collection framework. For example if you define a list of string and trying to add integer value to the list, compiler throws error. List list = new ArrayList(); //Compilation Error : he method add(int, String) in the type List is not applicable for the arguments (int) list.add(10); As a developer you may always notice redundancy in defining parameter type information in both the side of declaration. As off Java 1.6, language doesn’t provide any feature to overcome it. Using Static Factory : One possible way to overcome this problem by using static factory methods. If you frequently use typed list or map in application then you can define generic static factory methods which returns you required typed map or list. public static HashMap getMapInstance(){ ...

Eclipse PDE: Everything about Editor Part 4

Auto-Edit Strategy This post of Everything About Editor series explains about editors auto complete feature. Auto edit feature of any editor helps developer while writing code, for example in java editor of eclipse provides auto complete features for string quotes , open braces etc. When you type open curly braces ‘{‘ , eclipse java editor auto inserts closing curly braces considering line and indentation. Here I am going to implement auto-edit strategy for open single , double quotes and opening curly braces ‘{‘. To use auto edits, you must implement the org.eclipse.jface.text.IAutoEditStrategy interface, which contains just one method: void customizeDocumentCommand(IDocument document,DocumentCommand command); This is called each time you make a change to the document with the DocumentCommand object containing the information about that change. So, adding indents or making any change to the text being changed is as simple as modifying the DocumentCommand object. 1. Implement IA...

Strategy Design Pattern by example

This post explains Strategy design pattern using an example. When to Use ? – Its an alternative to Template Design Pattern. Use this pattern when template methods implementation as well as steps in which they need to execute are dynamic. Its like no of steps in algorithm vary or the class that implements the steps needs as independent inheritance hierarchy . How to use ? – Implement an interface defining the individual steps. Define a class that implements this interface ( individual steps) , this class is not forced to inherit from an abstract template super class. Main algorithm implementation class uses this interface implementation for each of algorithm steps execution An example – Same example as Template Method Design pattern is being used, Process order. Interface - package strategypattern; import java.util.List; import templatemethod.Order; public interface ProcessOrderHelper {     public  boolean  validateCustomer(String custName...