【问题标题】:Unity-Encrypting String using the Decorator Pattern使用装饰器模式的 Unity 加密字符串
【发布时间】:2017-10-17 16:31:06
【问题描述】:

我正在尝试应用装饰器模式来制作将单词加密为某种加密的对象,例如 L337 方法,该方法将 9 替换为 g 或 4 替换为 r。基本上,我想在输入字段中输入一个单词并在文本对象中显示加密的单词。但我无法让 L337 装饰器从主装饰器类继承。它不接受关键字“super”,所以我尝试了基本词,但是当我实现加密时,它不会接受对象 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()
    {

    }

}

}

【问题讨论】:

    标签: java c# unity3d


    【解决方案1】:

    我认为您实际上想使用tempEncryption,但您并没有真正说出不能使用newEncryption 的地方,所以我猜。

    但无论如何,我希望这能澄清一些事情。它从您的代码略微编辑,因此我不需要放置 GUI 的东西,但您可以将其 CnP 统一。

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

    或者我错过了这一切?!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 2013-05-07
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多