Design Patterns
工厂模式
工厂模式,定义一个创建对象的接口,让子类决定实现哪一个类
使一个类的实例化延迟到其子类
/// <summary>
/// Factory Pattern
/// 工厂模式,定义一个创建对象的接口,让子类决定实现哪一个类
/// 使一个类的实例化延迟到其子类
/// 简单工厂模式不符合开放封闭原则,即对拓展开放,对修改封闭
/// </summary>
namespace _02_FactoryPattern
{
/// <summary>
/// Opreation Class 运算类
/// </summary>
class Operation
{
private double _optionA;
private double _optionB;
public double OptionA
{
get { return _optionA; }
set { _optionA = value; }
}
public double OptionB
{
get { return _optionB; }
set { _optionB = value; }
}
/// <summary>
/// Calculations
/// </summary>
public virtual double GetResult()
{
double result = 0;
return result;
}
}
/// <summary>
/// Addtion Class
/// </summary>
class OperationAdd : Operation
{
public override double GetResult()
{
double result = OptionA + OptionB;
return result;
}
}
/// <summary>
/// Sub Class
/// </summary>
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = OptionA - OptionB;
return result;
}
}
/// <summary>
/// Factory Interface
/// </summary>
interface IFactory
{
Operation CreateOperation();
}
/// <summary>
/// Add Factory
/// </summary>
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
/// <summary>
/// Sub Factory
/// </summary>
class SubFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
class Program
{
static void Main(string[] args)
{
//生成一个减的工厂
IFactory factory = new SubFactory();
//拿到这个工厂的实例
Operation oper = factory.CreateOperation();
//计算
oper.OptionA = 1;
oper.OptionB = 3;
double result = oper.GetResult();
WriteLine(result.ToString());
ReadKey();
}
}
}
抽象工厂模式
提供一个创建一系列相关或者相互依赖对象的接口,而无需指定他们具体的类
/// <summary>
/// 抽象工厂模式
/// 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定他们具体的类
/// </summary>
namespace _04_AbstractFactoryPattern
{
/// <summary>
/// 用户类
/// </summary>
class User
{
private int _id;
private string _name;
public int Id { get => _id; set => _id = value; }
public string Name { get => _name; set => _name = value; }
}
/// <summary>
/// IUser接口,用于客户端的访问,解除与具体数据库访问的耦合
/// </summary>
interface IUser
{
void Insert(User user);
User GetUser(int id);
}
/// <summary>
/// Sql Server类
/// </summary>
class SqlServerUser : IUser
{
public void Insert(User user)
{
Console.WriteLine("在Sql Server数据库中添加了一个user");
}
public User GetUser(int id)
{
Console.WriteLine("在Sql Server数据库中根据ID拿到了一个User");
return null;
}
}
/// <summary>
/// 修改程序为Access数据库访问
/// </summary>
class AccessUser : IUser
{
public void Insert(User user)
{
Console.WriteLine("Access数据库中添加了一个user");
}
public User GetUser(int id)
{
Console.WriteLine("Access数据库中根据ID拿到了一个User");
return null;
}
}
interface IFactory
{
IUser CreaterUser();
}
class SqlFactory : IFactory
{
public IUser CreaterUser()
{
return new SqlServerUser();
}
}
class AccessFactory : IFactory
{
public IUser CreaterUser()
{
return new AccessUser();
}
}
class Program
{
static void Main(string[] args)
{
User user = new User();
IFactory factory = new AccessFactory();
IUser iUser = factory.CreaterUser();
iUser.Insert(user);
iUser.GetUser(1);
Console.ReadKey();
}
}
}
转载于:https://www.cnblogs.com/AbstractLee/p/8621705.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101756.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...