Sunday, November 28, 2004

Bridge Pattern and Abstract Factory Pattern

Both the bridge pattern and the abstract factory pattern decouple two families of related objects. The following is a list of differences between them:
  • In the bridge pattern, the first family is an abstraction (a class that relies on abstract operations); the second is its implementations. Both can vary independently.
  • In the abstract factory pattern, the first family is multiple families; the second is an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Comparing with the first families. In the bridge pattern, the first family is an abstraction. In the abstract factory pattern, the first family is multiple abstractions (families) in parallel.
  • Comparing with the second families. In the bridge pattern, the second family is used as the implementations or a part of implementations (that can vary) of the first family. In the abstraction factory pattern, the second family is used to create objects from the first family. Normally one object from one abstraction.
  • Bridge pattern is a structural pattern; abstract factory pattern is a creational pattern.

I wish to have better descriptions for their distinctions.

Saturday, November 27, 2004

Bridge Pattern and Strategy Pattern

Bridge pattern and strategy pattern have the following in common.

  • Both decouple types from a part of implementations (the variation part) using the adapter pattern.
  • Both expect variations of implementations.

The differences between them are:

  • Bridge pattern expect variations on types (the abstraction part). Strategy pattern has no abstraction on types.
  • The implementations for the bridge pattern have to consider variations of types; the implementations for the strategy pattern are independent of types. The implementations for the strategy pattern are normally common algorithms or business ruless.
  • Clients decide which type and implementation to use in the bridge pattern; both clients and types can decide which implementation to use in the strategy pattern.
  • Bridge pattern is a structural pattern; Strategy pattern is a behavioral pattern.

Friday, November 26, 2004

Some mandates of the design pattern community

Here are some mandates of the design pattern community.

  • Find what varies and encapsulate it.
  • Favor composition over inheritance.
  • Design to interfaces, not to implementations.

I quoted from "Design Patterns Explained" by Alan Shalloway and James R. Trott.

Sunday, November 14, 2004

Web Parts in ASP.NET 2.0

Web Parts provide you with an entire programming framework that rests on top of the ASP.NET 2.0 Framework. This new framework has powerful features, enabling you to build Web applications that can be customized by Web site users and administrators at runtime.

There are two basic ways to create a Web Part. You can treat any standard ASP.NET control as a Web Part or you can build a custom control that derives from the base WebPart class.

Standard ASP.NET controls, Web User Controls, and even custom controls can all be used as Web Parts. When you treat a standard control as a Web part, the control is represented in the Web Part framework with the GenericWebPart class.

Alternatively, you can build a custom Web Part control by building a new control that derives from the base WebPart class. The advantage of this second method of creating a Web Part is that it provides you with additional options for customizing the appearance and behavior of the Web Part.

The article I like the most in this topic is

Introducing the ASP.NET 2.0 Web Parts Framework

Read the article for more details.

Saturday, November 13, 2004

Data Execution Prevention in XP Service Pack 2

Data execution prevention (DEP) is a set of hardware and software technologies that perform additional checks on memory to help protect against malicious code exploits. In Windows XP SP2, DEP is enforced by both hardware and software.

Hardware-enforced DEP marks all memory locations in a process as non-executable unless the location explicitly contains executable code. DEP helps prevent these attacks by intercepting them and raising an exception.

Software-enforced DEP is designed to mitigate exploits of exception handling mechanisms in Windows. By default, software-enforced DEP only protects limited system binaries, regardless of the hardware-enforced DEP capabilities of the processor.

Some application behaviors are expected to be incompatible with data execution prevention. Applications which perform dynamic code generation (such as Just-In-Time code generation) and that do not explicitly mark generated code with Execute permission might have compatibility issues with data execution prevention. Applications which are not built with SafeSEH must have their exception handlers located in executable memory regions.

Applications that attempt to violate DEP will receive an exception with status code STATUS_ACCESS_VIOLATION (0xC0000005). If an application requires executable memory, it must explicitly set this attribute on the appropriate memory by specifying PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_WRITECOPY in the memory protection argument of the Virtual* memory allocation functions. Heap allocations using the malloc() and HeapAlloc() functions are non-executable.

Check the following page for detail.

Changes to Functionality in Microsoft Windows XP Service Pack 2

Aborting a TcpListener listening thread

If you develop an application using TcpListenner. You normally launch a new thread (listenThread) to listen incoming sockets. So you use TcpListener.AcceptTcpClient inside a loop. Then you might call listenThread.Abort to abort the listenning thread if you need to abort the thread.

The problem with this approach is that listenThread.Abort doesn't throw a ThreadAbortException whileTcpListener.AcceptTcpClient is blocking. I know two solutions.

