编程语言
首页 > 编程语言> > Design Pattern - Flyweight(Java)

Design Pattern - Flyweight(Java)

作者:互联网

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

Definition

Use sharing to support large numbers of fine-grained objects efficiently.

Participants

    The classes and/or objects participating in this pattern are:

Sample code in Java


This structural code demonstrates the Flyweight pattern in which a relatively small number of objects is shared many times by different clients.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.sample;

/**
 * The 'ConcreteFlyweight' class.
 *
 * @author Chimomo
 */
class ConcreteFlyweight extends Flyweight {

    /**
     * The operation.
     *
     * @param extrinsicState
     */
    @Override
    public void operation(int extrinsicState) {
        System.out.println("ConcreteFlyweight: " + extrinsicState);
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.sample;

/**
 * The 'Flyweight' abstract class.
 *
 * @author Chimomo
 */
abstract class Flyweight {

    /**
     * The operation.
     *
     * @param extrinsicState
     */
    public abstract void operation(int extrinsicState);

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.sample;

import java.util.Hashtable;

/**
 * The 'FlyweightFactory' class.
 *
 * @author Chimomo
 */
class FlyweightFactory {

    // The flyweights.
    private Hashtable flyweights = new Hashtable();

    /**
     * Initializes a new instance of the "FlyweightFactory"class.
     */
    public FlyweightFactory() {
        this.flyweights.put("X", new ConcreteFlyweight());
        this.flyweights.put("Y", new ConcreteFlyweight());
        this.flyweights.put("Z", new ConcreteFlyweight());
    }

    /**
     * Get flyweight.
     *
     * @param key
     * @return
     */
    public Flyweight getFlyweight(String key) {
        return (Flyweight) this.flyweights.get(key);
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.sample;

/**
 * Startup class for Structural Flyweight Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args
     */
    public static void main(String[] args) {

        // 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);
    }

}

/*
Output:
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
 */
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.sample;

/**
 * The 'UnsharedConcreteFlyweight' class.
 *
 * @author Chimomo
 */
class UnsharedConcreteFlyweight extends Flyweight {

    /**
     * The operation.
     *
     * @param extrinsicState Extrinsic state
     */
    @Override
    public void operation(int extrinsicState) {
        System.out.println("UnsharedConcreteFlyweight: " + extrinsicState);
    }

}

This real-world code demonstrates the Flyweight pattern in which a relatively small number of Character objects is shared many times by a document that has potentially many characters.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

/**
 * The 'Flyweight' abstract class.
 *
 * @author Chimomo
 */
abstract class Character {

    // The ascent.
    protected int ascent;

    // The descent.
    protected int descent;

    // The height.
    protected int height;

    // The point size.
    protected int pointSize;

    // The symbol.
    protected char symbol;

    // The width.
    protected int width;

    /**
     * Display.
     *
     * @param pointSize
     */
    public abstract void display(int pointSize);

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

/**
 * The 'ConcreteFlyweight' class.
 *
 * @author Chimomo
 */
class CharacterA extends Character {

    /**
     * Initializes a new instance of the "CharacterA" class.
     */
    public CharacterA() {
        this.symbol = 'A';
        this.height = 100;
        this.width = 120;
        this.ascent = 70;
        this.descent = 0;
    }

    /**
     * Display.
     *
     * @param pointSize
     */
    @Override
    public void display(int pointSize) {
        this.pointSize = pointSize;
        System.out.println(this.symbol + " (point size " + this.pointSize + ")");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

/**
 * The 'ConcreteFlyweight' class.
 *
 * @author Chimomo
 */
class CharacterB extends Character {

    /**
     * Initializes a new instance of the "CharacterB" class.
     */
    public CharacterB() {
        this.symbol = 'B';
        this.height = 100;
        this.width = 140;
        this.ascent = 72;
        this.descent = 0;
    }

    /**
     * Display.
     *
     * @param pointSize
     */
    @Override
    public void display(int pointSize) {
        this.pointSize = pointSize;
        System.out.println(this.symbol + " (point size " + this.pointSize + ")");
    }
    
}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

import java.util.HashMap;
import java.util.Map;

/**
 * The 'FlyweightFactory' class.
 *
 * @author Chimomo
 */
class CharacterFactory {

    // The characters.
    private Map<java.lang.Character, Character> characters = new HashMap<>();

    /**
     * Get character.
     *
     * @param key
     * @return
     */
    public Character getCharacter(char key) {

        // Uses "lazy initialization".
        Character character = null;

        if (this.characters.containsKey(key)) {
            character = this.characters.get(key);
        } else {
            switch (key) {
                case 'A':
                    character = new CharacterA();
                    break;

                case 'B':
                    character = new CharacterB();
                    break;

                // ...

                case 'Z':
                    character = new CharacterZ();
                    break;
            }
            this.characters.put(key, character);
        }
        return character;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

/**
 * The 'ConcreteFlyweight' class.
 *
 * @author Chimomo
 */
class CharacterZ extends Character {

    /**
     * Initializes a new instance of the "CharacterZ" class.
     */
    public CharacterZ() {
        this.symbol = 'Z';
        this.height = 100;
        this.width = 100;
        this.ascent = 68;
        this.descent = 0;
    }

    /**
     * Display.
     *
     * @param pointSize
     */
    @Override
    public void display(int pointSize) {
        this.pointSize = pointSize;
        System.out.println(this.symbol + " (point size " + this.pointSize + ")");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.flyweight.realworld;

/**
 * Startup class for Real-World Flyweight Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args Arguments
     */
    public static void main(String[] args) {

        // Build a document with text.
        final String document = "AAZZBBZB";
        char[] chars = document.toCharArray();
        CharacterFactory factory = new CharacterFactory();

        // Extrinsic state.
        int pointSize = 10;

        // For each character use a flyweight object.
        for (char c : chars) {
            pointSize++;
            Character character = factory.getCharacter(c);
            character.display(pointSize);
        }
    }

}

/*
Output:
A (point size 11)
A (point size 12)
Z (point size 13)
Z (point size 14)
B (point size 15)
B (point size 16)
Z (point size 17)
B (point size 18)
 */

标签:Java,Chimomo,Pattern,Flyweight,Design,flyweight,pointSize,class,chimomo
来源: https://blog.csdn.net/chimomo/article/details/80975442