简单工厂模式一

 

Block.cs

namespace SimpleFactory
{
    interface Block
    {
        int[][] GetData();
        void Change();
    }

    public class Square : Block
    {
        int[][] data;
        public Square()
        {
            data = new int[][]
            {
                new int[] { 0, 0, 0, 0 },
                new int[] { 0, 1, 1, 0 },
                new int[] { 0, 1, 1, 0 },
                new int[] { 0, 0, 0, 0 }
            };
        }
        public int[][] GetData()
        {
            return data;
        }
        public void Change() { }
    }

    public class Bar : Block
    {
        int index;
        int[][][] data;
        public Bar()
        {
            index = 0;
            data = new int[][][]
            {
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 1, 1, 1, 1 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                }
            };
        }
        public int[][] GetData()
        {
            return data[index];
        }
        public void Change()
        {
            index = (index + 1) % 2;
        }
    }

    public class LBlock : Block
    {
        int index;
        int[][][] data;
        public LBlock()
        {
            index = 0;
            data = new int[][][]
            {
                new int[][]
                {
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 1, 1, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 0, 0, 1, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 1, 1, 1, 0 },
                    new int[] { 1, 0, 0, 0 },
                    new int[] { 0, 0, 0, 0 },
                }
            };
        }
        public int[][] GetData()
        {
            return data[index];
        }
        public void Change()
        {
            index = (index + 1) % 4;
        }
    }

    public class LRBlock : Block
    {
        int index;
        int[][][] data;
        public LRBlock()
        {
            index = 0;
            data = new int[][][]
            {
                new int[][]
                {
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 1, 1, 1, 0 },
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 1, 0, 0, 0 },
                    new int[] { 1, 1, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                }
            };
        }
        public int[][] GetData()
        {
            return data[index];
        }
        public void Change()
        {
            index = (index + 1) % 4;
        }
    }

    public class ZBlock : Block
    {
        int index;
        int[][][] data;
        public ZBlock()
        {
            index = 0;
            data = new int[][][]
            {
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 1, 1, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 0, 1, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 1, 0, 0 },
                }
            };
        }
        public int[][] GetData()
        {
            return data[index];
        }
        public void Change()
        {
            index = (index + 1) % 2;
        }
    }

    public class ZRBlock : Block
    {
        int index;
        int[][][] data;
        public ZRBlock()
        {
            index = 0;
            data = new int[][][]
            {
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 1, 1, 0, 0 },
                    new int[] { 0, 0, 0, 0 },
                },
                new int[][]
                {
                    new int[] { 0, 0, 0, 0 },
                    new int[] { 0, 1, 0, 0 },
                    new int[] { 0, 1, 1, 0 },
                    new int[] { 0, 0, 1, 0 },
                }
            };
        }
        public int[][] GetData()
        {
            return data[index];
        }
        public void Change()
        {
            index = (index + 1) % 2;
        }
    }
}

BlockFactory.cs

namespace SimpleFactory
{
    class BlockFactory
    {
        public static Block CreateBlock(int type)
        {
            Block block = null;
            switch (type)
            {
                case 0:
                    block = new Square();
                    break;
                case 1:
                    block = new Bar();
                    break;
                case 2:
                    block = new LBlock();
                    break;
                case 3:
                    block = new LRBlock();
                    break;
                case 4:
                    block = new ZBlock();
                    break;
                default:
                    block = new ZRBlock();
                    break;
            }
            return block;
        }
    }
}

Program.cs

namespace SimpleFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            Block block = BlockFactory.CreateBlock(0);
            ConsoleKeyInfo keyInfo;
            while (true)
            {
                keyInfo = Console.ReadKey(true);
                if (keyInfo.KeyChar == '0')
                {
                    block = BlockFactory.CreateBlock(0);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '1')
                {
                    block = BlockFactory.CreateBlock(1);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '2')
                {
                    block = BlockFactory.CreateBlock(2);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '3')
                {
                    block = BlockFactory.CreateBlock(3);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '4')
                {
                    block = BlockFactory.CreateBlock(4);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '5')
                {
                    block = BlockFactory.CreateBlock(5);
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '.')
                {
                    block.Change();
                    printBlock(block);
                }
                else if (keyInfo.KeyChar == '6')
                {
                    Console.Clear();
                }
                else
                {
                    break;
                }
            }
        }
        static void printBlock(Block block)
        {
            int[][] data = block.GetData();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if(data[i][j] == 0)
                        Console.Write(" ");
                    else
                        Console.Write("■");
                }
                Console.WriteLine();
            }
        }
    }
}

类结构图

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值