【设计模式学习笔记】设计模式-开始

面向对象编程的好处:可维护、易扩展、可复用、灵活性好

UML类图常用标记

字段、方法与属性

【设计模式学习笔记】设计模式-开始_第1张图片
对应代码:

class ClassName
    {
        //字段
        public int field_1;
        protected float field_2;
        private string field_3;
        //属性
        public int Property {
            get;
            set;
        }
        //方法
        public float Function(int parameter_1,float parameter_2) {
            return parameter_1 + parameter_2;
        }
    }

注意:当类名为斜体时类为抽象类

继承、实现接口

【设计模式学习笔记】设计模式-开始_第2张图片

对应代码:

class Subclass : ClassName, Interface1 {
        public void InterfaceFun() {
            Console.WriteLine("接口实现");
        }
    }
interface Interface1 {
        void InterfaceFun();
    }

关联、聚合、组合、依赖

【设计模式学习笔记】设计模式-开始_第3张图片

对应代码:

class RelateClass {
        private ClassName _className;
    }

【设计模式学习笔记】设计模式-开始_第4张图片

对应代码:

class 雁群 {
        private 大雁[] _dayan;
    }

【设计模式学习笔记】设计模式-开始_第5张图片

对应代码:

class Bird
{
    private Wing wing;
    public Bird()
    {
        wing = new wing();
    }
}

依赖关系:
【设计模式学习笔记】设计模式-开始_第6张图片
对应代码:

class Animal { public Animal(Oxygen oxygen,Water water) { } }

参考:
《大化设计模式》 程杰 http://cj723.cnblogs.com/

你可能感兴趣的:(DesignPattern)