21 Eyl 2015

Flyweight Pattern

Author: strongsoul | Filed under: Teknik

Flyweight tasarım deseni bir nesnenin çok kullanıldığı yapılarda bellek tüketimini optimize etmek amacıyla kullanılan bir desendir. Bunu nesne üretimini sürekli tekrar etmek yerine bir havuzdan tedarik etmek sureti ile gerçekleştirir.

Bir örnek ile pekiştirelim;

using System;
using System.Collections;

namespace Flyweight
{
	class MainApp
	{
		static void Main()
		{
			int extrinsicstate = 22;
			FlyweightFactory factory = new FlyweightFactory();

			Flyweight fx = factory.GetFlyweight("X");
			fx.Operation(--extrinsicstate);
			fx.OperationView ();

			Flyweight fx1 = factory.GetFlyweight("X");
			fx1.Operation(30);
			fx1.OperationView ();
			fx.OperationView ();

			Flyweight fy = factory.GetFlyweight("Y");
			fy.Operation(--extrinsicstate);
			fy.OperationView ();

			Flyweight fz = factory.GetFlyweight("Z");
			fz.Operation(--extrinsicstate);
			fz.OperationView ();

			UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();
			fu.Operation(--extrinsicstate);
			fu.OperationView ();

			//nesnenin bir örneğini oluşturamayıp hata fırlatacaktır
			Flyweight fk = factory.GetFlyweight("K");
			fk.Operation(--extrinsicstate);
			fk.OperationView ();

			Console.ReadKey();
		}
	}

	class FlyweightFactory
	{
		private Hashtable flyweights = new Hashtable();
		public FlyweightFactory()
		{
			flyweights.Add("X", new ConcreteFlyweight("X"));
			flyweights.Add("Y", new ConcreteFlyweight("Y"));
			flyweights.Add("Z", new ConcreteFlyweight("Z"));
		}
		public Flyweight GetFlyweight(string key)
		{
			return ((Flyweight)flyweights[key]);
		}
	}
		
	abstract class Flyweight
	{
		public abstract void Operation(int lextrinsicstate);
		public abstract void OperationView();
	}
		
	class ConcreteFlyweight : Flyweight
	{
		string key="";
		public ConcreteFlyweight(String lkey)
		{
			key = lkey;
		}

		int extrinsicstate;
		public override void Operation(int lextrinsicstate)
		{
			extrinsicstate = lextrinsicstate;
		}
			
		public override void OperationView()
		{
			Console.WriteLine ("ConcreteFlyweight: (" + key + ")" + extrinsicstate);
		}
	}
		
	class UnsharedConcreteFlyweight : Flyweight
	{
		int extrinsicstate;
		public override void Operation(int lextrinsicstate)
		{
			extrinsicstate = lextrinsicstate;
		}

		public override void OperationView()
		{
			Console.WriteLine("UnsharedConcreteFlyweight: " + extrinsicstate);
		}
	}
}

Çıktısı :
————————————–

ConcreteFlyweight: (X)21
ConcreteFlyweight: (X)30
ConcreteFlyweight: (X)30
ConcreteFlyweight: (Y)20
ConcreteFlyweight: (Z)19
UnsharedConcreteFlyweight: 18

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Flyweight.MainApp.Main () [0x000b5] in /home/strongsoul-pc/Desktop/yandex-disk/development/incelenen/dpcsharp/patterns/flyweight/Flyweight/Flyweight/Program.cs:36
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Flyweight.MainApp.Main () [0x000b5] in /home/strongsoul-pc/Desktop/yandex-disk/development/incelenen/dpcsharp/patterns/flyweight/Flyweight/Flyweight/Program.cs:36

Press any key to continue…

Yorum Bırakın