Posts

Showing posts with the label Example

Decorator Design Pattern

Image
Structural Design Pattern Summary Decorator design pattern is used to extend or decorate certain objects of a Class (not all objects) statically or at run time. Multiple decorators can act on an object to add the desired behavior to a particular object instance without effecting other instances. Decorator design pattern is a Structural design pattern, it is alternative to inheritance/ Sub classing in java. Java sub classing effects all the instances of the class, and some times it is not desirable. Difference between Decorator and Visitor Decorator design pattern works on a single object. Decorator design pattern helps in enhancing a single object with new functionality or behaviour. Decorator is a Structural design pattern, Visitor is a behavioral design pattern. Visitor works on composite objects. Visitor pattern is used to add a new algorithm or functionality to collection of objects or hierarchy of objects. Details Following are the Participants in this pat...

Masking Credit Card number in Java

Sometimes we need to mask crucial information like Credit Card Numbers, CVV numbers etc before storing  or logging the information. This example mask Credit Card Number (Except last 4 Digit) from a Text which contains information along with Credit Card Number. The following example demonstrates how we can easily mask the credit card with Matcher and Pattern Classes. This Sample Code uses Matcher and Pattern. Pattern Used in this sample is not optimized for Credit Card Numbers, this pattern will get any numerical numbers in the String Content.  Based on the Credit Card Type a more efficient and Strict RegEx can be used to mask the Credit Card. /**Mask the Credit card number but last four digit value **/   Pattern PATTERN = Pattern.compile( "[0-9]+" ); String message = content; Matcher matcher = PATTERN.matcher(message); String maskingChar = "*"; StringBuilder finalMask = new StringBuilder(maskingChar...