2017-10-17 12 views
0

我试图将装饰模式应用于将一个单词加密成某种加密的对象,如L337方法,它用g替换字母9,或用r替换4 。基本上,我想在输入字段中输入一个单词并在文本对象中显示加密的单词。但是我不能让L337装饰器从主装饰器类继承。它不会接受关键字'super',所以我尝试了基本词,但是当我实现Encrypt时,它不会接受newEncryption对象。有人可以帮我弄清楚如何把这种模式放在一起吗?使用装饰模式统一加密字符串

我基本知道装饰模式是什么。它正在制作一个对象,制作一个基本的装饰器,并制作一个特定的装饰器,并用装饰来实例化对象以获得独有的方法和特性。

public class Encryption : MonoBehaviour 
{ 

public static InputField inputBox; 
public static Text outputText; 



public interface IEncryption { void Encrypt(); } 


public class TextEncryption : IEncryption 
{ 
    public void Encrypt() 
    { 
     string currentText = inputBox.text; 
     outputText.text = currentText; 
    } 
} 


public abstract class encryptionDecorator : IEncryption 
{ 
    protected IEncryption tempEncryption; 
    public encryptionDecorator(IEncryption newEncryption) 
    { 
     tempEncryption = newEncryption; 
    } 

    public void Encrypt() 
    { 
     tempEncryption.Encrypt(); 
    } 
} 

public class L337EncryptionDecorator : encryptionDecorator 
{ 
    public L337EncryptionDecorator(IEncryption newEncryption) : base(newEncryption) 
    { 
     print("Encrypting L337 Code"); 
    } 

    public void Encrypt() 
    { 

    } 

} 

}

回答

0

我想到你居然想用tempEncryption,但你没有真正告诉你在那里不能使用newEncryption所以即时猜测。

但无论如何,我希望这将清除一些事情。它从你的代码中稍作编辑,所以我不需要把GUI的东西,但你可以把它合并到一起。

using UnityEngine; 

public class Encryption : MonoBehaviour { 

    public interface IEncryption { 
     void Encrypt(); 
    } 

    public class TextEncryption : IEncryption { 
     public void Encrypt() { 
     } 
    } 

    public abstract class EncryptionDecorator : IEncryption { 

     protected IEncryption tempEncryption; 

     public EncryptionDecorator(IEncryption newEncryption) { 

      //this will be called when you override the constructor 
      Debug.Log("In EncryptionDecorator constructor: " + newEncryption.GetType()); 
      tempEncryption = newEncryption; 
     } 

     //if you are going to override a method in a child class, 
     //declare it either abstract ("no body; passes implementation to child") or 
     //virtual ("allows for a base implementation") 
     public virtual void Encrypt() { 

      Debug.Log("In EncryptionDecorator.Encrypt(): " + tempEncryption.GetType()); 
      tempEncryption.Encrypt(); 
     } 
    } 

    public class L337EncryptionDecorator : EncryptionDecorator { 

     public L337EncryptionDecorator(IEncryption newEncryption) : base(newEncryption) { 

      //newEncryption is a parameter, think of it as sort of a local variable. 
      //but since you pass it down to the parent class, it gets assigned to tempEncryption 
      //the base-class constructor is called first! 
      Debug.Log("In L337EncryptionDecorator constructor: " + newEncryption.GetType()); 
     } 

     //this overrides the base implementation. you can call it with 
     //base.Encrypt() though. 
     public override void Encrypt() { 
      //you have no parameters here, but you could use the inherited variable tempEncryption because you declared it protected 
      Debug.Log("In L337EncrytionDecorator.Encrypt(): " + tempEncryption.GetType()); 

      //base refers to the base class 
      base.Encrypt(); 
     } 

    } 

    void Start() { 

     IEncryption encryption = new L337EncryptionDecorator(new TextEncryption()); 

     encryption.Encrypt(); 

    } 
} 

或者我可能错过了这是一回事?