When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Monday, September 18, 2000

Using Object-Orientation in ASP.NET : Encapsulation
By Ian Stallings


Encapsulation (Information Hiding). A principle, used when developing an overall program structure, that each component of a program should encapsulate or hide a single design decision... The interface to each module is defined in such a way as to reveal as little as possible about its inner workings. [Oxford, 1986]
- Peter Coad and Edward Yourdon. Object-Oriented Analysis, 2nd ed. Englewood Cliffs, NJ. Prentice Hall

- continued -

This article attempts to explain the concept of Encapsulation in a OO environment. It assumes you have some understanding of OO (if you've not yet read my earlier article, Using Object-Orientation in ASP.NET : Overview, you might wish to do so before continuing with this article). For more information on OO you can also visit the OO FAQ at http://www.cyberdyne-object-sys.com/oofaq2/.

What is Encapsulation?
Encapsulation is the ability to hide the internal workings of an object's behavior and its data. For instance, let's say you have a object named Car and this object has a method (another word for behavior) named start(). When you create an instance of a car object and call its start() method you are not worried about what happens to accomplish this, you just want to make sure the state of the car is changed to 'running' afterwards. This kind of behavior hiding is encapsulation and it makes programming much easier. When you want your car object to be in a 'running' state, instead of calling: fuel.on(), starter.on(), etc., you just call start(). This not only makes it easier to work with, but if the internal workings of this start() method have to change, the results will be the same.

Encapsulation goes hand in hand with placing your code into a component and using this component when needed. By placing the logic we need for similar processes into a component, we eliminate the need to rewrite that code throughout an application every time the process has to be modified. When we need to make a change to that process, we simply modify that component and the change is propagated throughout the application wherever you used the component.

How Do We Apply This?
Ok, so now we have a general idea of what encapsulation means. But how do we apply this? With code of course.

For this article I have decided to use C#/ASP.NET. This is primarily because of ASP.NET's ability to use Object-Oriented languages like C#. C# is very Java-like so it's pretty easy to pick up by someone like myself, who has some experience with Java. If you have no experience with Java, C#, or C++, don't worry, I tried to write the code examples as simple as possible, so hopefully you can pick it up quickly. To try and avoid confusion I haven't tapped into the full power of C#, sacrificing efficiency for simplicity. So don't take the example as the end all be all in coding. Simply recognize the use of encapsulation and our mission is accomplished. (To help learn C#, be sure to read the articles in the C# Article Index!)

Here's our fictional Foo class below. It has a private String variable named _phoneNumber, which we can set via the method setPhoneNumber(). We have no idea what is going on behind the scenes when we call this method. It could be parsing the string data we entered and validating it. It could be verifying it as correct in a Database. It doesn't matter because the internal logic for setting the phone number is hidden from the developer. They simply create an object instance of this foo class and call the method setPhoneNumber(), passing in a phone number as an argument. Example Foo class below:

namespace KungFoo {

using System;
using System.Text;

  public class Foo {

    private String _phoneNumber;

    public Foo(){
      _phoneNumber = "703-567-8860";
    }

    public void setPhoneNumber(String strPhoneNumber){
      _phoneNumber = strPhoneNumber;
    }

    public String getPhoneNumber(){
      StringBuilder sb = new StringBuilder("Phone Number:");		
      sb.Append(_phoneNumber);
      return sb.ToString();
    }
  }
}

Notice that we could have made a public property named .phoneNumber, but we chose not to. Instead we use a method to alter the state of the object. Why you ask? What if instead of just setting a phoneNumber property, we now have to also change a property named .hasPhone to TRUE before the .phoneNumber property can be set. Everywhere this code was called from would have to be changed. But we control the properties (state) by using our methods (behaviors). Behaviors should change/control state of the object. This is good encapsulation.

In Part 2 we'll look at using the Foo class through an ASP.NET page. By seeing how we'd use this class, we can see what benefits encapsulation brings to the table!

  • Read Part 2


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • 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
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES