2014-10-18 99 views
0

我有一个类正在变成God Object。在这个游戏中,它管理着主游戏场景本身,这应该是它变得这么大的原因。从对象中提取行为

但是我可以将这一类的某些部分组合在一起。具体来说,他们处理在游戏中随机触发的特殊模式。

我想知道什么是将此行为分开并将其转换为对象的最佳方法。我迷失在这里,因为这是一种行为,而不是一件“事情”,所以我想不出一个漂亮而优雅的解决方案。

class Game 
    var lives = 1 
    var score = 0 
    var time = 0 

    doStuff() {} 
    doMoreStuff() {} 
    specialActivate() {} 
    specialUpdate() {} 
    specialInput() {} 
    specialDeactivate() {} 

正如你所看到的,有很多“特殊模式”相关的功能。问题是这些函数中的一些函数依赖于Game中包含的变量和方法。

  1. 创建一个新类并在Context(或Model)对象中将所需的变量和回调一起发送。

    class Game 
        var lives = 1 
        var score = 0 
        var time = 0 
        var special = new Special(new SpecialContext(lives, score, time, doStuff)) 
    
        doStuff() {} 
        doMoreStuff() {} 
    
    class Special 
        var context:SpecialContext 
    
        constructor(context:SpecialContext) 
         this.context = context 
    
        activate() {} 
        deactivate() {} 
        input() {} 
        update() {} 
    
    class SpecialContext 
        var lives 
        var score 
        var time 
        var doStuff:Callback 
    
        constructor(lives, score, time, callbackDoStuff:Callback) 
         this.lives = lives 
         this.score = score 
         this.time = time 
         this.doStuff = callbackDoStuff 
    
  2. 创建界面并制作游戏界面,以保护游戏。

    class Game implements IGameSpecial 
        var lives = 1 
        var score = 0 
        var time = 0 
        var special = new Special(this) 
    
        doStuff() {} 
        doMoreStuff() {} 
    
    class Special 
        var game:IGameSpecial 
    
        constructor(gameSpecial:IGameSpecial) 
         this.game = game 
    
        activate() {} 
        deactivate() {} 
        input() {} 
        update() {} 
    
    interface IGameSpecial 
        doStuff() 
        getLives() 
        setLives() 
        getTime() 
        setTime() 
        getScore() 
        setScore() 
    
  3. 直接发送游戏类(脏方法)。

    class Game 
        var lives = 1 
        var score = 0 
        var time = 0 
        var special = new Special(this) 
    
        doStuff() {} 
        doMoreStuff() {} 
    
    class Special 
        var game:Game 
    
        constructor(gameSpecial:Game) 
         this.game = game 
    
        activate() {} 
        deactivate() {} 
        input() {} 
        update() {} 
    

有没有解决这个优雅的方式?这个问题是否有与之相关的名称或模式?

回答

1

我这样做:

1)创建一个接口:

interface ISpecialAction { 
    execute() 
} 

2)实现它为每一特殊行动:

class Activation implements ISpecialAction { 
    setLives() 
    setTime() 
    execute() 
} 

3)类游戏委托给本类

class Game { 

    specialActivate() { 
     Activation a = new Activation() 
     a.execute() 
    } 

} 

这个游戏类中的特殊动作会分离出来,所以 你不会再有什么关于特殊 动作的神类了。 例如,当使用Factory或Builder创建 特殊操作时,由特殊操作类实现的接口可以帮助 。

2

您提到了一种“特殊模式”,可能与“正常模式”相反。 “模式”是(或至少可以是)一种类型,它会建议使用状态模式。然而,它看起来好像你在类中有任何类型的代码来判断是否应该使用这些“特殊”方法,所以我假设在课堂外有某种东西决定了这一点。

我希望你可以将'特殊'方法分解成Game类中更简单的方法。这意味着你可以让“特殊模式”成为在适当的时候应用于游戏的装饰器。这是否会奏效取决于如何使用Game类。

+0

你是绝对正确的,事实上有一个枚举告诉我当前的模式。我只是不认为这是我的例子所需要的。 – Veehmot 2014-10-18 20:57:11

+0

在这种情况下,状态模式就是要走的路。网上有很多来源解释它,包括维基百科。 – 2014-10-18 21:50:07