��Ԫ�����ڲ����Ҳ����滷���ı���ı�Ĺ�����֣���Ϊ��Ԫ������ڲ�״̬�� ��Ԫģʽ���Ա�������ķdz�������Ŀ������ڳ�������У���ʱ��Ҫ���ɴ���ϸ���ȵ���ʵ������ʾ���ݡ� ���������Щʵ�����˼�������������϶�����ͬ�ģ���ʱ���ܽ��ܴ���ȵļ�����Ҫʵ������������� ����ܰ���Щ�����Ƶ���ʵ�������棬�ڷ�������ʱ�����Ǵ��ݽ������Ϳ���ͨ����Ԫ���ٵ���ʵ���������� ���һ��Ӧ�ó���ʹ���˴����Ķ��󣬶������Ķ�������˺ܴ�Ĵ洢����ʱ��Ӧ�ÿ���ʹ�á� ���о��Ƕ���Ĵ����״̬�����ⲿ״̬�����ɾ���ⲿ״̬����ô�����ý��ٵĹ��������ȡ���ܶ�Ķ��� ʹ����Ԫģʽ�����˹������ʵ�������ͻ��󽵵͡� ��һ����Եײ�����ģʽ������ .NET�е��ַ��� string
示例#1
0
        /*
         * note que quando vou buscar um objeto eles já estão instanciados.
         *
         * A intenção desse padrão é usar compartilhamento para suportar um grande número de objetos semelhantes de forma eficiente. O padrão visa minimizar o uso
         * de memória no armazenamento de vários objetos através do compartilhamento das informações em comum que essas instâncias possuem.
         *
         * Participantes:
         * Flyweight - é a interface que define como devem ser as classes que trabalham neste padrão. é importante citar que ela descreve como os dados extrínsecos são objtidos,
         * ou seja, as operações que fazem essa transposição de informações;
         * ConcreteFlyweight - são as classes que implementam o contrato IFlyweight e que devem permitir o comportamento. Essas classes mantém dados intrínsecos;
         * UnsharedConcreteFlyweight - possuem as mesmas características do ConcreteFlyweight, no entanto não são objetos compartilhados. Isso porque este padrão permite
         * o compartilhamento, mas não obriga que isso seja sempre seguido;
         * FlyweightFactory - Classe que é responsável pela criação dos objetos, além de manter o repositório de objetos que implementam o Flyweight;
         * Client - É quem utiliza os objetos IFlyweight, sempre os obtendo através do FlyweightFactory
         */
        public void Flyweight()
        {
            // externo
            int ext = 10;

            FlyweightFactory fabrica = new FlyweightFactory();

            Flyweight.Flyweight f1 = fabrica.getFlyweight("A");
            Response.Write(f1.Operation(ext++));

            Flyweight.Flyweight f2 = fabrica.getFlyweight("B");
            Response.Write("<br>" + f2.Operation(ext++));

            Flyweight.Flyweight f3 = fabrica.getFlyweight("C");
            Response.Write("<br>" + f3.Operation(ext++));

            Flyweight.Flyweight f4 = fabrica.getFlyweight("A");
            Response.Write("<br>" + f4.Operation(ext++));

            Flyweight.Flyweight f5 = new UnsharedConcreteFlyweight();
            Response.Write("<br>" + f5.Operation(ext++));
        }
示例#2
0
        static void Main()
        {
            int brick = 10;

            FlyweightFactory factory = new FlyweightFactory();

            Flyweight fx = factory.GetFlyweight("X");

            fx.Operation(--brick);

            Flyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--brick);

            Flyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--brick);

            UnsharedBrickFlyweight fu = new UnsharedBrickFlyweight();

            fu.Operation(--brick);

            Console.ReadKey();
        }
示例#3
0
        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 fu = new UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            Console.ReadKey();
        }
示例#4
0
 //将不使用的蝇量对象放回蝇量工厂
 public void SetFlyweight(Flyweight flyweight)
 {
     //重置外部状态
     flyweight.Operation(null);
     flyweights.Add(flyweight);
 }
示例#5
0
 public ConcreteObject(int id, Flyweight flyweight)
 {
     this.Id     = id;
     this.Shared = flyweight;
 }