VOOZH about

URL: https://dzone.com/users/361436/prathapgivantha.html

⇱ Prathap Givantha Kalansuriya - DZone Member


Prathap Givantha Kalansuriya

Tech Lead at Home

Colombo, LK

Joined Dec 2008

Stats

Reputation: 192
Pageviews: 480.0K
Articles: 4
Comments: 0

Articles

What Is a JWT Token?
Learn more about the JSON Web Token and how it can securely transmit information.
Updated January 24, 2020
· 55,838 Views · 11 Likes
How to Create a REST API With Spring Boot
Learn how to create REST API with Spring Boot, JPA, Hibernate, and MySQL.
November 27, 2018
· 385,903 Views · 14 Likes
Difference Between Mysql Replace and Insert on Duplicate Key Update
While me and my friend roshan recently working as a support developers at Australia famous e-commerce website. recently roshan as assign a new bug in this site it’s related to the product synchronize process in the ware house product table and the e-commerce site, his main task was check the quickly the site product table and check with ware house product table product if the either insert new data into a site database, or update an existing record on the site database, Of course, doing a lookup to see if the record exists already and then either updating or inserting would be an expensive process (existing items are defined either by a unique key or a primary key). Luckily, MySQL offers two functions to combat this (each with two very different approaches). 1. REPLACE = DELETE+INSERT 2. INSERT ON DUPLICATE KEY UPDATE = UPDATE + INSERT 1 . REPLACE This syntax is the same as the INSERT function. When dealing with a record with a unique or primary key, REPLACE will either do a DELETE and then an INSERT, or just an INSERT if use this this function will cause a record to be removed, and inserted at the end. It will cause the indexing to get broken apart, decreasing the efficiency of the table. If, however REPLACE INTO ds_product SET pID = 3112, catID = 231, uniCost = 232.50, salePrice = 250.23; 2. ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE clause to the INSERT function. This one actively hunts down an existing record in the table which has the same UNIQUE or PRIMARY KEY as the one we’re trying to update. If it finds an existing one, you specify a clause for which column(s) you would like to UPDATE. Otherwise, it will do a normal INSERT. INSERT INTO ds_product SET pID = 3112, catID = 231, uniCost = 232.50, salePrice = 250.23, ON DUPLICATE KEY UPDATE uniCost = 232.50, salePrice = 250.23; This should be helpful when trying to create database queries that add and update information, without having to go through the extra step. Thanks Have a Nice Day
October 3, 2012
· 14,396 Views
Strategy Design Pattern
today i'm going to look at the strategy pattern , it's one of the more important design patterns in the gof pattern. in my previous post i discussed what is a design pattern ? and the basic background related to design patterns. which type of pattern ? it's categorized under behavioral pattern. how gof (gang of four) define it ? define a family of algorithms, encapsulate each one, and make them interchangeable. strategy lets the algorithm vary independently from clients that use it. how is it used in the real world ? to explain the strategy in the real world, let's take the example. e-commerce has been a bit of a buzz word in the world today. trading on the internet is a hugely complex business and there are many aspects to consider when designing a successful e commerce site. in these websites when the customer buys items they are in the processing shopping cart. many payment mechanisms are introduced to the customers to dotheir payment ex: pay pal , visa , mobile payment some time according to the country wise there are payment mechanisms introduced ex: aus b-pay. this real world scenario i'm get as the example :) what is the current problem and how to solve it ? :o in this scenario i need to maintain the different payment mechanisms in the e commerce site. these payment methods are needed to separate each other because they have different process, but they do the same action (make the payment) to give the correct solution. i need to follow mainly 3 design principles. principles ? 1. i need to identify the aspects of my application that vary and separate them from what stays the same. 2. program to an interface, not to an implementation. 3. favor composition over inheritance ok next i go through the each principle how to design according to principle ? 1. first i'm going to identify the three payment types: visa , pay pal and mobile payment. 2. in these payment method, do the same action (process the customer payment) but it's changed on the run time according the customer selection option. for using oop concepts, i'm going use abstract class or an interface to create the opportunity to dynamic behavioral changes with the help of polymorphism. 3. after creating the interface, i'm going to put more than one class together as class variables that is called composition, then encapsulate payment methods into their own set of classes. like ex: payment types visa , pay pal how it can design in the design level ? how to implement it ? here i'll use java code to do my sample implementation. paymentmethod interface public interface paymentmethod { public boolean pay(double amount); } pay pal implementation import java.util.date; public class paypal implements paymentmethod { private final string name; private final string cardnumber; private final date expires; public string getname() { return name; } public string getcardnumber() { return cardnumber; } public date getexpires() { return expires; } public paypal(string name, string cardnumber, date expires) { super(); this.name = name; this.cardnumber = cardnumber; this.expires = expires; } @override public boolean pay(double amount) { return true; // if payment goes through } } visa implementation import java.util.date; public class visa implements paymentmethod { private final string name; private final string cardnumber; private final date expires; public string getname() { return name; } public string getcardnumber() { return cardnumber; } public date getexpires() { return expires; } public visa(string name, string cardnumber, date expires) { super(); this.name = name; this.cardnumber = cardnumber; this.expires = expires; } @override public boolean pay(double amount) { return true; // if payment goes through } } mobilepayment implementation import java.util.date; public class mobilepayment implements paymentmethod { private final string serviceprovider; private final string mobilenumber; private final date expires; public string getserviceprovider() { return serviceprovider; } public string getmobilenumber() { return mobilenumber; } public date getexpires() { return expires; } public mobilepayment(string serviceprovider, string mobilenumber, date expires) { super(); this.serviceprovider = serviceprovider; this.mobilenumber = mobilenumber; this.expires = expires; } @override public boolean pay(double amount) { return true; // if payment goes through } } there i'm implement the item class for the do this item class public class item { private final string code; private final string name; private final double price; public item(string code, string name, double price) { this.code = code; this.name = name; this.price = price; } public string getcode() { return code; } public string getname() { return name; } public double getprice() { return price; } } shoppingcart class import java.util.arraylist; import java.util.list; public class shoppingcart { private final list items; public shoppingcart() { items = new arraylist(); } public void additem(item item) { items.add(item); } public double calctotalcost() { double total = 0.0; for (item item : items) { total += item.getprice(); } return total; } public boolean pay(paymentmethod method) { double totalcost = calctotalcost(); return method.pay(totalcost); } } samplepayprocesstest class import static org.junit.assert.*; import java.util.calendar; import java.util.date; import org.junit.test; public class samplepayprocesstest { @test public void paybillusingvisa() { shoppingcart instance = new shoppingcart(); item a = new item("it001","t-shirt", 750.43); instance.additem(a); item b = new item("it002","hat", 102.99); instance.additem(b); date expirydate = getcardexpireydate(); paymentmethod visa = new visa("captaindebug", "1234234534564567", expirydate); boolean result = instance.pay(visa); asserttrue(result); } private date getcardexpireydate() { calendar cal = calendar.getinstance(); cal.clear(); cal.set(2015, calendar.january, 21); return cal.gettime(); } } conclusion according to the given solution, the behavior of a class should be defined at runtime, and easily maintaining each payment behavior if i need newa payment method i can easily plug, without effecting to others. this is the one of the sample scenarios we can address the strategy pattern in the real world, ok we will meet next day with another gof design pattern. thank you have a nice day :)
September 18, 2012
· 12,723 Views · 4 Likes

User has been successfully modified

Failed to modify user

Let's be friends: