大话设计模式之简单工厂模式

转载地址:

http://www.cnblogs.com/xyz168/archive/2011/11/25/2263437.html


本节的主要内容:1.简单工厂模式的意图;2.UML图描述;3示例代码

一、简单工厂模式意图根据提供的数据类型。选择一个类进行实例化。

二、UML图描述

三、代码示例

    举例说明:

工作中薪资的计算方式:

程序员=底薪+ 绩效;

销售员=底薪+提成;

助理=底薪+平均奖金;

Staff类代码:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.SimpleFactoryExample
7 {
8 public abstract class Staff
9 {
10 public abstract void GetSalary();
11 }
12 }

Coder、Salesman、Assistant类

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.SimpleFactoryExample
7 {
8 public class Coder:Staff
9 {
10 public override void GetSalary()
11 {
12 Console.WriteLine("Coder's Salar:底薪+绩效");
13 }
14 }
15 public class Salesman:Staff
16 {
17 public override void GetSalary()
18 {
19 Console.WriteLine("Coder's Salar:底薪+绩效");
20 }
21 }
22
23 public class Assistant:Staff
24 {
25 public override void GetSalary()
26 {
27 Console.WriteLine("Assistant's Salar:底薪+平均奖金");
28 }
29 }
30 }

JobEnum

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.SimpleFactoryExample
7 {
8 public enum JobEnum
9 {
10 Assistant,
11 Coder,
12 Salesman
13 }
14 }

StaffFactory类:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.SimpleFactoryExample
7 {
8 class StaffFactory
9 {
10 public static Staff CreateStaff(JobEnum jobEnum)
11 {
12 Staff staff=null;
13 switch (jobEnum)
14 {
15 case JobEnum.Assistant:
16 staff = new Assistant();
17 break;
18 case JobEnum.Coder:
19 staff = new Coder();
20 break;
21 case JobEnum.Salesman:
22 staff = new Salesman();
23 break;
24 }
25 return staff;
26 }
27 }
28 }

Program.cs

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.SimpleFactoryExample
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Console.Title = "简单工厂模式";
13 Console.WriteLine("Press any key to exit.......\r\n");
14 Staff staff = StaffFactory.CreateStaff(JobEnum.Salesman);
15 staff.GetSalary();
16 Console.Read();
17 }
18 }
19 }

运行结果如下:

四、代码这里下载

五、小结:这个模式主要是封装了Coder、Assistant等子类。当有的新员工子类(如:客户类),直接继承Staff类,修改StaffFactory类就OK.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它通过一个工厂类来创建不同类型的对象,而无需暴露对象创建的逻辑给客户端。在Python中,简单工厂模式可以通过一个工厂类来创建不同的产品对象。下面是一个简单的示例: ```python class Product: def operation(self): pass class ConcreteProductA(Product): def operation(self): print("Performing operation A.") class ConcreteProductB(Product): def operation(self): print("Performing operation B.") class SimpleFactory: @staticmethod def create_product(product_type): if product_type == "A": return ConcreteProductA() elif product_type == "B": return ConcreteProductB() else: raise ValueError("Invalid product type.") # 使用简单工厂创建产品对象 factory = SimpleFactory() product_a = factory.create_product("A") product_a.operation() product_b = factory.create_product("B") product_b.operation() ``` 在上述示例中,`Product` 是一个抽象产品类,`ConcreteProductA` 和 `ConcreteProductB` 是具体产品类。`SimpleFactory` 是工厂类,通过 `create_product` 方法根据不同的产品类型创建相应的产品对象。 通过简单工厂模式,客户端无需知道具体的产品类,只需要通过工厂类来创建产品对象。这样可以降低客户端与具体产品类的耦合度,并且当需要新增产品时,只需要修改工厂类即可。 希望这个简单的示例能帮助你理解简单工厂模式在Python中的应用。如果有任何进一步的问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值