代码改变世界

设计模式中的聚合和组合

2020-10-20 16:17  石吴玉  阅读(580)  评论(0编辑  收藏  举报

聚合:整体与部分之前的弱关系,部分不会随着整体生命周期的结束而结束。eg:电脑(整体)与 鼠标(部分)

has -a 关系,父类包含子类,但子类可以独立存在

组合:整体与部分之间的强关系,部分随着整体生命周期的结束而结束(部分不能脱离整体单独存在)。 eg: 鸟(整体)与 翅膀(部分)

part-of 关系,父类拥有子类,子类不能独立与父类存在

 

一组关联关系是聚合还是组合,要根据实际业务来看,两个类,在不同的场景中可以有不同的关联关系

代码案例

(1)聚合关系

namespace TestCore
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Name testName = new Name() { ChineseName = "小明" };
            Address testAddress = new Address() { HomeAddress ="地球" };
            Student testStudent = new Student(testName, testAddress);
            Console.WriteLine("更改前的名称:" + testStudent._name.ChineseName);
            Console.WriteLine("更改前的地址:" + testStudent._address.HomeAddress);
            Console.WriteLine("开始更改 testName 与 testAddress 的值-------------");
            testName.ChineseName = "小明(更改后)";
            testAddress.HomeAddress = "地球(更改后)";
            Console.WriteLine("更改后的名称:" + testStudent._name.ChineseName);
            Console.WriteLine("更改后的地址:" + testStudent._address.HomeAddress);
        }
    }

    public class Student {
        public Name _name;
        public Address _address;
        /*
         此处Name和Address与Student是聚合关系,子类Name与Address的生命周期不会随着Student的生命周期结束而结束
        且此处Name与Address在Student中出现是以地址的形式,并未开辟新的存储空间,而是将现有的对象的地址传递给了Student类的属性,当外界name对象与address对象发生变化时,Student的这两个属性会随之变换
         */
        public Student(Name name, Address address)
        {
            this._name = name;
            this._address = address;
        }
    }
    public class Name{
        public string ChineseName { get; set; }
    }

    public class Address { 
        public string HomeAddress { get; set; }
    }
}
View Code

运行结果:

 

 

(2)组合关系

namespace TestCore
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Name testName = new Name() { ChineseName = "小明" };
            Address testAddress = new Address() { HomeAddress ="地球" };
            Student testStudent = new Student(testName, testAddress);
            Console.WriteLine("更改前的名称:" + testStudent._name.ChineseName);
            Console.WriteLine("更改前的地址:" + testStudent._address.HomeAddress);
            Console.WriteLine("开始更改 testName 与 testAddress 的值-------------");
            testName.ChineseName = "小明(更改后)";
            testAddress.HomeAddress = "地球(更改后)";
            Console.WriteLine("更改后的名称:" + testStudent._name.ChineseName);
            Console.WriteLine("更改后的地址:" + testStudent._address.HomeAddress);
        }
    }

    public class Student {
        public Name _name;
        public Address _address;
        /*
         此处Name和Address与Student是组合关系,Name与Address在Student中开辟了新的存储空间,传给Student的不是一个地址,而是一个独立的对象,这个对象和外部的testName、testAddress已经没有关系,不会随之改变
         */
        public Student(Name name, Address address)
        {
            this._name = new Name() { ChineseName = name.ChineseName};
            this._address = new Address() { HomeAddress = address.HomeAddress};
        }
    }
    public class Name{
        public string ChineseName { get; set; }
    }

    public class Address { 
        public string HomeAddress { get; set; }
    }
}
View Code

 

运行结果: