コード例 #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
ファイル: Flyweight.cs プロジェクト: jhabaa/flyweight1.0
    // Start is called before the first frame update
    void Start()

    {   // affichage d'un ballon par instance d'un Flyweight Factory
        var factory = new FlyweightFactory();
        var ballon  = factory.getFlyweight(GameObject.CreatePrimitive(PrimitiveType.Sphere));

        ballon.show(new Vector3(Random.Range(-20, 20), Random.Range(-20, 20), Random.Range(-20, 20)), materials[Random.Range(0, materials.Length)]);
        //cube de visée
        body            = GameObject.Find("Cube").GetComponent <Rigidbody>();
        body.useGravity = false;
        body.GetComponent <Renderer>().material = materials[Random.Range(0, materials.Length)];

        for (int i = 0; i < ballsNumber; i++) // affichage des autres ballons
        {
            factory = new FlyweightFactory();
            ballon  = factory.getFlyweight(GameObject.CreatePrimitive(PrimitiveType.Sphere));
            ballon.show(new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)), materials[Random.Range(0, materials.Length)]);
        }
    }