forked from WeihanLi/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototype.cs
More file actions
33 lines (28 loc) · 896 Bytes
/
Prototype.cs
File metadata and controls
33 lines (28 loc) · 896 Bytes
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
using System;
using System.Collections.Concurrent;
namespace FlyweightPattern
{
internal abstract class Flyweight
{
public abstract void Operation(int extrinsicstate);
}
internal class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("operation in ConcreteFlyweight");
}
}
internal class UnsharedFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("operation in UnsharedFlyweight");
}
}
internal class FlyWeightFactory
{
private readonly ConcurrentDictionary<string, Flyweight> _flyweights = new ConcurrentDictionary<string, Flyweight>();
public Flyweight GetFlyweight(string name) => _flyweights.GetOrAdd(name, n => new ConcreteFlyweight());
}
}