每天早上从家里上班到公司门口可以选择的交通工具
常规写法
///
/// 地铁类
///
class metro
{
public metro()
{
Name = "地铁";
merit = "不会堵车";
defect = "距离家很远";
}
public string Name { get; set; }
public string merit { get; set; }
public string defect { get; set; }
}
///
/// 公交类
///
class transit
{
public transit()
{
Name = "公交";
merit = "离家很近";
defect = "会堵车";
}
public string Name { get; set; }
public string merit { get; set; }
public string defect { get; set; }
}
///
/// 爬楼梯类
///
class ascendthestairs
{
public ascendthestairs()
{
Name = "爬楼梯";
merit = "可以锻炼身体";
defect = "很累";
}
public string Name { get; set; }
public string merit { get; set; }
public string defect { get; set; }
}
///
/// 坐电梯
///
class takethelift
{
public takethelift()
{
Name = "坐电梯";
merit = "很快就到了";
defect = "不能锻炼身体";
}
public string Name { get; set; }
public string merit { get; set; }
public string defect { get; set; }
}
上述类分成两种,一种是从家到公司用的交通工具,一种是从公司到办公室用的交通工具。
前台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
transit me = new transit();
takethelift asc = new takethelift();
Console.WriteLine("第一天");
Console.WriteLine("上班乘坐的交通工具是{0},它的优点是{1},缺点是{2}", me.Name, me.merit, me.defect);
Console.WriteLine("来到公司楼下我选择的是{0}到达办公室,它的优点是{1},缺点是{2}", asc.Name, asc.merit, asc.defect);
transit me1 = new transit();
takethelift asc1 = new takethelift();
Console.WriteLine("第二天");
Console.WriteLine("上班乘坐的交通工具是{0},它的优点是{1},缺点是{2}", me1.Name, me1.merit, me1.defect);
Console.WriteLine("来到公司楼下我选择的是{0}到达办公室,它的优点是{1},缺点是{2}", asc1.Name, asc1.merit, asc1.defect);
Console.Read();
}
}
}
用简单工厂来实现:
1.创建一个接口Iduty用于抽象从家到公司的交通工具
public interface IComeUp
{
}
2.创建一个接口IComeUp用于抽象公司到办公室的交通工具
public interface Iduty
{
}
3.创建一个枚举
public enum transitType
{
metero = 1,
transit = 2,
takethelift = 3,
ascendthestairs = 4
}
4.将4个类分别继承对应的接口
class metro : Iduty
class takethelift : IComeUp
class transit : Iduty
class ascendthestairs : IComeUp
5.创建一个工厂类,用于实例化对象,不用客户知道具体是怎么实现的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Factory
{
public static Iduty Show(transitType i)
{
Iduty id = null;
switch (i)
{
case transitType.metero:
id = new metro();
break;
case transitType.transit:
id = new transit();
break;
default:
break;
}
return id;
}
public static IComeUp Show1(transitType i)
{
IComeUp id = null;
switch (i)
{
case transitType.takethelift:
id = new takethelift();
break;
case transitType.ascendthestairs:
id = new ascendthestairs();
break;
default:
break;
}
return id;
}
}
}
6.前台调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
metro me = Factory.Show(transitType.metero) as metro;
ascendthestairs asc = Factory.Show1(transitType.ascendthestairs) as ascendthestairs;
Console.WriteLine("第一天");
Console.WriteLine("上班乘坐的交通工具是{0},它的优点是{1},缺点是{2}", me.Name, me.merit, me.defect);
Console.WriteLine("来到公司楼下我选择的是{0}到达办公室,它的优点是{1},缺点是{2}", asc.Name, asc.merit, asc.defect);
transit me1 = new transit();
takethelift asc1 = new takethelift();
Console.WriteLine("第二天");
Console.WriteLine("上班乘坐的交通工具是{0},它的优点是{1},缺点是{2}", me1.Name, me1.merit, me1.defect);
Console.WriteLine("来到公司楼下我选择的是{0}到达办公室,它的优点是{1},缺点是{2}", asc1.Name, asc1.merit, asc1.defect);
Console.Read();
}
}
}
上述代码用简单工厂来编写和常规编写的区别:
前台调用的时候,不需要知道对象是怎么来的,直接调用对应的方法就可以了,对象的实例化是由工厂类去完成。
缺点:
当每加一次新类型时都要修改工厂类的代码,这里就违反了开闭原则,所以简单工厂很少回去使用,但是学习简单工厂才能学习其他的工厂模式