developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
PDA Phones & Cases
Shop Online
Laptops
Promotional Golf
Desktop Computers
Auto Insurance Quote
Server Racks
Web Hosting Directory
Cell Phones
Baby Photo Contest
Shop
Televisions
Computer Deals
Logo Design

 
Biz Resources
Data Integration Software
Web Hosting
Email Solutions


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Design Pattern: Proxy
By Jeff Langr

Go to page: 1  2  3  4  Next  

A proxy is a stand-in for something or someone else. As an actor, you might hire a proxy to attend an autograph signing session. Your proxy is providing a layer between you and your fans, but can forward relevant messages as necessary. You want your proxy to behave as much like you do as possible, so that the fans believe they are interacting with you directly.

Proxies in software are similar to proxies in real life. You might create a distributed object proxy. Such a proxy is designed to make its clients think that they are directly interacting with the object, when in reality the object lives in a process on a remote machine. The proxy manages the intricacies of communicating with the distributed object while ensuring that the client remains blissfully ignorant of these details.

As you might imagine, proxies in Java are heavily dependent upon having both the proxy and the target class—the class that the proxy is "standing in" for—implement a common interface. The proxy needs to respond to all the same messages as the target, and forward them as appropriate.

In this article, I'll demonstrate use of a proxy that provides a security layer, disallowing clients with insufficient access from executing specific methods.

I've built a Portfolio class using test-driven development (TDD). The simple implementation of Portfolio provides two query methods, numberOfHoldings and sharesOf, and one update method named purchase.

Listing 1: PortfolioTest

import static org.junit.Assert.*;
import org.junit.*;

public class PortfolioTest {
   private Portfolio portfolio;

   @Before
   public void initialize() {
      portfolio = new Portfolio();
   }

   @Test
   public void containsNoHoldingsOnCreation() {
      assertEquals(0, portfolio.numberOfHoldings());
   }

   @Test
   public void storesSinglePurchaseSingleShare() {
      assertEquals(0, portfolio.sharesOf("MSFT"));
      portfolio.purchase("MSFT", 1);
      assertEquals(1, portfolio.numberOfHoldings());
      assertEquals(1, portfolio.sharesOf("MSFT"));
   }

   @Test
   public void sumsSharesForMultiplePurchasesSameSymbol() {
      portfolio.purchase("MSFT", 1);
      portfolio.purchase("MSFT", 2);
      assertEquals(1, portfolio.numberOfHoldings());
      assertEquals(3, portfolio.sharesOf("MSFT"));
   }

   @Test
   public void segregatesSharesBySymbol() {
      portfolio.purchase("MSFT", 5);
      portfolio.purchase("IBM", 10);
      assertEquals(2, portfolio.numberOfHoldings());
      assertEquals(5, portfolio.sharesOf("MSFT"));
   }

   @Test(expected=InvalidSymbolException.class)
   public void disallowsPurchaseOfNullSymbol() {
      portfolio.purchase(null, 0);
   }
}

Listing 2: Portfolio

import java.util.*;

public class Portfolio {
   private Map<String,Integer> symbols =
      new HashMap<String,Integer>();

   public int numberOfHoldings() {
      return symbols.size();
   }

   public int sharesOf(String symbol) {
      if (!symbols.containsKey(symbol))
         return 0;
      return symbols.get(symbol);
   }

   public void purchase(String symbol, int shares) {
      if (symbol == null)
         throw new InvalidSymbolException();
      symbols.put(symbol, shares + sharesOf(symbol));
   }
}

The InvalidSymbolException class is simply an empty subclass of RuntimeException.

The first step toward building a proxy is to define a common interface. I execute an Extract Implementer refactoring [Feathers2004], ending up with an interface named Portfolio and an implementation of the Portfolio interface named PortfolioImpl.

Listing 3: Introducing an interface

// Portfolio.java
public interface Portfolio {
   int numberOfHoldings();
   int sharesOf(String symbol);
   void purchase(String symbol, int shares);
}

// PortfolioImpl.java--renamed from Portfolio
import java.util.*;

public class PortfolioImpl implements Portfolio {
   ...

Go to page: 1  2  3  4  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Architecture & Design Archives

Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Whitepaper: XML Processing in Applications--Take the Next Step
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES