Friday, October 8, 2010

Java සමග Software Design Patterns (4 කොටස | Proxy Pattern)

බොහෝ පරිගණක වැඩසටහන් සැලසුම් කිරීමේදී අපට පද්ධතියෙන් පරිභාහිර සම්පත්(resources)  අප සැලසුම් කරන වැඩසටහන තුල භාවිතයට ගැනීමට සිදුවේ. උදාහරණ වශයෙන් ජාල සම්බන්දතා(network connections), files, පරිගණකයේ සසම්භාවී මතකය තුල පවතින විශාල වස්තුවක්(a large object in memory) හෝ සම්පත් අපතේ යෑම නිසා duplicate  කල නොහැකි අනෙකුත් ඕනෑම resource එකක් සැලකිය හැකියි.

මෙවැනි අවස්ථාවලදී අප සකස්කරන සැලසුම අසාර්ථක එකක් වුවහොත් එය කෙලවර වන්නේ අධික ලෙස bandwidth එක භාවිතා කරන හෝ පරිගණක මතකය අපතේ යවන හෝ කාර්‍යක්ෂමතාව අතින් ඉතාම අඩු මට්ටමක පරිගණක වැඩසටහනකින්. ප්‍රොක්සි (proxy design pattern) යනු මෙවැනි අවස්ථාවන්හිදී සාර්ථකව apply කල හැකි design pattern එකකි.



මෙහිදී සිදුකරනුයේ අදාල resource එක ප්‍රවේශය කිරීම(access) සඳහා interface එකක් සැකසීමයි. පහත ඉදිරිපත් කර ඇති class diagram එක මගින් මෙහි ක්‍රියාකාරීත්වය වටහාගැනීම පහසුය.

 

අපි දැන් ඉහත model එක ජාවාතුල ක්‍රියාවට නංවන  අන්දම බලමු.
ProxyTest.java
/**      
*class  : ProxyTest
*Author : Kanishka Dilshan      
*Purpose: Showing how to implement Proxy design pattern 
*Blog   : http://javaxclass.blogspot.com      
*/ 

interface Subject {
 void doAction();
}

class RealSubject implements Subject{
 
 public RealSubject(){
  //init resources
 }
 
 public void doAction(){
  //do something
 }
 
 private void someInternalProcess(){
  /*do some internal process related to 
   *the external resource
   */
 }
}

class Proxy implements Subject{
 private Subject subj=null;
 
 public Proxy(){
  //init initial attributes
 }
 
 public void doAction(){
  //handle the RealSubject
  if(subj==null){
   subj=new RealSubject();
  }
  subj.doAction();
 }
 
}

public class ProxyTest {
 public static void main(String args[]){
  Subject subj1=new Proxy();
  Subject subj2=new Proxy();
  
  subj1.doAction();
  //this initializes the resource
  subj2.doAction();
  //this also initializes the resource
  subj1.doAction();
  //this time it will not initializes the resource
 }
}
ඉහත class diagram එක implement කරන අන්දම වටහා ගැනීමට සහ Proxy හි මූලධර්මය වටහාගැනීමට අදාල ජාවා කේතය ප්‍රමාණවත් වුවද ප්‍රායෝගික වශයෙන් Proxy pattern එක කිසියම් ගැටලුවකට apply කරන අන්දම වටහාගැනීමට අපි පහත උදාහරණය සලකා බලමු.

