zoukankan      html  css  js  c++  java
  • 原型模式

    静态代码是没有模式的,与时间结合运动起来才能体现出来程序的立体感

    什么是原型模式?

    就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建细节

    UML图如下

    原型深入去看,就是特殊的工厂,

    从原型模式延伸出来,讨论的一些其他话题

    原型模式实现了对象的自身再创造,涉及有浅复制,深复制

    浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有的其他对象的引用仍然指向原来的对象

    深复制:把引用对象的变量指向复制过的新对象,而不是原有被引用 的对象

    为什么要有原型模式呢

    相似或者相同的对象需要多个初始化的时候,一个个去初始化,传值不担容易出错,还繁琐

    直接New对象耦合高

    原型模式是怎么解决这些问题呢

    将对象的复制操作,由对象自身来完成,对于调用方屏蔽了构造细节,降低耦合点

    以下是示例代码:

    class Resume:ICloneable
        {
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private int age;
    
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string sex;
    
            public string Sex
            {
                get { return sex; }
                set { sex = value; }
            }
            private string timeArea;
    
            public string TimeArea
            {
                get { return timeArea; }
                set { timeArea = value; }
            }
            private string company;
    
            public string Company
            {
                get { return company; }
                set { company = value; }
            }
            private WorkExpression wok;
    
            public WorkExpression Wok
            {
                get { return wok; }
                set { wok = value; }
            }
            public Resume(string name)
            {
                this.name = name;
                wok = new WorkExpression();
               
                
    
            }
            //设置构造函数
            public Resume(WorkExpression wok)
            {
                this.wok = (WorkExpression)wok.Clone();
            }
            //设置个人信息
            public void SetPersonInfo(string sex,int age)
            {
                this.age = age;
                this.sex = sex;
     
            }
            public void SetWorkExpression(string timeArea,string company)
            {
                  wok.WorkArea=timeArea;
                 wok.Company=company;
     
            }//显示个人信息
            public void Disply()
            {
                Console.WriteLine("{0},{1},{2}",name,age,sex);
                Console.WriteLine("工作经历:{0}{1}",wok.WorkArea,wok.Company);
            }
    
            //实现接口行为
            #region ICloneable 成员
    
            public object Clone()
            {
                Resume re = new Resume(this.wok);
                re.name = this.name;
                re.sex = sex;
                re.age = age;
                return re;
            }
    
            #endregion
        }
     class WorkExpression:ICloneable
        {
            //工作地点
            private string workArea;
    
            public string WorkArea
            {
                get { return workArea; }
                set { workArea = value; }
            }
            private string company;
    
            public string Company
            {
                get { return company; }
                set { company = value; }
            }
    
    
            #region ICloneable 成员
    
            public object Clone()
            {
                return this.MemberwiseClone();
            }
    
            #endregion
        }
    static void Main(string[] args)
            {
                Resume re = new Resume("gdnyfcuso");
                re.SetPersonInfo("",24);
                re.SetWorkExpression("xingxinli","2012-3");
                re.Disply();
                Resume ra = (Resume)re.Clone();
                ra.SetWorkExpression("xing","201321");
                ra.Disply();
                Resume rb=(Resume)ra.Clone();
                rb.SetWorkExpression("xing", "2   01321");
                rb.Disply();
                Console.Read();
            }
  • 相关阅读:
    LeetCode15 3Sum
    LeetCode10 Regular Expression Matching
    LeetCode20 Valid Parentheses
    LeetCode21 Merge Two Sorted Lists
    LeetCode13 Roman to Integer
    LeetCode12 Integer to Roman
    LeetCode11 Container With Most Water
    LeetCode19 Remove Nth Node From End of List
    LeetCode14 Longest Common Prefix
    LeetCode9 Palindrome Number
  • 原文地址:https://www.cnblogs.com/gdnyfcuso/p/6676352.html
Copyright © 2011-2022 走看看