A Flyweight mode without method inheritance

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Namespace Flyweight
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String document = "AAZZBBZBAAZZBBZBAAZZBBZB ";

Char [] chars = document. ToCharArray ();
Int charsCount = 0;

CharacterFactory factory = new CharacterFactory ();

Foreach (char c in chars)

{

CharsCount ++;

Character character = factory. GetCharacter (c );

Response. Write (character. Display (charsCount, "# 0000FF "));

}
Response. Write ("Total" + charsCount + "character ");


}

}

}

Class CharacterFactory
{

Private Dictionary <char, Character> _ characters =

New Dictionary <char, Character> ();

 

Public Character GetCharacter (char key)
{

// Uses "lazy initialization"

Character character = null;

If (_ characters. ContainsKey (key ))
{

Character = _ characters [key];

}

Else
{

Switch (key)
{

Case 'A': character = new characloud (); break;

Case 'B': character = new CharacterB (); break;

//...

Case 'Z': character = new CharacterZ (); break;

}

_ Characters. Add (key, character );

}

Return character;

}

}

 

/// <Summary>

/// The 'flywith' abstract class

/// </Summary>

Abstract class Character
{

Protected char symbol;

Protected int width;

Protected int height;

Protected string color;

Protected int descent;

Protected int fontsize;

 

Public string Display (int fontsize, string color)
{

This. fontsize = fontsize;
This. color = color;

Return "<font size =" + fontsize + "COLOR =" + color + ">" + this. symbol + "</FONT> ";
}
}

 

/// <Summary>

/// A' ConcreteFlyweight 'class

/// </Summary>

Class characloud: Character
{

// Constructor

Public characloud ()
{

This. symbol = 'a ';

}

// Public override string Display (int fontsize, string color)
//{

// This. fontsize = fontsize;
// This. color = color;

// Return "<font size =" + fontsize + "COLOR =" + color + ">" + this. symbol + "</FONT> ";
//}

}

 

/// <Summary>

/// A' ConcreteFlyweight 'class

/// </Summary>

Class CharacterB: Character
{

// Constructor

Public CharacterB ()
{

This. symbol = 'B ';

}

}

Class CharacterZ: Character
{

// Constructor

Public CharacterZ ()
{

This. symbol = 'Z ';

}

 

}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.