-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flyweight.cs
56 lines (45 loc) · 1.29 KB
/
Flyweight.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
namespace Flyweight
{
public interface IFlyweightFactory
{
Flyweight GetFlyweight(char symbol);
}
public class FlyweightFactory: IFlyweightFactory
{
private Dictionary<char, Flyweight> alphabet;
public FlyweightFactory()
{
alphabet = new Dictionary<char, Flyweight>();
}
public Flyweight GetFlyweight(char symbol)
{
if (!this.alphabet.ContainsKey(symbol))
{
this.alphabet.Add(symbol, new WordFlyweight());
}
return this.alphabet[symbol] as Flyweight;
}
}
public abstract class Flyweight
{
protected static int CountUsedWords = 0;
public int id { get; private set; }
public Flyweight()
{
CountUsedWords++;
var random = new Random();
this.id = random.Next(0, 1000);
}
public abstract void SomeOperation(string message);
}
public class WordFlyweight: Flyweight
{
public WordFlyweight(): base() { }
public override void SomeOperation(string message)
{
Console.WriteLine($"Message: {message}\n Count used words: {Flyweight.CountUsedWords}");
}
}
}