首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FlyWeight设计模式

FlyWeight设计模式
EN

Stack Overflow用户
提问于 2018-08-20 08:44:24
回答 1查看 179关注 0票数 1

我正在浏览一个我在网上发现的飞重模式的例子。

代码语言:javascript
复制
import java.util.Random;
import java.util.HashMap;

// A common interface for all players
interface Player
{
    public void assignWeapon(String weapon);
    public void mission();
}

// Terrorist must have weapon and mission
class Terrorist implements Player
{
    // Intrinsic Attribute
    private final String TASK;

    // Extrinsic Attribute
    private String weapon;

    public Terrorist()
    {
        TASK = "PLANT A BOMB";
    }
    public void assignWeapon(String weapon)
    {
        // Assign a weapon
        this.weapon = weapon;
    }
    public void mission()
    {
        //Work on the Mission
        System.out.println("Terrorist with weapon "
                        + weapon + "|" + " Task is " + TASK);
    }
}

// CounterTerrorist must have weapon and mission
class CounterTerrorist implements Player
{
    // Intrinsic Attribute
    private final String TASK;

    // Extrinsic Attribute
    private String weapon;

    public CounterTerrorist()
    {
        TASK = "DIFFUSE BOMB";
    }
    public void assignWeapon(String weapon)
    {
        this.weapon = weapon;
    }
    public void mission()
    {
        System.out.println("Counter Terrorist with weapon "
                        + weapon + "|" + " Task is " + TASK);
    }
}

// Claass used to get a playeer using HashMap (Returns
// an existing player if a player of given type exists.
// Else creates a new player and returns it.
class PlayerFactory
{
    /* HashMap stores the reference to the object
    of Terrorist(TS) or CounterTerrorist(CT). */
    private static HashMap <String, Player> hm =
                        new HashMap<String, Player>();

    // Method to get a player
    public static Player getPlayer(String type)
    {
        Player p = null;

        /* If an object for TS or CT has already been
        created simply return its reference */
        if (hm.containsKey(type))
                p = hm.get(type);
        else
        {
            /* create an object of TS/CT */
            switch(type)
            {
            case "Terrorist":
                System.out.println("Terrorist Created");
                p = new Terrorist();
                break;
            case "CounterTerrorist":
                System.out.println("Counter Terrorist Created");
                p = new CounterTerrorist();
                break;
            default :
                System.out.println("Unreachable code!");
            }

            // Once created insert it into the HashMap
            hm.put(type, p);
        }
        return p;
    }
}

// Driver class
public class CounterStrike
{
    // All player types and weopons (used by getRandPlayerType()
    // and getRandWeapon()
    private static String[] playerType =
                    {"Terrorist", "CounterTerrorist"};
    private static String[] weapons =
    {"AK-47", "Maverick", "Gut Knife", "Desert Eagle"};


    // Driver code
    public static void main(String args[])
    {
        /* Assume that we have a total of 10 players
        in the game. */
        for (int i = 0; i < 10; i++)
        {
            /* getPlayer() is called simply using the class
            name since the method is a static one */
            Player p = PlayerFactory.getPlayer(getRandPlayerType());

            /* Assign a weapon chosen randomly uniformly
            from the weapon array */
            p.assignWeapon(getRandWeapon());

            // Send this player on a mission
            p.mission();
        }
    }

    // Utility methods to get a random player type and
    // weapon
    public static String getRandPlayerType()
    {
        Random r = new Random();

        // Will return an integer between [0,2)
        int randInt = r.nextInt(playerType.length);

        // return the player stored at index 'randInt'
        return playerType[randInt];
    }
    public static String getRandWeapon()
    {
        Random r = new Random();

        // Will return an integer between [0,5)
        int randInt = r.nextInt(weapons.length);

        // Return the weapon stored at index 'randInt'
        return weapons[randInt];
    }
}

Implementation:这是在反击游戏中创造恐怖分子和打击恐怖分子的工具。所以我们有两个等级,一个是恐怖分子(T),另一个是反恐(CT)。每当玩家想要武器时,我们都会给他分配武器。在任务中,恐怖分子的任务是放置炸弹,而反恐怖分子则必须扩散炸弹。

我相信这个例子是wrong.In这个代码对象保持不变,我们只是改变武器,如果一个新玩家进入游戏,每个人的武器的意志改变。我理解得对吗?谁能给我提供一个很好的飞行重量的例子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 09:15:52

你是对的,你的榜样是错误的。你只有两个玩家在hashmap (CT和Tero)。使用hashmap来存储两个必须播放的对象是非常简单的。

而且,由于您只有两次播放,并且正在更改它们的attribut,但实际上您正在更改所有player config :)

我要修改了一小部分示例证明这是错误的。我只需将播放器存储在一个数组中,然后重新打印它。

代码语言:javascript
复制
     System.out.println("reuse --------------------------");
    for(Player p : players){
        p.mission();
    }

这给了我流量输出:

代码语言:javascript
复制
    Terrorist Created
Terrorist with weapon Maverick| Task is PLANT A BOMB
Counter Terrorist Created
Counter Terrorist with weapon AK-47| Task is DIFFUSE BOMB
Terrorist with weapon AK-47| Task is PLANT A BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Terrorist with weapon Gut Knife| Task is PLANT A BOMB
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB
Terrorist with weapon Gut Knife| Task is PLANT A BOMB
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
reuse --------------------------
Terrorist with weapon Maverick| Task is PLANT A BOMB
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB
Terrorist with weapon Maverick| Task is PLANT A BOMB

你清楚地看到每支球队都是同样的武器(最后一次被选中)。

如果您想要一个很好的例子,请参考您的文章;) :) )类似于维基百科

主要是重用已经创建的配置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51927070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档