public static void FlyWeight() { int exterinsicSate = 22; FlyWeightFactory factory = new FlyWeightFactory(); factory.SetFlyWeight(new ConcreteFlyWeight(), "X"); factory.SetFlyWeight(new ConcreteFlyWeight(), "Y"); factory.SetFlyWeight(new ConcreteFlyWeight(), "Z"); factory.GetConFlyWeight("X").Operation(--exterinsicSate); factory.GetConFlyWeight("Y").Operation(--exterinsicSate); factory.GetConFlyWeight("Z").Operation(--exterinsicSate); UnSharedConcreteFlyweight un = new UnSharedConcreteFlyweight(); un.Operation(--exterinsicSate); }
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); Flyweight uf = new UnSharedConcreteFlyweight(); uf.Operation(--extrinsicstate); Console.Read(); }
static void Main(string[] args) { try { {//BasicStructure int extrinsicState = 22; FlyweightFactory flyweightFactory = new FlyweightFactory(); var flyweight = flyweightFactory.GetFlyweight("X"); flyweight.Operation(--extrinsicState); flyweight = flyweightFactory.GetFlyweight("Y"); flyweight.Operation(--extrinsicState); flyweight = flyweightFactory.GetFlyweight("Z"); flyweight.Operation(--extrinsicState); UnSharedConcreteFlyweight uf = new UnSharedConcreteFlyweight(); uf.Operation(--extrinsicState); } {//SituationSimulation WebSiteFactory webSiteFactory = new WebSiteFactory(); var fx = webSiteFactory.GetWebSiteCategory("产品展示"); fx.Use(); var fy = webSiteFactory.GetWebSiteCategory("产品展示"); fy.Use(); var fz = webSiteFactory.GetWebSiteCategory("产品展示"); fz.Use(); var fl = webSiteFactory.GetWebSiteCategory("博客"); fl.Use(); var fm = webSiteFactory.GetWebSiteCategory("博客"); fm.Use(); var fn = webSiteFactory.GetWebSiteCategory("博客"); fn.Use(); Console.WriteLine($"网站分类总数为:{webSiteFactory.GetWebSiteCount()}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
static void Main(string[] args) { //享元模式 int extrinsicState = 22; FlyWeightFactory fact = new FlyWeightFactory(); Flyweight fx = fact.GetFlayWeight("x"); fx.Operation(--extrinsicState); Flyweight fy = fact.GetFlayWeight("y"); fy.Operation(--extrinsicState); Flyweight fz = fact.GetFlayWeight("z"); fz.Operation(--extrinsicState); UnSharedConcreteFlyweight unsharedFlyweight = new UnSharedConcreteFlyweight(); unsharedFlyweight.Operation(--extrinsicState); Console.WriteLine("********************************************"); WebSiteFactory wf = new WebSiteFactory(); WebSiteClass wc = wf.getWebSiteCategory("产品展示"); wc.Use(new User("小鸟")); WebSiteClass wc1 = wf.getWebSiteCategory("产品展示"); wc.Use(new User("大鸟")); WebSiteClass bg = wf.getWebSiteCategory("博客"); wc.Use(new User("科技")); Console.Read(); }