One is to set the listening thread as a background thread. This only works if you only abort the listening thread while you abort the app.

The other one is to keep a global TcpListener object and call TcpListener.Stop when you want to abort the thread. A SocketException with ErrorCode 10004 will be thrown. The app breaks the loop when encountering this error code.

Any better solutions?

Wednesday, November 10, 2004

Derived TcpClient classes used on the server side

The TcpClient class is built on top of the System.Net.Sockets.Socket class and takes care of the details of transferring data. It doesn't cover all the features of the Socket class. For example, RemoteEndPoint (used to identify a remote socket).

To access features of the Socket class not exposed by the protocol classes, you must use the underlying Socket class. TcpClient uses the Client property to identify the underlying Socket. However this property is protected. So you need to derive a class from TcpClient to access the Client property. On the other hand, TcpListener.AcceptClient returns a TcpClient object. So the derived TcpClient class can not be used on the server side directly. The following code shows you how to get around this.

public class TcpTalker : TcpClient
private RemoteEndPoint remotePoint = null;
public TcpTalker(Socket socket)
this.Client = socket;
remotePoint = this.Client.RemoteEndPoint;
// use the derived class on the server side
// tcpl is a TcpListener object
TcpTalker tlk = new TcpTalker(tcpl.AcceptSocket());

Tuesday, November 09, 2004

Visual Studio 2005 Team System

Noah Coad gave a talk on VS 2005 Team System at the user group meeting of the .NET Developer Association yesterday. Visual Studio 2005 Team System provides tools to support the entire software development team. Here is apartial list that it provides:
· Project management
· Analysis and design
· Static analysis tools that detect defects in code by inspecting thesource or binaries without running the code.
· Unit testing integration
· Profiler support
· Testing tools
· Work item tracking
· Source code control

Well, too many cool things, too little time to learn.

Monday, November 08, 2004

BackgroundWorker Component in Windows Forms 2.0

In Windows Forms applications, the "UI Thread Only" rule states that developers should avoid writing code that directly manipulates controls on the UI thread from a worker thread except using Control.XXXInvoke. In .NET framework 1.x, writing code dealing with the communication between the UI thread and a worker thread requires a fair degree of work.

In .NET framework 2.0, Microsoft will introduce a new component named BackgroundWorker to save time for developers to write this communication code.

Below is a sample I created to show you how easy to use BackgroundWorker. I created a Windows Forms application with VS 2005 beta, drag and drop two buttons, a BackgroundWorker, and a ProcessBar control on the Window.

namespace TestBackgroundWorker
partial class Form1 : Form
public Form1()
progressBar1.Maximum = 100;
private void button1_Click(object sender, EventArgs e)
progressBar1.Value = 0;
private void button2_Click(object sender, EventArgs e)

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
if (backgroundWorker1.CancellationPending)
e.Cancel = true;

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
progressBar1.Value = e.ProgressPercentage;
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
progressBar1.Value = 80;
progressBar1.Value = 100;

I learnt the BackgroundWorker component from Michael Weinhardt's article

Safe, Even Simpler Multithreading in Windows Forms 2.0

The link seems to be broken now.

Sunday, November 07, 2004

Alan Shalloway's Rules

Class designing is limited to 5 mins. If no consensus then the simpler one wins.

Architecture designing is limited to 1 hr. If no decision then the simpler one wins.

Thank George A. Smith for his notes.

Saturday, November 06, 2004

A XP Developer's View Point

Arlo Belshee answered a lot of questions regarding XP practices. Read the detail from the following page.

Responses to XP related questions

I got this link from Chris Sells' blog a long time ago. I think that it might be good to pair it with Steve McConnell's comment.

Steve McConnell's Comment

According to Charlie Poole, Steve McConnell made the following comment.

The biggest mistake we made about design during 90's was to design everything up front. The biggest mistake we made about design during 2000 is to design nothing.

Friday, November 05, 2004

Three ways to consume Amazon Web services in .NET

There are three ways to consume Amazon Web services in .NET.

1. Using System.Net.WebRequest to get data and System.Xml.XmlTextReader to parse data. This way is recommended by
2. Using System.Xml.XmlTextReader to get and parse data. I prefer this way. The underlying might be the same as the first way.
3. Getting the WSDL file and generating a proxy. This is the way that Microsoft recommends to consume a Web service.

XP and design

The topic of the Seattle XP Users Group meeting in this month was XP and design. WardCunningham, Charlie Poole, Alan Shalloway and George Smith chaired the discussion.