ගැටලුව:
URL එක දෙනලද(http://sites.google.com/site/ansisliit/Home/MyData.dat) data file එකක් proxy design pattern එක යොදා view කරවීමට අදාල UML design එක සහ එය ජාවා යොදාගෙන implement කරන්න.
පිළිතුර:

RemoteFile.java
/**     
*class  : ProxyExampleTest  
*Author : Kanishka Dilshan     
*Purpose: Showing how to implement Proxy design pattern
*Blog   : http://javaxclass.blogspot.com     
*/ 
public interface RemoteFile {
 void viewFile();
}
RealRemoteFile.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class RealRemoteFile implements RemoteFile {
 private String fileContent;
 private URL url;
 
 public RealRemoteFile(String u){
  try {
   url=new URL(u);
  } catch (MalformedURLException e) {
   System.err.println("Invalid URL!");
  }
  fileContent="";
  
  loadResource();
 }

 @Override
 public void viewFile() {
  System.out.println("Displaying the remote file...\n");
  System.out.println(fileContent);
 }
 
 private void loadResource(){
  System.out.println("Loading Remote Resource");
  try {
   URLConnection conn=url.openConnection();
   conn.setDoOutput(false); //read only
   InputStreamReader isr=new InputStreamReader(conn.getInputStream());
   BufferedReader br=new BufferedReader(isr);
   String tempLine;
   while((tempLine=br.readLine())!= null){
    fileContent+=tempLine+"\n";
   }
  } catch (IOException e) {
   System.err.println("Cannot open the location "+ e.getMessage());
  }
 }
}
ProxyRemoteFile.java
public class ProxyRemoteFile implements RemoteFile{
 RemoteFile rmtFile=null;
 String rmtFilePath;
 
 public ProxyRemoteFile(String url){
  rmtFilePath=url;
 }

 @Override
 public void viewFile() {
  long st1,st2;
  st1=System.nanoTime();
  if(rmtFile==null){
   rmtFile=new RealRemoteFile(rmtFilePath);
  }
  rmtFile.viewFile();
  st2=System.nanoTime();
  long timeDiff=st2-st1;
  double timeAmt=timeDiff*10e-12;
  System.out.println("Time taken to view : "+ timeAmt+ " s");  
 }
}
ProxyExampleTest.java
public class ProxyExampleTest {
 public static void main(String args[]){
  String location="http://sites.google.com/site/ansisliit/Home/MyData.dat";
  RemoteFile rFile=new ProxyRemoteFile(location);
  rFile.viewFile();
  //view same file again
  System.out.println("\nView same file again\n");
  rFile.viewFile();
 }
}
output:

පැහැදිලි කිරීම;
මුල් වතාවේ viewFile() method එක call කල විට එය view වීම සඳහා තත්පර 0.0387 ක් ගතවී ඇති බවත් නැවත වතාවක් එකම RemoteFile object එක සඳහා එම viewFile() method එක call කල විට ගතවී ඇත්තේ ඉතාමත්ම සුළු කාලයක් එනම් තත්පර 1.3641*10-6 ක් බවත් ඔබට පෙනෙනවා ඇති.


අප විසින්  සාර්ථකව Proxy design pattern එක මෙම ගැටලුවට ආදේශ කල නිසා එකම resource එක නැවත නැවතත් ප්‍රවේශනය වීම වැළකී තිබේ.
ඔබට අවශ්‍ය නම් මේ සඳහා අදාල java කේත eclipse project එකක් ලෙස භාගත කරගත හැක.
(9.52 KB)

10 comments:

  1. හොඳ වැඩක්, ස්තූතියි.

    ReplyDelete
  2. This is really really good for programmers and students, there is no way to learn software patterns in Sinhala

    ReplyDelete
  3. Maxxaaa......kisima aduwak naaaa....

    ReplyDelete
  4. This is a great work keep it up !

    As a suggestion try to include links for related tutorials written in English for same subject area . It will increase the value of your posts because readers can do further studies ...

    ReplyDelete
  5. u r doing a great job here by providing all these complex things in simple sinhala as we could understand easily.....thanks a lot and may triple gem bless u!

    i got to mention one thing, this blog seems little complex when finding a article or viewing it, actually its a real mess.plz don't get angry im just telling what i felt...its better if you could arrange this blogs and all those widgets in very organize manner...it will provide better look to ur valuble articles.

    ReplyDelete
  6. Thank you very much for showing my weakness points. I'll try my best to rearrange those widgets.
    Thanks again!!!
    May the triple gem bless you!!!

    ReplyDelete
  7. Great work...

    thanks a lot...

    jee padam poddak karanna ko plssss....

    ReplyDelete