(3).工厂方法模式

我们接上一个模式的实例:

 

(1).创建水果接口 IFruit:

 1 namespace 工厂方法模式
 2 {
 3     public interface IFruit
 4     {
 5         /// <summary>
 6         /// 水果的单价
 7         /// </summary>
 8         double Amount { get; }
 9 
10 
11         /// <summary>
12         /// 种的数量
13         /// </summary>
14         /// <param name="amount"></param>
15         void Plant(int amount);
16 
17         /// <summary>
18         /// 生成
19         /// </summary>
20         /// <param name="days"></param>
21         void Grow(int days);
22 
23         /// <summary>
24          ///收获
25         /// </summary>
26         /// <returns></returns>
27         double Harvest () ;
28     }
29 }
View Code

(2).创建工厂接口 IFruitFactory:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 工厂方法模式.Interface
 8 {
 9     public interface IFruitFactory
10     {
11         IFruit Create();
12 
13     }
14 }
View Code

(3).创建水果的实例 Apple

 1 namespace 工厂方法模式
 2 {
 3     public class Apple : IFruit
 4     {
 5         public double Amount { get; set; }
 6 
 7         public void Grow(int days)
 8         {
 9             Amount *= days * 0.65;
10         }
11 
12         public double Harvest()
13         {
14             return Amount;
15         }
16 
17         public void Plant(int amount)
18         {
19             Amount = amount;
20         }
21     }
22 }
View Code

(4).创建水果的实例 Banana

 1 namespace 工厂方法模式
 2 {
 3     public class Banana : IFruit
 4     {
 5         public double Amount { get; set; }
 6 
 7         public void Grow(int days)
 8         {
 9             Amount *= days * 1.2;
10         }
11 
12         public double Harvest()
13         {
14             return Amount;
15         }
16 
17         public void Plant(int amount)
18         {
19             Amount = amount;
20         }
21     }
22 }
View Code

(5).创建水果的实例 Orange

 1 namespace 工厂方法模式
 2 {
 3     public class Orange : IFruit
 4     {
 5         public double Amount { get; set; }
 6 
 7         public void Grow(int days)
 8         {
 9             Amount *= days * 0.85;
10         }
11 
12         public double Harvest()
13         {
14             return Amount;
15         }
16 
17         public void Plant(int amount)
18         {
19             Amount = amount;
20         }
21     }
22 }
View Code

(6).创建水果工厂的实例 AppleFactory

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 工厂方法模式.Interface;
 7 
 8 namespace 工厂方法模式.Models
 9 {
10     public class AppleFactory : IFruitFactory
11     {
12         public IFruit Create()
13         {
14             return new Apple();
15         }
16     }
17 }
View Code

(7).创建水果工厂的实例 BananaFactory

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 工厂方法模式.Interface;
 7 
 8 namespace 工厂方法模式.Models
 9 {
10     public class BananaFactory : IFruitFactory
11     {
12         public IFruit Create()
13         {
14             return new Banana();
15         }
16     }
17 }
View Code

(8).创建水果工厂的实例 OrangeFactory

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 工厂方法模式.Interface;
 7 
 8 namespace 工厂方法模式.Models
 9 {
10     public class OrangeFactory : IFruitFactory
11     {
12         public IFruit Create()
13         {
14             return new Orange();
15         }
16     }
17 }
View Code

(9).创建使用类:Client

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 工厂方法模式.Models;
 7 
 8 namespace 工厂方法模式
 9 {
10     public class Client
11     {
12         public void Start()
13         {
14             Dostuff(new AppleFactory().Create());
15             Dostuff(new BananaFactory().Create());
16             Dostuff(new OrangeFactory().Create());
17             ///如果我们要新加一种水果,则只用新加一个水果,再加一个工厂不用动之前的代码,符合开闭原则
18 
19             Dostuff(new NewFruitFactory().Create());
20 
21         }
22 
23         public void Dostuff(IFruit fruit) 
24         {
25             fruit.Plant(10);
26             fruit.Grow(180);
27             Console.WriteLine($"柑橘收获: {fruit.Harvest()}");
28         }
29     }
30 }
View Code

(10).调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 工厂方法模式
 8 {
 9     internal class Program
10     {
11         static void Main(string[] args)
12         {
13             new Client().Start();
14             Console.ReadLine(); 
15         }
16     }
17 }
View Code

 

posted @ 2025-03-15 19:08  小码哥-风云  阅读(4)  评论(0)    收藏  举报