2017-10-17 54 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(); 

    } 
} 

或者我可能錯過了這是一回事?