Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这种模式叫什么?_C#_Design Patterns - Fatal编程技术网

C# 这种模式叫什么?

C# 这种模式叫什么?,c#,design-patterns,C#,Design Patterns,当你在一个对象的构造函数中给它一个参数,并且这个对象用这个参数填充它的字段时,这个模式叫什么 例如: /// <summary> /// Represents a user on the Dream.In.Code website. /// </summary> public class User { /// <summary> /// Load a user by providing an ID. /// </su

当你在一个对象的构造函数中给它一个参数,并且这个对象用这个参数填充它的字段时,这个模式叫什么

例如:

/// <summary>
/// Represents a user on the Dream.In.Code website.
/// </summary>
public class User
{       
    /// <summary>
    /// Load a user by providing an ID.
    /// </summary>
    /// <param name="ID">A user's individual ID number.</param>
    public User(string ID)
    {
        WebClient webClient = new WebClient();
        string htmlSource = webClient.DownloadString(new Uri(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}",ID)));
        XDocument xml = XDocument.Parse(htmlSource);

        var profileXML = xml.Element("ipb").Element("profile");

        //Load general profile information.
        this.ID = profileXML.Element("id").Value;
        this.Name = profileXML.Element("name").Value;
        this.Rating = profileXML.Element("rating").Value;
        this.Photo = profileXML.Element("photo").Value;
        this.Reputation = profileXML.Element("reputation").Value;
        this.Group = profileXML.Element("group").Element("span").Value;
        this.Posts = profileXML.Element("posts").Value;
        this.PostsPerDay = profileXML.Element("postsperday").Value;
        this.JoinDate = profileXML.Element("joined").Value;
        this.ProfileViews = profileXML.Element("views").Value;
        this.LastActive = profileXML.Element("lastactive").Value;
        this.Location = profileXML.Element("location").Value;
        this.Title = profileXML.Element("title").Value;
        this.Age = profileXML.Element("age").Value;
        this.Birthday = profileXML.Element("birthday").Value;
        this.Gender = profileXML.Element("gender").Element("gender").Element("value").Value;

        //Load contact information.
        var contactXML = xml.Element("ipb").Element("profile").Element("contactinformation");
        this.AIM = contactXML.XPathSelectElement("contact[title='AIM']/value").Value;
        this.MSN = contactXML.XPathSelectElement("contact[title='MSN']/value").Value;
        this.Website = contactXML.XPathSelectElement("contact[title='Website URL']/value").Value;
        this.ICQ = contactXML.XPathSelectElement("contact[title='ICQ']/value").Value;
        this.Yahoo = contactXML.XPathSelectElement("contact[title='Yahoo']/value").Value;
        this.Jabber = contactXML.XPathSelectElement("contact[title='Jabber']/value").Value;
        this.Skype = contactXML.XPathSelectElement("contact[title='Skype']/value").Value;
        this.LinkedIn = contactXML.XPathSelectElement("contact[title='LinkedIn']/value").Value;
        this.Facebook = contactXML.XPathSelectElement("contact[title='Facebook']/value").Value;
        this.Twitter = contactXML.XPathSelectElement("contact[title='Twitter']/value").Value;
        this.XFire = contactXML.XPathSelectElement("contact[title='Xfire']/value").Value;

        //Load latest visitors.
        var visitorXML = xml.Element("ipb").Element("profile").Element("latestvisitors");
        this.Visitors = (from visitor in visitorXML.Descendants("user")
                        select new Visitor(){
                            ID = visitor.Element("id").Value,
                            Name = visitor.Element("name").Value,
                            Url = visitor.Element("url").Value,
                            Photo = visitor.Element("photo").Value,
                            Visited = visitor.Element("visited").Value,
                        }).ToList();

        //Load friends.
        var friendsXML = xml.Element("ipb").Element("profile").Element("friends");
        this.Friends = (from friend in friendsXML.Descendants("user")
                        select new Friend()
                        {
                            ID = friend.Element("id").Value,
                            Name = friend.Element("name").Value,
                            Url = friend.Element("url").Value,
                            Photo = friend.Element("photo").Value
                        }).ToList();

        //Load comments.
        var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
        this.Comments = (from comment in commentsXML.Descendants("comment")
                            select new Comment()
                            {
                                ID = comment.Element("id").Value,
                                Text = comment.Element("text").Value,
                                Date = comment.Element("date").Value,
                                UserWhoPosted = new Friend()
                                {
                                    ID = comment.Element("user").Element("id").Value,
                                    Name = comment.Element("user").Element("name").Value,
                                    Url = comment.Element("user").Element("url").Value,
                                    Photo = comment.Element("user").Element("photo").Value
                                }
                            }).ToList();
    } 
}
我会:

UserWhoPosted = new Friend(myFriendXElementVariable);
让它解析它需要的内容并填充它的字段

谢谢你的指导

编辑:在阅读了您的所有建议后,我对代码进行了一些清理。你觉得这样更好吗?您会改进什么?

谢谢你花时间来帮助我,我正在努力抛开坏的编程习惯


我想称之为的变体,因为您真正做的只是将一些XML结构包装成更友好的类型。

这不是一种模式。设计模式是解决一个常见问题的东西,例如当值发生变化时如何保持客户机的更新(观察者)。这其实不是问题,只是语言语法问题。第二个问题的答案是肯定的。

在您的示例中,我倾向于将任何必需的数据传递给构造函数,因为它定义了类所需的依赖性


另一个问题是将xml数据传递到构造函数而不是具体类型。具体类型定义了一组已知的属性或字段,其中xml不能保证结构…

对我来说,它更像是一种反模式:在构造函数中包含xml请求、读取和字段初始化。 也许您可以使用工厂模式,如下所示:

public static class FactoryUser{
public static User GetUserFromXml(Xml){
//your code here
}
}

public static class UserWebrequester{
public static Xml GetXmlUser(id){
}
}

也许您可以添加一些单例来对类进行单元测试。

在构造函数中执行如此繁重的操作是不好的做法,我建议您将构造函数设置为私有,并创建将返回用户实例的静态加载方法。我不知道这个模式是如何命名的。

好吧,这不是一个“已知模式”,可能是一个您可以使用的模式(没有问题……模式就是这样,是对已确定问题的常见解决方案)

但是,由于您的用户类负责加载自身,因此我认为该类是作为DAO工作的

正如其他用户所说,构造函数中的逻辑太多了。构造函数应该只具有创建对象所必需的参数,并且尽可能少地进行工作。最好构造函数也避免抛出异常;因为代码越多,发生异常的可能性就越高,对构造函数执行的操作越多,抛出异常的可能性就越高

现在,看看你的课堂。。。我看到你的“用户”有诸如姓名、等级等字段。在用户对象的生命周期内,不允许不填充这些字段吗?在创建新用户时,这可能是真的。因此,对于用户来说,拥有这个ID并不是一个先决条件,根据我所述的规则,这会使构造函数无效

尝试创建一个Load静态方法以返回新用户或创建工厂。它将在将来的可维护性等方面帮助您


最后,试着看一下。您可以使用它而不是普通工厂来维护您的用户和其他对象。

这样做肯定违反了:

D = comment.Element("user").Element("id").Value,
你真的不想有这么长的方法调用链。让朋友从其论点中询问值是一种更好的设计,耦合更少


子部分、朋友、访客。。。不应该给他们做工作。这就分开了你的顾虑。它还使测试更容易,因为您可以存根toplevel参数,而无需深入三个级别以满足构造函数的要求

谢谢大家的回答;这很有趣,但我学的越多,我就越意识到我知道的是多么少P@Matthieu因为您可以注入依赖项并模拟工厂,这在静态类中是不可能的,因为您无法实现接口。这取决于您是否进行了单元测试^。
D = comment.Element("user").Element("id").Value,