笑说设计模式-小白逃课被点名

2021/02/20 10:30

• 简单工厂
• 工厂方法
• 抽象工厂

场景分析

简单工厂

**Calculator操作类 **

    public class Calculator
{
public double GetResult(double A, double B, string operate)
{
double result = 0d;
switch (operate)
{
case "+":
result = A + B;
break;
case "-":
result = A - B;
break;
case "*":
result = A * B;
break;
case "/":
result = A / B;
break;
default: break;
}
return result;
}
}


    class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入数字A");
string a = Console.ReadLine();
Console.WriteLine("请输入数字B");
string b = Console.ReadLine();
Console.WriteLine("请选择操作符号(+、-、*、/)");
string operate = Console.ReadLine();

Calculator box = new Calculator();
double result = box.GetResult(Convert.ToDouble(a), Convert.ToDouble(b), operate);
Console.WriteLine(result);

}
}


1、如果输入A=10，B=0,程序就会报错，没有做输入的有效性验证

2、如果操作符号不按照规定的输入 ，也会导致报错“

    public class Operate
{
public double NumberA { get; set; }
public double NumberB { get; set; }

public virtual double GetResult()
{
return 0;
}
}

public class OperateAdd : Operate
{
public override double GetResult()
{
return this.NumberA +this.NumberB;
}
}

public class OperateSub : Operate
{
public override double GetResult()
{
return this.NumberA - this.NumberB;
}
}


    public class OperateFactory
{
public static Operate GetOperateFactory(string operate)
{
Operate fac = null;
switch (operate)
{
case "+":
fac = new OperateAdd();
break;
case "-":
fac = new OperateSub();
break;
case "*":
fac = new OperateMul();
break;
case "/":
fac = new OperateDiv();
break;
default:
break;
}
return fac;
}
}


  static void Main(string[] args)
{
Console.WriteLine("请选择操作符号(+、-、*、/)");
string operateStr = Console.ReadLine();
Operate operate = OperateFactory.GetOperateFactory(operateStr);
operate.NumberA = 10;
operate.NumberB = 4;
double result = operate.GetResult();
Console.WriteLine(result);
}


工厂方法

....几分钟过去了.....

1、定义一个抽象类Fruit.cs，这个类定义俩个抽象方法printfColor()printfName()

2、实现俩种不同的水果分别继承此抽象类并复写抽象方法。

3、定义一个工厂接口，定义接口方法createFruit()

4、实现俩个不同的工厂分别实现水果实例的创建。

    public abstract class Fruit
{
public abstract void PrintfColor();
public abstract void PrintfName();
}

public class Apple : Fruit
{
public override void PrintfColor()
{
Console.WriteLine("红色");
}

public override void PrintfName()
{
Console.WriteLine("苹果");
}
}


   public interface IFruitFactory
{
Fruit CreateFruit();
}

public class AppleFactory : IFruitFactory
{
public Fruit CreateFruit()
{
return new Apple();
}
}


           //苹果工厂
IFruitFactory appleFac = new AppleFactory();
Fruit apple = appleFac.CreateFruit();
apple.PrintfColor();
apple.PrintfName

//橘子工厂
IFruitFactory orangeFac = new OrangeFactory();
Fruit orage = orangeFac.CreateFruit();
orage.PrintfColor();
orage.PrintfName();


1、读取配置文件（json/xml）

2、获取所有的配置方式，循环遍历

3、判断配置类型，如果是输入到文件的配置。new一个文件日志工厂，将配置信息作为参数传递，便于后期方法调用；如果是输入到控制台的配置。new一个日志工厂也是做同样的操作

4、每一个工厂只管理自己的事情，但是应该都拥有日志输出这个接口。

5、当上层调用打印方法时候，循环遍历所有的工厂，调用接口的日志输出输出方法

抽象工厂

A

• A_苹果工厂.cs
• A_橘子工厂.cs

B

• B_苹果工厂.cs
• B_橘子工厂.cs

C

• C_苹果工厂.cs
• C_橘子工厂.cs

    public abstract class Fruit
{
public string Name { get; set; }
public abstract void PrintfColor();
public abstract void PrintfName();
}

public class Apple : Fruit
{
public Apple(string name)
{
this.Name = name;
}

public override void PrintfColor()
{
Console.WriteLine(this.Name + "红色");
}

public override void PrintfName()
{
Console.WriteLine(this.Name + "苹果");
}
}


A

• A_苹果工厂.
• A_橘子工厂

B

• B_苹果工厂
• B_橘子工厂

C

• C_苹果工厂

A 工厂

• 苹果/橘子

B 工厂

• 苹果/橘子

C 工厂

• 苹果/橘子

    public interface IFruitFactory
{
Fruit CreateApple(string name);
Fruit CreateOrange(string name);
}

public class AFactory : IFruitFactory
{
public Fruit CreateApple(string name)
{
return new Apple(name);
}

public Fruit CreateOrange(string name)
{
return new Orange(name);
}
}


            IFruitFactory fac = new AFactory();
Fruit a_Apple = fac.CreateApple("a工厂");
Fruit a_Orange = fac.CreateOrange("a工厂");
a_Apple.PrintfName();
a_Orange.PrintfName();

IFruitFactory b_fac = new BFactory();
Fruit b_Apple = b_fac.CreateApple("b工厂");
Fruit b_Orange = b_fac.CreateOrange("b工厂");
b_Apple.PrintfName();
b_Orange.PrintfName();


0 评论
1 收藏
2