概述
我们一般为一个类提供构造函数,并利用这个构造函数完成对象的创建工作。当客户知道为哪个类创建实例,并知道构造函数的参数时(假设是用带参数的构造函数)。然而由于条件的现限制,是无法用通常的方式来构造对象的实例! 在进行对象构造之前,要逐步收集与构造相关的信息.那么创建对象的过程和对象的表现就应该分离开来。此时对象的表现是相对稳定的。
应用场景是当创建一个复杂的对象时,这个对象由各个子对象构成,而由于需求的变化导致子对象的性质也是未确定的。
using
System;
using System.Collections.Generic;
using System.Text;
namespace BuilderPattern
... {
/**//// <summary>
/// 所有课程
/// </summary>
public class Coures
...{
/**//// <summary>
/// 保存课程信息
/// </summary>
IDictionary<string, decimal> coures = new Dictionary<string, decimal>();
/**//// <summary>
/// 选课
/// </summary>
/// <param name="serialNumber"></param>
/// <param name="score"></param>
public void Select(string serialNumber, decimal score)
...{
coures.Add(serialNumber, score);
}
/**//// <summary>
/// 计算总学分
/// </summary>
/// <returns>总学分</returns>
public decimal ComputeScores()
...{
decimal scores = 0;
foreach (KeyValuePair<string, decimal> kvp in coures)
...{
Console.WriteLine(kvp.Key + ":" + kvp.Value);
Console.WriteLine(" ");
scores += kvp.Value;
}
return scores;
}
}
/**//// <summary>
/// 选课建造者抽象类,表现(下面的抽象函数)是相对稳定的。
/// </summary>
public abstract class Builder
...{
/**//// <summary>
/// 必修课程成绩
/// </summary>
public abstract void BuildCompulsory();
/**//// <summary>
/// 选修课程成绩
/// </summary>
public abstract void BuildElective();
/**//// <summary>
/// 限制课程成绩
/// </summary>
public abstract void BuildRestriction();
/**//// <summary>
/// 获得课程字典对象
/// </summary>
/// <returns></returns>
public abstract Coures GetCoures();
}
/**//// <summary>
/// 本科生选课成绩
/// </summary>
public class UndergraduateBuilder : Builder
...{
private Coures coures = new Coures();
/**//// <summary>
/// 本科生选必修课程成绩
/// </summary>
public override void BuildCompulsory()
...{
coures.Select("001", 80M);
}
/**//// <summary>
/// 本科生选选修课程成绩
/// </summary>
public override void BuildElective()
...{
coures.Select("101", 85M);
}
/**//// <summary>
/// 本科生选限制课程成绩
/// </summary>
public override void BuildRestriction()
...{
coures.Select("201", 95M);
}
/**//// <summary>
/// 本科生选获得课程字典对象
/// </summary>
/// <returns></returns>
public override Coures GetCoures()
...{
return coures;
}
}
/**//// <summary>
/// 研究生选课成绩
/// </summary>
public class GraduateBuilder : Builder
...{
private Coures coures = new Coures();
/**//// <summary>
/// 研究生选必修课程成绩
/// </summary>
public override void BuildCompulsory()
...{
coures.Select("S001", 70M);
}
/**//// <summary>
/// 研究生选选修课程成绩
/// </summary>
public override void BuildElective()
...{
coures.Select("S101", 75M);
}
/**//// <summary>
/// 研究生选限制课程成绩
/// </summary>
public override void BuildRestriction()
...{
coures.Select("S201", 80M);
}
/**//// <summary>
/// 研究生选获得课程字典对象
/// </summary>
/// <returns></returns>
public override Coures GetCoures()
...{
return coures;
}
}
/**//// <summary>
/// 课程成绩管理类
/// </summary>
public class CourseScoresManager
...{
/**//// <summary>
/// 这里的参数便是未确定的
/// </summary>
/// <param name="builder"></param>
public void CourseScoresManager(Builder builder)
...{
builder.BuildCompulsory();
builder.BuildElective();
builder.BuildRestriction();
}
}
class Program
...{
static void Main(string[] args)
...{
/**//* Builder instance = new UndergraduateBuilder(); */
Builder instance = new GraduateBuilder();
CourseScoresManager courseScoresManager = new CourseScoresManager(instance);
Coures coures = instance.GetCoures();
decimal totalScores = coures.ComputeScores();
Console.WriteLine(totalScores);
Console.WriteLine(" ");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BuilderPattern
... {
/**//// <summary>
/// 所有课程
/// </summary>
public class Coures
...{
/**//// <summary>
/// 保存课程信息
/// </summary>
IDictionary<string, decimal> coures = new Dictionary<string, decimal>();
/**//// <summary>
/// 选课
/// </summary>
/// <param name="serialNumber"></param>
/// <param name="score"></param>
public void Select(string serialNumber, decimal score)
...{
coures.Add(serialNumber, score);
}
/**//// <summary>
/// 计算总学分
/// </summary>
/// <returns>总学分</returns>
public decimal ComputeScores()
...{
decimal scores = 0;
foreach (KeyValuePair<string, decimal> kvp in coures)
...{
Console.WriteLine(kvp.Key + ":" + kvp.Value);
Console.WriteLine(" ");
scores += kvp.Value;
}
return scores;
}
}
/**//// <summary>
/// 选课建造者抽象类,表现(下面的抽象函数)是相对稳定的。
/// </summary>
public abstract class Builder
...{
/**//// <summary>
/// 必修课程成绩
/// </summary>
public abstract void BuildCompulsory();
/**//// <summary>
/// 选修课程成绩
/// </summary>
public abstract void BuildElective();
/**//// <summary>
/// 限制课程成绩
/// </summary>
public abstract void BuildRestriction();
/**//// <summary>
/// 获得课程字典对象
/// </summary>
/// <returns></returns>
public abstract Coures GetCoures();
}
/**//// <summary>
/// 本科生选课成绩
/// </summary>
public class UndergraduateBuilder : Builder
...{
private Coures coures = new Coures();
/**//// <summary>
/// 本科生选必修课程成绩
/// </summary>
public override void BuildCompulsory()
...{
coures.Select("001", 80M);
}
/**//// <summary>
/// 本科生选选修课程成绩
/// </summary>
public override void BuildElective()
...{
coures.Select("101", 85M);
}
/**//// <summary>
/// 本科生选限制课程成绩
/// </summary>
public override void BuildRestriction()
...{
coures.Select("201", 95M);
}
/**//// <summary>
/// 本科生选获得课程字典对象
/// </summary>
/// <returns></returns>
public override Coures GetCoures()
...{
return coures;
}
}
/**//// <summary>
/// 研究生选课成绩
/// </summary>
public class GraduateBuilder : Builder
...{
private Coures coures = new Coures();
/**//// <summary>
/// 研究生选必修课程成绩
/// </summary>
public override void BuildCompulsory()
...{
coures.Select("S001", 70M);
}
/**//// <summary>
/// 研究生选选修课程成绩
/// </summary>
public override void BuildElective()
...{
coures.Select("S101", 75M);
}
/**//// <summary>
/// 研究生选限制课程成绩
/// </summary>
public override void BuildRestriction()
...{
coures.Select("S201", 80M);
}
/**//// <summary>
/// 研究生选获得课程字典对象
/// </summary>
/// <returns></returns>
public override Coures GetCoures()
...{
return coures;
}
}
/**//// <summary>
/// 课程成绩管理类
/// </summary>
public class CourseScoresManager
...{
/**//// <summary>
/// 这里的参数便是未确定的
/// </summary>
/// <param name="builder"></param>
public void CourseScoresManager(Builder builder)
...{
builder.BuildCompulsory();
builder.BuildElective();
builder.BuildRestriction();
}
}
class Program
...{
static void Main(string[] args)
...{
/**//* Builder instance = new UndergraduateBuilder(); */
Builder instance = new GraduateBuilder();
CourseScoresManager courseScoresManager = new CourseScoresManager(instance);
Coures coures = instance.GetCoures();
decimal totalScores = coures.ComputeScores();
Console.WriteLine(totalScores);
Console.WriteLine(" ");
Console.ReadLine();
}
}
}
最后
以上就是大意机器猫为你收集整理的C# 设计模式之生成器模式的全部内容,希望文章能够帮你解决C# 设计模式之生成器模式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复