边看书边做边发挥-图书软件-3

每一章应该有名称,包含许多小节,小节又可以嵌套小节。每个小节是一段内容和一些示例代码,所以 Chapter 类除了 Sections 属性,还应该有名称属性,如果名称不初始化,就使用默认名称,现在修改 Chapter 类,添加这些成员。构造函数可以初始这些名字和 sections 字段,在给 sections 字段初始化时使用 IEnumerable 类型,这样可以更广泛的接收参数。

    public class Chapter : INotifyPropertyChanged
    {
        private string name = "未命名的章节";
        private ObservableCollection<Section> sections = new ObservableCollection<Section>();

        public Chapter()
        {

        }

        public Chapter(string name)
        {
            this.name = name;
        }


        public Chapter(string name, IEnumerable<Section> collection)
            : this(name)
        {
            sections = new ObservableCollection<Section>(collection);
        }

        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }

        ...其他代码...
    }

在 FrameworkDesignGuidelinesBook 类的构造函数修改 catalogue 对象的Add()方法的实参,初始化每一章的类时使用名称和小节集合

    public class FrameworkDesignGuidelinesBook : INotifyPropertyChanged
    {
        private Catalogue catalogue = new Catalogue();

        public FrameworkDesignGuidelinesBook()
        {
            catalogue.Add(new Chapter("概述", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("框架设计基础", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("命名规范", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("类型设计规范", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("成员设计", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("扩展性设计", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("异常", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("使用规范", new ObservableCollection<Section>()));
            catalogue.Add(new Chapter("常用设计模式", new ObservableCollection<Section>()));
        }
        ...其他代码...
    }

还缺少一些信息没有添加,就是节的名称和节的嵌套信息,现在只是用一个空集合初始化,这是因为 Section 类还没有任何成员。章节也应该包含一些章数,比如第一章,第二章,这可以给 Chapter 类再添加一些成员,表示章数,但现在还不需要。Section 类添加如下成员,名称,页码以及嵌套节点属性,构造函数使用一些重载来方便初始化成员,并且可以使用 this 关键字对自身的调用,从而减少输入量,Section 类也要实现通知机制,也要添加 OnPropertyChanged()、 SetProperty() 内部方法

    public class Section : INotifyPropertyChanged
    {
        private string name;
        private int page;
        private Section nestSection;

        public Section()
        {

        }

        public Section(string name, int page)
        {
            this.name = name;
            this.page = page;
        }

        public Section(string name, int page, Section nsetSection)
            : this(name, page)
        {
            this.nestSection = nsetSection;
        }

        public string Name{...}

        public int Page{...}

        public Section NestSection{...}

        ...其他成员...
    }

这里 Section 类的 NestSection 属性是一个错误,他的子节点应该是一个集合,因为子节点可以有许多同级的节点,而这节点又有自己的子节点集合,可以无限的嵌套,取决于这个目录的深度,因为目录本身就是一颗树,所以使用一个链表集合是非常合适的,但是我之前的代码就白费了,尽管如此,还是有继续的意义,因为自己亲自亲为实现自己的类,可以获得宝贵的学习经验。同时,章节类 Chapter 类其实也是完全可以用 Section 类代替,尽管如此,为章节和节使用不同的类还是有一点意义,可以强类型化这两个专属的对象,分别完全控制这两个不同的类的行为。如果章和节有共同的行为,使用同一个类,或者使用链表总是最好的选择,虽然写到这里发现了最好的方法,但我仍然完成当前的代码,之后我再使用一个链表,一个节的类就完成了这个目的,于是,便有了两种选择,初始化一本书。现在修改上面的 NestSection 属性和构造函数,与 Chapter 类相差无几

    public class Section : INotifyPropertyChanged
    {
        private string name;
        private int page;
        private ObservableCollection<Section> sections;

        public Section()
        {

        }

        public Section(string name, int page)
        {
            this.name = name;
            this.page = page;
        }

        public Section(string name, int page, IEnumerable<Section> collection)
            : this(name, page)
        {
            this.sections = new ObservableCollection<Section>(collection);
        }

        public string Name
        {...}

        public int Page
        {...}

        public ObservableCollection<Section> Sections
        {
            get { return sections; }
            set { SetProperty(ref sections, value); }
        }
        ...其他成员...
    }

回到 FrameworkDesignGuidelinesBook 类,修改构造函数的代码,添加目录信息,由于 Section 类可以嵌套信息,所以Add方法比较长,换行即可。注意这里使用集合初始化器时可以不加括号


    public class FrameworkDesignGuidelinesBook : INotifyPropertyChanged
    {
        private Catalogue catalogue = new Catalogue();

        public FrameworkDesignGuidelinesBook()
        {
            catalogue.Add(
                new Chapter("概述",
                    new ObservableCollection<Section> 
                    { 
                        new Section("精心设计的框架所具备的品质", 2,
                            new ObservableCollection<Section>
                            {
                                new Section("精心设计的框架所具备的品质",2),
                                new Section("精心设计的框架是简单的",2),
                                new Section("精心设计的框架代价高",3),
                                new Section("精心设计的框架充满利弊权衡",4),
                                new Section("精心设计的框架应该借鉴过去的经验",4),
                                new Section("精心设计的框架要考虑未来的发展",5),
                                new Section("精心设计的框架应具有良好的集成性",5),
                                new Section("精心设计的框架是一致的",7),
                            }) 
                    }));

             catalogue.Add(
                new Chapter("框架设计基础",
                    new ObservableCollection<Section> 
                    { 
                        new Section("渐进的框架", 9), 
                        new Section("框架的基本设计原则", 12
                            new ObservableCollection<Section>
                            {
                            ...其他小节信息...
                            }) 
                    }));
            }
            ...其他章...
     ...其他成员...
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值