C#装饰器模式和包装问题

时间:2016-10-26 03:59:12

标签: c# decorator

我在处理代码返回值时遇到问题。所以基本上我试图显示以下输出:

  

Morg,输入,xy, paddle

我的代码在C#中,根据下面的代码,这些输出基于我的字符串返回值。我正在使用它们希望测试我的程序以查看它是否有效。到目前为止它工作但不是我想要的方式。我只能显示类型返回值,但我似乎无法显示其余部分。

这就是它给我的。

  

输入

但最顶端的那个是我想要显示为输出的那个。不只是类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            MorgReader myMorg = new FileReader();
            MorgReader myMorg2 = new MorgType(new MorgXY(new MorgMovement(new FileReader())));

            Console.WriteLine(myMorg.Read());
            Console.WriteLine(myMorg2.Read());

        }

    }

    // THE MAIN ABSTRACT CLASS //
    abstract class MorgReader
    {
        public abstract string Read();
    }

    //  THE CLASS THAT WILL READ THE FILE //
    class FileReader : MorgReader
    {
        public override string Read()
        {
            return "Morg";
        }
    }

    //  MY READER DECORATOR //

    abstract class ReaderDecorator : MorgReader
    {
        private MorgReader wrappedReader;
        protected ReaderDecorator(MorgReader wrapped)
        {
            wrappedReader = wrapped;
        }
        protected MorgReader WrappedReader
        { get { return wrappedReader; } }
    }

    class MorgType : ReaderDecorator
    {
        public MorgType(MorgReader wrapped) : base(wrapped)
        { }

        public override string Read()
        {
            return "type";
        }
    }

    class MorgXY : ReaderDecorator
    {
        public MorgXY(MorgReader wrapped) : base(wrapped)
        { }

        public override string Read()
        {
            return "x,y";
        }
    }

    class MorgMovement : ReaderDecorator
    {
        public MorgMovement(MorgReader wrapped) : base(wrapped)
        { }

        public override string Read()
        {
            return "paddle";
        }
    }
}

2 个答案:

答案 0 :(得分:2)

更改您的ReaderDecorator课程

abstract class ReaderDecorator : MorgReader
{
    private MorgReader wrappedReader;
    protected ReaderDecorator(MorgReader wrapped)
    {
        wrappedReader = wrapped;
    }

    protected MorgReader WrappedReader
    { get { return wrappedReader; } }

    public override string Read()
    {
        var wrapped = WrappedReader.Read();
        if (!string.IsNullOrEmpty(wrapped))
            wrapped += ", ";

        return wrapped + ReadImpl();
    }

    // template method
    protected abstract string ReadImpl();
}

在具体类中实现ReadImpl,如下所示

class MorgType : ReaderDecorator
{
    public MorgType(MorgReader wrapped) : base(wrapped)
    { }

    protected override string ReadImpl()
    {
        return "type";
    }
}

如果您不想引入ReadImpl方法,那么您应该在每个具体类中覆盖Read方法,如下所示

public override string Read()
{
    var wrapped = WrappedReader.Read();
    if (!string.IsNullOrEmpty(wrapped))
        wrapped += ", ";

    return wrapped + "type";
}

答案 1 :(得分:1)

我认为你的装饰图案缺失了......好吧,装饰品(LOL)。所以装饰你的抽象类后代,如下所示:

class MorgType : ReaderDecorator
{
    public MorgType(MorgReader wrapped) : base(wrapped)
    { }

    public override string Read()
    {
        return WrappedReader == null ? "type" : WrappedReader.Read() + ",type";
    }
}

class MorgXY : ReaderDecorator
{
    public MorgXY(MorgReader wrapped) : base(wrapped)
    { }

    public override string Read()
    {
        return WrappedReader == null ? "x,y" : WrappedReader.Read() + ",x,y";
    }
}

class MorgMovement : ReaderDecorator
{
    public MorgMovement(MorgReader wrapped) : base(wrapped)
    { }

    public override string Read()
    {
        return WrappedReader == null ? "paddle" : WrappedReader.Read() + ",paddle";
    }
}

你应该看到你期望myMorg2的结果,这是你在帖子中描述的以逗号分隔的输出。