Nobody seems to believe that XP avoids design. All the XP people say they do it all the time.

Some issues regarding micro design were discussed.

Thursday, November 04, 2004

How to get around "right click disabled" on some websites?

On some websites, you find an interesting image and try to save it. You right click the image and a pop up window is launched to tell you that you are not able to save the image. You can get around this following the following steps.
· Right click on the image, a pop up window is displayed, and keep pressing (holding) the right button of the mouse.
· Move the mouse to OK button of the pop up window while keep pressing (holding) the right button of the mouse. Then press the left button of themouse while keep pressing (holding) the right button of the mouse. (So the left button and the right button of the mouse are pressed at the sametime.)
· Release the left button, the pop up window disappears.
· Now move the mouse to the image and then release the right button.The context menu will be displayed. You can save the image.

Consuming Web Services Efficiently

I wrote a review article on various options provided by the Microsoft .NETFramework for making Web service calls and focus on making asynchronous Webservice calls to improve performance. Here is the link.

The article was written in last September. It's outdated now considering the rapid progress in the area of Web services. I'm going to revisit the area and use it to warm up.

Wednesday, November 03, 2004

Amazon Simple Queue Service (Beta)

Jeff Barr gave a talk about Amazon Web Services at .NET user group. AWE is cool, but I heared his talk before. The news to me about AWE is Amazon Simple Queue Service (Beta) that was released today.

The Amazon Simple Queue Service offers a reliable, highly scalable hosted queue for buffering messages between distributed application components. Registered developers have free access to the Simple Queue Service during the Beta, but storage is limited to 4,000 queue entries per developer. The maximal size for each entry is 1 MB.

Using the Amazon Simple Queue Service, you can decouple components of your application so that they run independently, with the Queue Service easing messaging management between components. Any component of a distributed application can store any type of data in a reliable queue at Any other component or application can then later retrieve the data, using queue semantics.

It seems to me that the Queue can be used to store semi-secret data.

Tuesday, November 02, 2004

How to create a 1-bit-per-pixel image from a true-color image in .NET

In .NET, the following code

Bitmap bmp = new Bitmap (160, 64);

creates a Bitmap object with the default pixel format Format32bppARGB. You can create a Bitmap object with a different pixel format as follows.

Bitmap bmp = new Bitmap (160, 64, PixelFormat.Format1bppIndexed);

After you have a Bitmap instance, you might want to call Graphics.FromImage to get a Graphics object so that you can draw on the Bitmap instance. According to SDK, this method throws an exception if the image has any ofthe following pixel formats.
· PixelFormat.Format1bppIndexed
· PixelFormat.Format4bppIndexed
· PixelFormat.Format8bppIndexed
· PixelFormat.Undefined
· PixelFormat.DontCare
· PixelFormat.Format16bppArgb1555
· PixelFormat.Format16bppGrayScale

So your best bet to get a Graphics object and have the minimal file size will be a 24-bit-per-pixel bitmap. That might not fulfill your requirements in term of the file size.

Here is one way I recommend. You first create a Bitmap object with the default pixel format, get a Graphics object, and draw the image as you need. Then you create a new Bitmap object with your desired pixel format, use Bitmap.LockBits to get an entry to the bitmap bytes, set pixels on the second bitmap according to the corresponding pixels on the first bitmap, and call Bitmap.UnlockBits to release the lock.

The following link shows how to create a 1-bit-per-pixel image from atrue-color image in .NET.

You will need to lock the image data so that the GC doesn't move it around during a collection (and while you are moving around it).

I learnt this way from Fabian Schmied and Chris Anderson (Merak).

Some Basic Facts about GIFs

I wrote code for creating images, drawing waveforms on them, and saving them in different formats for a few years. But I didn't know some basic facts until recently. Here are some basic facts.

Why GIFs are smaller compared with bitmaps?

It's both a compression thing and a color thing.

The GIF format uses compression, the BMP format (with rare exceptions) doesn't. So GIF is smaller because the image is compressed.

GIF only supports 8 bits per pixel. BMP normally uses 32 bits per pixel. So that's another reason GIFs are often smaller.

GIFs are often referred to as a lossless format. This is only really true if your original image was an 8bpp image. The only genuinely lossless formats widely used are PNG and TIFF.

An image in GIF format includes the headers, lookup table, and the imagedata in compressed form. An image in BMP format includes headers, imagedata, palette (if present), etc.

I learnt those basic facts from Chris Tavares, Chris Anderson (Merak), and Ian Griffiths.

Windows XP Service Pack 2

I just installed Windows XP Service Pack 2. It adds the feature of blocking pop-ups. Cool!