Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C#

Implementing the factory pattern using attributes and activation

Rate me:
Please Sign up or sign in to vote.
4.85/5 (37 votes)
9 Mar 20036 min read 93.7K   302   87  
This article shows you how to implement the Factory pattern using attributes and activation.
using System;
using ResponseFactory;

namespace HelloWorld
{

	[UserAgent("Mozilla", false)]
	public class HTMLResponseFormatter : ResponseFormatter
	{
		public HTMLResponseFormatter() {}
		public override string FormatResponse(string Text)
		{
			System.Text.StringBuilder Response = new System.Text.StringBuilder();

			Response.Append("<html><body>");
			Response.Append(Text);
			Response.Append("</body></html>");

			return Response.ToString();
		}
	}

	[UserAgent("XML", false)]
	public class XMLResponseFormatter : ResponseFormatter
	{
		public XMLResponseFormatter() {}
		public override string FormatResponse(string Text)
		{
			System.Text.StringBuilder Response = new System.Text.StringBuilder();

			Response.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			Response.Append("<response>");
			Response.Append(Text);
			Response.Append("</response>");

			return Response.ToString();
		}
	}

	[UserAgent("", true)]
	public class TextResponseFormatter : ResponseFormatter
	{
		public TextResponseFormatter() {}
		public override string FormatResponse(string Text)
		{
			return Text;
		}
	}

	[UserAgent("Nokia", false)]
	[UserAgent("Ericsson", false)]
	[UserAgent("WML", false)]
	public class WMLResponseFormatter : ResponseFormatter
	{
		public override string FormatResponse(string Text)
		{
			System.Text.StringBuilder Response = new System.Text.StringBuilder();

			Response.Append("<?xml version=\"1.0\"?>");
			Response.Append("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
			Response.Append("<wml>");
			Response.Append("<card id=\"response\" title=\"response\">");

			Response.Append(Text);

			Response.Append("</card>");
			Response.Append("</wml>");

			return Response.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Kodkultur AB
Sweden Sweden
A Pretty nice guy Smile | :)

Comments and Discussions