C#面向对象设计模式 学习之旅(三) - Abstract Factory 抽象工厂模式(创建型)
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryPattern
{
//Abstract factory pattern
class Program
{
//在这个例子中,使用了面向对象的三大特性:封装、继承、多态。
static void Main(string[] args)
{
int id = 1;
string input = id.ToString();
try
{
while (Convert.ToInt32(input) > 0)
{
Console.WriteLine("please input a number more than 0 ");
input = Console.ReadLine();
id = Convert.ToInt32(input);
Employee e = Factory.GetEmployee(id);
Console.Write(e.GetType().ToString() + " = " + e.GetSalary());
Console.WriteLine();
Console.WriteLine();
}
}
catch
{
}
}
}
/**//// <summary>
/// 员工抽象类
/// </summary>
public abstract class Employee
{
/**//// <summary>
/// 必须重写的抽象方法:获得薪水
/// </summary>
/// <returns></returns>
public abstract int GetSalary();
//public virtual string GetTypeName()
//{
// return "BaseType";
//}
}
/**//// <summary>
/// 工程师类,继承于:员工类
/// </summary>
internal class Engineer : Employee
{
/**//// <summary>
/// 重写的实现方法:获得工程师工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(5000 + 100 * 2);
}
}
/**//// <summary>
/// 销售类,继承于:员工类
/// </summary>
public class Sales : Employee
{
/**//// <summary>
/// 重写的实现方法:获得销售工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(1000 + 500 * 10);
}
}
/**//// <summary>
/// 经理类,继承于:员工类
/// </summary>
class Manager : Employee
{
/**//// <summary>
/// 重写的实现方法:获得经理工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(5000 + 1000 * 10);
}
}
/**//// <summary>
/// 工厂类
/// </summary>
class Factory
{
/**//// <summary>
/// 获得员工:提供一个创建抽象基类的方法
/// </summary>
/// <param name="id">员工编号</param>
/// <returns>员工</returns>
public static Employee GetEmployee(int id)
{
if (id > 80)
{
return (Employee)(new Manager());
}
else if (id > 50)
{
return (Employee)(new Sales());
}
else
{
return (Employee)(new Engineer());
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryPattern
{
//Abstract factory pattern
class Program
{
//在这个例子中,使用了面向对象的三大特性:封装、继承、多态。
static void Main(string[] args)
{
int id = 1;
string input = id.ToString();
try
{
while (Convert.ToInt32(input) > 0)
{
Console.WriteLine("please input a number more than 0 ");
input = Console.ReadLine();
id = Convert.ToInt32(input);
Employee e = Factory.GetEmployee(id);
Console.Write(e.GetType().ToString() + " = " + e.GetSalary());
Console.WriteLine();
Console.WriteLine();
}
}
catch
{
}
}
}
/**//// <summary>
/// 员工抽象类
/// </summary>
public abstract class Employee
{
/**//// <summary>
/// 必须重写的抽象方法:获得薪水
/// </summary>
/// <returns></returns>
public abstract int GetSalary();
//public virtual string GetTypeName()
//{
// return "BaseType";
//}
}
/**//// <summary>
/// 工程师类,继承于:员工类
/// </summary>
internal class Engineer : Employee
{
/**//// <summary>
/// 重写的实现方法:获得工程师工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(5000 + 100 * 2);
}
}
/**//// <summary>
/// 销售类,继承于:员工类
/// </summary>
public class Sales : Employee
{
/**//// <summary>
/// 重写的实现方法:获得销售工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(1000 + 500 * 10);
}
}
/**//// <summary>
/// 经理类,继承于:员工类
/// </summary>
class Manager : Employee
{
/**//// <summary>
/// 重写的实现方法:获得经理工资
/// </summary>
/// <returns>工资</returns>
public override int GetSalary()
{
return Convert.ToInt32(5000 + 1000 * 10);
}
}
/**//// <summary>
/// 工厂类
/// </summary>
class Factory
{
/**//// <summary>
/// 获得员工:提供一个创建抽象基类的方法
/// </summary>
/// <param name="id">员工编号</param>
/// <returns>员工</returns>
public static Employee GetEmployee(int id)
{
if (id > 80)
{
return (Employee)(new Manager());
}
else if (id > 50)
{
return (Employee)(new Sales());
}
else
{
return (Employee)(new Engineer());
}
}
}
}
先放个链接,以后把学的知识整理进来。