乐在其中设计形式(C#) – 抽象工厂方式(Abstract Factory Pattern)

[索引页] [源码下载]

乐在其中设计方式(C#) – 抽象工厂方式(Abstract Factory Pattern)

作者:webabcd

介绍
提供一个创设一多级相关或相互正视对象的接口,而无需点名它们具体的类。

示例
有Message和MessageModel,Message有贰个Insert()方法,该办法的参数是MessageModel。
C# 1

AbstractMessageModel

C# 2using System;
C# 3using System.Collections.Generic;
C# 4using System.Text;
C# 5
C# 6namespace Pattern.AbstractFactory
C# 7C# 8C# 9{
C# 10C# 11    /**//// <summary>
C# 12    /// 抽象Message实体类(AbstractProduct)
C# 13    /// </summary>
C# 14    public abstract class AbstractMessageModel
C# 15C# 16    C# 17{
C# 18C# 19        /**//// <summary>
C# 20        /// 构造函数
C# 21        /// </summary>
C# 22        public AbstractMessageModel()
C# 23C# 24        C# 25{
C# 26
C# 27        }
C# 28
C# 29C# 30        /**//// <summary>
C# 31        /// 构造函数
C# 32        /// </summary>
C# 33        /// <param name=”msg”>Message内容</param>
C# 34        /// <param name=”pt”>Message公布时间</param>
C# 35        public AbstractMessageModel(string msg, DateTime pt)
C# 36C# 37        C# 38{
C# 39            this._message = msg;
C# 40            this._publishTime = pt;
C# 41        }
C# 42
C# 43        private string _message;
C# 44C# 45        /**//// <summary>
C# 46        /// Message内容
C# 47        /// </summary>
C# 48        public string Message
C# 49C# 50        C# 51{
C# 52C# 53            get C# 54{ return _message; }
C# 55C# 56            set C# 57{ _message = value; }
C# 58        }
C# 59
C# 60        private DateTime _publishTime;
C# 61C# 62        /**//// <summary>
C# 63        /// Message发布时间
C# 64        /// </summary>
C# 65        public DateTime PublishTime
C# 66C# 67        C# 68{
C# 69C# 70            get C# 71{ return _publishTime; }
C# 72C# 73            set C# 74{ _publishTime = value; }
C# 75        }
C# 76
C# 77C# 78        /**//// <summary>
C# 79        /// UserId
C# 80        /// </summary>
C# 81        public abstract string UserId
C# 82C# 83        C# 84{
C# 85            get;
C# 86            set;
C# 87        }
C# 88    }
C# 89}
C# 90

SqlMessageModel

C# 91using System;
C# 92using System.Collections.Generic;
C# 93using System.Text;
C# 94
C# 95namespace Pattern.AbstractFactory
C# 96C# 97C# 98{
C# 99C# 100    /**//// <summary>
C# 101    /// SqlMessage实体类(Product)
C# 102    /// </summary>
C# 103    public class SqlMessageModel : AbstractMessageModel
C# 104C# 105    C# 106{
C# 107C# 108        /**//// <summary>
C# 109        /// 构造函数
C# 110        /// </summary>
C# 111        public SqlMessageModel()
C# 112            : base()
C# 113C# 114        C# 115{
C# 116
C# 117        }
C# 118
C# 119C# 120        /**//// <summary>
C# 121        /// 构造函数
C# 122        /// </summary>
C# 123        /// <param name=”userId”>UserId</param>
C# 124        /// <param name=”msg”>Message内容</param>
C# 125        /// <param name=”pt”>Message发布时间</param>
C# 126        public SqlMessageModel(string userId, string msg, DateTime pt)
C# 127            : base(msg, pt)
C# 128C# 129        C# 130{
C# 131            this._userId = userId;
C# 132        }
C# 133
C# 134        private string _userId;
C# 135C# 136        /**//// <summary>
C# 137        /// Message内容
C# 138        /// </summary>
C# 139        public override string UserId
C# 140C# 141        C# 142{
C# 143C# 144            get C# 145{ return _userId; }
C# 146C# 147            set C# 148{ _userId = value; }
C# 149        }
C# 150    }
C# 151}
C# 152

XmlMessageModel

C# 153using System;
C# 154using System.Collections.Generic;
C# 155using System.Text;
C# 156
C# 157namespace Pattern.AbstractFactory
C# 158C# 159C# 160{
C# 161C# 162    /**//// <summary>
C# 163    /// XmlMessage实体类(Product)
C# 164    /// </summary>
C# 165    public class XmlMessageModel : AbstractMessageModel
C# 166C# 167    C# 168{
C# 169C# 170        /**//// <summary>
C# 171        /// 构造函数
C# 172        /// </summary>
C# 173        public XmlMessageModel()
C# 174            : base()
C# 175C# 176        C# 177{
C# 178
C# 179        }
C# 180
C# 181C# 182        /**//// <summary>
C# 183        /// 构造函数
C# 184        /// </summary>
C# 185        /// <param name=”userId”>UserId</param>
C# 186        /// <param name=”msg”>Message内容</param>
C# 187        /// <param name=”pt”>Message揭橥时间</param>
C# 188        public XmlMessageModel(string userId, string msg, DateTime pt)
C# 189            : base (msg, pt)
C# 190C# 191        C# 192{
C# 193            this._userId = userId;
C# 194        }
C# 195
C# 196        private string _userId;
C# 197C# 198        /**//// <summary>
C# 199        /// Message内容
C# 200        /// </summary>
C# 201        public override string UserId
C# 202C# 203        C# 204{
C# 205            // 解密算法后的值
C# 206C# 207            get C# 208{ return _userId; } 
C# 209
C# 210            // 加密算法后的值
C# 211C# 212            set C# 213{ _userId = value; }
C# 214        }
C# 215    }
C# 216}
C# 217

AbstractMessage

C# 218using System;
C# 219using System.Collections.Generic;
C# 220using System.Text;
C# 221
C# 222namespace Pattern.AbstractFactory
C# 223C# 224C# 225{
C# 226C# 227    /**//// <summary>
C# 228    /// 操作Message抽象类(AbstractProduct)
C# 229    /// </summary>
C# 230    public abstract class AbstractMessage
C# 231C# 232    C# 233{
C# 234C# 235        /**//// <summary>
C# 236        /// 插入Message
C# 237        /// </summary>
C# 238        /// <param name=”amm”>AbstractMessageModel</param>
C# 239        /// <returns></returns>
C# 240        public abstract string Insert(AbstractMessageModel amm);
C# 241    }
C# 242}
C# 243

SqlMessage

C# 244using System;
C# 245using System.Collections.Generic;
C# 246using System.Text;
C# 247
C# 248namespace Pattern.AbstractFactory
C# 249C# 250C# 251{
C# 252C# 253    /**//// <summary>
C# 254    /// Sql格局操作Message类(Product)
C# 255    /// </summary>
C# 256    public class SqlMessage : AbstractMessage
C# 257C# 258    C# 259{
C# 260C# 261        /**//// <summary>
C# 262        /// 插入Message
C# 263        /// </summary>
C# 264        /// <param name=”amm”>AbstractMessageModel</param>
C# 265        /// <returns></returns>
C# 266        public override string Insert(AbstractMessageModel amm)
C# 267C# 268        C# 269{
C# 270            return “Sql方式插入Message。帐号:” + amm.UserId 
C# 271                + “;内容:” + amm.Message 
C# 272                + “;时间:” + amm.PublishTime.ToString();
C# 273        }
C# 274    }
C# 275}
C# 276

XmlMessage

C# 277using System;
C# 278using System.Collections.Generic;
C# 279using System.Text;
C# 280
C# 281namespace Pattern.AbstractFactory
C# 282C# 283C# 284{
C# 285C# 286    /**//// <summary>
C# 287    /// Xml方式操作Message类(Product)
C# 288    /// </summary>
C# 289    public class XmlMessage : AbstractMessage
C# 290C# 291    C# 292{
C# 293C# 294        /**//// <summary>
C# 295        /// 插入Message
C# 296        /// </summary>
C# 297        /// <param name=”amm”>AbstractMessageModel</param>
C# 298        /// <returns></returns>
C# 299        public override string Insert(AbstractMessageModel amm)
C# 300C# 301        C# 302{
C# 303            return “Xml格局插入Message。帐号:” + amm.UserId
C# 304                + “;内容:” + amm.Message
C# 305                + “;时间:” + amm.PublishTime.ToString();
C# 306        }
C# 307    }
C# 308}
C# 309

AbstractMessageFactory

C# 310using System;
C# 311using System.Collections.Generic;
C# 312using System.Text;
C# 313
C# 314namespace Pattern.AbstractFactory
C# 315C# 316C# 317{
C# 318C# 319    /**//// <summary>
C# 320    /// 抽象Message工厂(AbstractFactory)
C# 321    /// </summary>
C# 322    public abstract class AbstractMessageFactory
C# 323C# 324    C# 325{
C# 326C# 327        /**//// <summary>
C# 328        /// 创建MessageModel对象
C# 329        /// </summary>
C# 330        /// <returns></returns>
C# 331        public abstract AbstractMessageModel CreateMessageModel();
C# 332
C# 333C# 334        /**//// <summary>
C# 335        /// 创建Message对象
C# 336        /// </summary>
C# 337        /// <returns></returns>
C# 338        public abstract AbstractMessage CreateMessage();
C# 339    }
C# 340}

SqlMessageFactory

C# 341using System;
C# 342using System.Collections.Generic;
C# 343using System.Text;
C# 344
C# 345namespace Pattern.AbstractFactory
C# 346C# 347C# 348{
C# 349C# 350    /**//// <summary>
C# 351    /// SqlMessage工厂(ConcreteFactory)
C# 352    /// </summary>
C# 353    public class SqlMessageFactory : AbstractMessageFactory
C# 354C# 355    C# 356{
C# 357C# 358        /**//// <summary>
C# 359        /// 创建SqlMessageModel对象
C# 360        /// </summary>
C# 361        /// <returns></returns>
C# 362        public override AbstractMessageModel CreateMessageModel()
C# 363C# 364        C# 365{
C# 366            return new SqlMessageModel();
C# 367        }
C# 368
C# 369C# 370        /**//// <summary>
C# 371        /// 创建SqlMessage对象
C# 372        /// </summary>
C# 373        /// <returns></returns>
C# 374        public override AbstractMessage CreateMessage()
C# 375C# 376        C# 377{
C# 378            return new SqlMessage();
C# 379        }
C# 380    }
C# 381}
C# 382

XmlMessageFactory

C# 383using System;
C# 384using System.Collections.Generic;
C# 385using System.Text;
C# 386
C# 387namespace Pattern.AbstractFactory
C# 388C# 389C# 390{
C# 391C# 392    /**//// <summary>
C# 393    /// XmlMessage工厂(ConcreteFactory)
C# 394    /// </summary>
C# 395C#,    public class XmlMessageFactory : AbstractMessageFactory
C# 396C# 397    C# 398{
C# 399C# 400        /**//// <summary>
C# 401        /// 创建XmlMessageModel对象
C# 402        /// </summary>
C# 403        /// <returns></returns>
C# 404        public override AbstractMessageModel CreateMessageModel()
C# 405C# 406        C# 407{
C# 408            return new XmlMessageModel();
C# 409        }
C# 410
C# 411C# 412        /**//// <summary>
C# 413        /// 创建XmlMessage对象
C# 414        /// </summary>
C# 415        /// <returns></returns>
C# 416        public override AbstractMessage CreateMessage()
C# 417C# 418        C# 419{
C# 420            return new XmlMessage();
C# 421        }
C# 422    }
C# 423}
C# 424

Message

C# 425using System;
C# 426using System.Collections.Generic;
C# 427using System.Text;
C# 428
C# 429namespace Pattern.AbstractFactory
C# 430C# 431C# 432{
C# 433C# 434    /**//// <summary>
C# 435    /// Context类
C# 436    /// </summary>
C# 437    public class Message
C# 438C# 439    C# 440{
C# 441        private AbstractMessage _insertMessage;
C# 442        private AbstractMessageModel _messageModel;
C# 443
C# 444C# 445        /**//// <summary>
C# 446        /// 构造函数
C# 447        /// </summary>
C# 448        /// <param name=”factory”>AbstractMessageFactory</param>
C# 449        public Message(AbstractMessageFactory factory)
C# 450C# 451        C# 452{
C# 453            DateTime pt = DateTime.Now;
C# 454
C# 455            _insertMessage = factory.CreateMessage();
C# 456            _messageModel = factory.CreateMessageModel();
C# 457
C# 458        }
C# 459
C# 460C# 461        /**//// <summary>
C# 462        /// 插入Message
C# 463        /// </summary>
C# 464        /// <param name=”userId”>UserId</param>
C# 465        /// <param name=”msg”>Message内容</param>
C# 466        /// <param name=”pt”>Message发表时间</param>
C# 467        /// <returns></returns>
C# 468        public string Insert(string userId, string msg, DateTime pt)
C# 469C# 470        C# 471{
C# 472            _messageModel.UserId = userId;
C# 473            _messageModel.Message = msg;
C# 474            _messageModel.PublishTime = pt;
C# 475
C# 476            return _insertMessage.Insert(_messageModel);
C# 477        }
C# 478    }
C# 479}
C# 480

client

C# 481using System;
C# 482using System.Data;
C# 483using System.Configuration;
C# 484using System.Collections;
C# 485using System.Web;
C# 486using System.Web.Security;
C# 487using System.Web.UI;
C# 488using System.Web.UI.WebControls;
C# 489using System.Web.UI.WebControls.WebParts;
C# 490using System.Web.UI.HtmlControls;
C# 491
C# 492using Pattern.AbstractFactory;
C# 493
C# 494public partial class AbstractFactory : System.Web.UI.Page
C# 495C# 496C# 497{
C# 498    protected void Page_Load(object sender, EventArgs e)
C# 499C# 500    C# 501{
C# 502        AbstractMessageFactory amf = new SqlMessageFactory();
C# 503
C# 504        Message m = new Message(amf);
C# 505
C# 506        Response.Write(m.Insert(“admin”, “Sql方式”, DateTime.Now));
C# 507        Response.Write(“<br />”);
C# 508
C# 509        amf = new XmlMessageFactory();
C# 510
C# 511        m = new Message(amf);
C# 512
C# 513        Response.Write(m.Insert(“user”, “Xml方式”, DateTime.Now));
C# 514    }
C# 515}
C# 516

运作结果
Sql格局插入Message。帐号:admin;内容:Sql方式;时间:二零零七-5-3
22:04:56
Xml格局插入Message。帐号:user;内容:Xml格局;时间:二〇〇六-5-3 22:04:56

参考
http://www.dofactory.com/Patterns/PatternAbstract.aspx

OK
[源码下载]

相关文章