コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("--------------------CompositePattern--------------------");
            Console.WriteLine();

            //透明式
            Folder myWord = new Word();

            myWord.Open();
            myWord.Add(new SonFolder());
            myWord.Remove(new SonFolder());

            Folder myFolder = new SonFolder();

            myFolder.Open();
            myFolder.Add(new SonFolder());
            myFolder.Remove(new SonFolder());//透明性使用统一的类即可完成,但需要处理叶子节点中不需要的方法
            Console.WriteLine("--------------------------------------------------------");

            //安全式
            Safety.Folder excel = new Safety.Excel();
            excel.Open();

            Safety.Folder myFolder1 = new Safety.NextFolder();
            myFolder1.Open();
            ((Safety.SonFolder)myFolder1).Add(new Safety.NextFolder());//安全模式需要处理类之间的关系,无需处理叶子节点中无关的方法,因为叶子节点中不存在无关的方法
            ((Safety.SonFolder)myFolder1).Remove(new Safety.NextFolder());
            Console.WriteLine("--------------------------------------------------------");
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Folder myword = new Word();

            myword.Open();//打开文件,处理文件
            try
            {
                myword.Add(new SonFolder());    //抛出异常
                myword.Remove(new SonFolder()); //抛出异常
            }
            catch (Exception)
            {
                //
            }


            Folder myfolder = new SonFolder();

            myfolder.Open();                  //打开文件夹

            myfolder.Add(new SonFolder());    //成功增加文件或者文件夹
            myfolder.Remove(new SonFolder()); //成功删除文件或者文件夹



            SafeFolder safeFolder = new SafeWord();

            safeFolder.Open();

            SafeFolder mySafeFolder = new NextSafeFolder();

            mySafeFolder.Open();

            //此处要是用增加和删除功能,需要转型的操作,否则不能使用
            ((SonSafeFolder)(mySafeFolder)).Add(new NextSafeFolder());
            ((SonSafeFolder)(mySafeFolder)).Remove(new NextSafeFolder());


            Console.ReadKey();
        }