static void Main(string[] args) { int state = 0; FlyweightFactory factory = new FlyweightFactory(); Flyweight flyweight = factory.GetFlyweight("2"); flyweight.Operation(state); flyweight = factory.GetFlyweight("228"); flyweight.Operation(state); flyweight = new UnsharedFlyweight(); flyweight.Operation(state); }
// "FlyweightFactory" // "ConcreteFlyweight" public void TestFlyWeigthPattern() { // Arbitrary extrinsic state int extrinsicstate = 22; var f = new FlyweightFactory(); // Work with different flyweight instances Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = f.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = f.GetFlyweight("Z"); fz.Operation(--extrinsicstate); var uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicstate); // Wait for user Console.Read(); }
public static void FlyweightStructural() { // Arbitrary extrinsic state int extrinsicstate = 22; FlyweightFactory factory = new FlyweightFactory(); // Work with different flyweight instances Flyweight fx = factory.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = factory.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = factory.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight(); fu.Operation(--extrinsicstate); }
static void Main() { var extrinsicState = 0; Flyweight flyweight = null; var factory = new FlyweightFactory(); flyweight = factory.GetFlyweight("1"); flyweight.Operation(extrinsicState); flyweight = factory.GetFlyweight("10"); flyweight.Operation(extrinsicState); flyweight = new UnsharedConcreteFlyweight(); flyweight.Operation(extrinsicState); }
/// <summary> /// Entry point into console application. /// </summary> static void Main() { // Arbitrary extrinsic state int extrinsicstate = 22; FlyweightFactory factory = new FlyweightFactory(); // Work with different flyweight instances Flyweight fx = factory.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = factory.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = factory.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight(); fu.Operation(--extrinsicstate); // Wait for user Console.ReadKey(); }
public void FlyweightShoulHaveStateUsedAfterTakingOperated() { Flyweight flyweight = new Flyweight("F1"); flyweight.Operation(); flyweight.State.Should().BeEquivalentTo("free"); }
/// <summary> /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { //定义外部状态,例如字母的位置等信息 int externalstate = 10; //初始化享元工厂 FlyweightFactory factory = new FlyweightFactory(); ; //判断是否已经创建了字母A,如果已经创建就直接使用创建的对象A Flyweight fA = factory.GetFlyweight("A"); if (fA != null) { //把外部状态作为享元对象的方法调用参数 fA.Operation(--externalstate); } //判断是否已经创建了字母B Flyweight fB = factory.GetFlyweight("B"); if (fB != null) { fB.Operation(--externalstate); } Flyweight fC = factory.GetFlyweight("C"); if (fC != null) { fC.Operation(--externalstate); } Flyweight fD = factory.GetFlyweight("D"); if (fD != null) { fD.Operation(--externalstate); } else { Console.WriteLine("驻留池中不存在字符串D"); Console.WriteLine("向驻留池添加字符D。。。。"); ConcreteFlyweight d = new ConcreteFlyweight("D"); factory.flyweights.Add("D", d); } Flyweight fD2 = factory.GetFlyweight("D"); if (fD2 != null) { fD2.Operation(--externalstate); } Console.Read(); }
static void Main() { int extrinsicstate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight uf = new UnshareConcreteFlyweight(); uf.Operation(--extrinsicstate); Console.ReadKey(); }
public static void FlyweightPattern() { int extrinsicatate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicatate); Flyweight fy = f.GetFlyweight("Y"); fx.Operation(--extrinsicatate); Flyweight fz = f.GetFlyweight("Z"); fx.Operation(--extrinsicatate); Flyweight uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicatate); Console.Read(); }
static void FlyWeightInvoke() { int externalstate = 10; // 定义外部状态,如字母的位置等信息 FlyweightFactory factory = new FlyweightFactory(); // 初始化享元工厂 Flyweight fa = factory.GetFlyweight("A"); fa.Operation(--externalstate); Flyweight fb = factory.GetFlyweight("B"); fb.Operation(--externalstate); Flyweight fd = factory.GetFlyweight("D"); fd.Operation(--externalstate); }
static void Main(string[] args) { int extrinsicstate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = f.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = f.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicstate); Console.Read(); }
public void StructuralTest() { int extrinsicstate = 22; FlyweightFactory factory = new FlyweightFactory(); Flyweight fx = factory.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = factory.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = factory.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight(); fu.Operation(--extrinsicstate); }
void Main() { int extrinsicstate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = f.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fd = f.GetFlyweight("D"); fd.Operation(--extrinsicstate); UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicstate); }
private static void ShowFlyweight() { Console.WriteLine("================================================"); Console.WriteLine("Pattern code (Flyweight):"); int extrinsicstate = 22; FlyweightFactory factory = new FlyweightFactory(); Flyweight fx = factory.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = factory.GetFlyweight("Y"); fy.Operation(--extrinsicstate); Flyweight fz = factory.GetFlyweight("Z"); fz.Operation(--extrinsicstate); UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight(); fu.Operation(--extrinsicstate); Console.WriteLine("\nReal code (Flyweight):"); // Build a document with text string document = "AAZZBBZB"; char[] chars = document.ToCharArray(); CharacterFactory characterFactory = new CharacterFactory(); // extrinsic state int pointSize = 10; // For each character use a flyweight object foreach (char c in chars) { pointSize++; Character character = characterFactory.GetCharacter(c); character.Display(pointSize); } }
static void Main(string[] args) { // 定义外部状态,例如字母的位置等信息。 int externalstate = 10; // 初始化享元工厂 FlyweightFactory factory = new FlyweightFactory(); // 判断是否已经创建了字母A,如果已经创建就直接使用创建的对象A。 Flyweight fa = factory.GetFlyweight("A"); // 把外部状态作为享元对象的方法调用参数 fa?.Operation(--externalstate); // 判断是否已经创建了字母B Flyweight fb = factory.GetFlyweight("B"); fb?.Operation(--externalstate); // 判断是否已经创建了字母C Flyweight fc = factory.GetFlyweight("C"); fc?.Operation(--externalstate); // 判断是否已经创建了字母D Flyweight fd = factory.GetFlyweight("D"); if (fd != null) { fd.Operation(--externalstate); } else { Console.WriteLine("驻留池中不存在字符串D"); // 这时候就需要创建一个对象并放入驻留池中 ConcreteFlyweight d = new ConcreteFlyweight("D"); factory.Flyweights.Add("D", d); } Console.Read(); }
private void Start() { // Arbitrary extrinsic state(外部状态) int externalState = 22; FlyweightFactory factory = new FlyweightFactory(); // Work with different flyweight instances Flyweight fx = factory.GetFlyweight("X"); fx.Operation(--externalState); Flyweight fy = factory.GetFlyweight("Y"); fy.Operation(--externalState); Flyweight fz = factory.GetFlyweight("Z"); fz.Operation(--externalState); UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight(); fu.Operation(--externalState); }
// Entry point into console application. static void Main() { // Arbitrary extrinsic state int extrinsic_state = 22; factory = new FlyweightFactory(); // Work with different flyweight instances ch = factory.GetFlyweight("X"); ch.Operation(--extrinsic_state); ch = factory.GetFlyweight("Y"); ch.Operation(--extrinsic_state); ch = factory.GetFlyweight("Z"); ch.Operation(--extrinsic_state); ch = new UnsharedConcreteFlyweight(); ch.Operation(--extrinsic_state); // Wait for user Console.ReadKey(); }
public void FlyweightShouldPerformOperation() { IFlyweight flyweight = new Flyweight("F1"); flyweight.Operation().Should().BeEquivalentTo("Operated by F1"); }
public void Operation() { _flyweight.Operation(_uniqueState); }