Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class Design - All methods routed via public then private? 1

Status
Not open for further replies.

TallOne

Programmer
May 14, 2004
164
US
Assume an application always routes db access through a public method and then to private. I'm wondering what benefits this may offer? Below is an example.

Code:
    public class ClsTest
    {
        private static string xMyMethod()
        {
            return "I accessed a database";
        }
        public static string MyMethod()
        {
            return xMyMethod();
        }
    }
 
what you describe is known as the Facade Pattern. It can be applied to any level of OOP (member, object, namespace). the idea is that the details are encapsulated behind a single (few) components.

The code above have no value. MyMethod is just noise. a better example of the facade pattern would be how log4net or an IoC container (castle.windsor or structure map) works.

with log4net the client API is very simple.
Code:
new XmlConfiguration().Configure("file name");
Code:
LogManager.GetLogger("name").Error(exception);
behind these 2 calls a lot is going on.
1. initializing the logs
2. reading the configuration file
3. creating appenders, formatters, filters, etc.

all your code (the client) needs to do is tell the api where to look for the configuration and what to log. log4net manages the rest.

it's similar with IoC containers. first you configure the container to tell it what types to resolve, then you ask it to resolve them for you. your code may look like this
Code:
container = new IocContainer()
   .Regsiter<MyType>()
   .Register<TheDependency>();
Code:
var instance = container.Resolve<MyType>();
you tell the container what you need, the container uses a number of objects and method calls to create an instance of MyType for you.

there are many other design concepts at play, but hopefully this provides some insight into what the facade pattern does.

you can download the source code for these projects to understand how these frameworks utilize the facade pattern for their public API.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks jmeckley! I've been coding using the Facade Pattern and didn't know it. The code I posted is an example of an application I've had to maintain. I have been getting irritated with the "noise" and just wanted to make sure I wasn't missing something before I change anything. Thanks again and have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top