Design Patterns in ActionScript–Abstract Factory

抽象工厂模式详解
本文通过对比工厂方法模式,引入并详细解析了抽象工厂模式的概念及其应用。通过具体案例,展示了如何利用抽象工厂模式来创建一系列相关或依赖的对象,而不指定它们的具体类。

Do you still remember the factory method pattern we talked about last time? If you’re already forgot it. May be you can take a look at the following intent, which was defined by the GOF book.

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

— By THE GOF BOOK


 

In out last example, we use eagleFactory and penguinFactory to decide which bird should be instantiated. And we can easily extend this program only by adding some new classes; we don’t need to change the main framework.

OK, now let’s do something more about the last example.

First, let’s add a new class named littleEagle, and the little eagle doesn’t have the ability to fly, and not migration. So, we can write the code as follows.

  1. class LittleEagle extends Bird {
  2. public   function LittleEagle (){
  3.  
  4. super ( new   neverFly () , new neverMigration ()) ;
  5. }
  6. }

Then, we need to add a bird related to penguin, eh, I wonder that whether there is some kind of penguin flies and not migration. It seems that god doesn’t make that kind of creature. So, we need to make it, and we can call it littlePenguin. Of course, it was faked :) The code is as below.

  1. class LittlePenguin extends Bird {
  2.  
  3. public   function LittlePenguin (){
  4.  
  5. super ( new   fly () , new neverMigration ()) ;
  6.  
  7. }
  8. }

According to the factory method pattern, we need to add two more factories to produce these two new products. And the UML diagram becomes like this.

clip_image002

How’s your feeling about this diagram?

Four factories correspond to four birds. Looking deep inside, we can found that littleEagle is much the same as eagle, and the same to the other two classes. If we want to add bigEagle and oldEagle, we’ll need another two more factories, and it sounds unreasonable. The eagles can be looked upon as series products. Maybe they should be produced by the same factory.

Now, I should show you the new pattern, named abstract factory pattern. Its intent was defined as follows.

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

– By THE GOF BOOK

Using this pattern to change our existing code, we can get the following UML diagram.

clip_image004

Of course, the code has to be changed. Let me show you the code of eagleFactory.

  1. public class EagleFactory implements BirdFactory {
  2.  
  3. public   function getBird () : bird {
  4. return   new eagle () ;
  5. }
  6.  
  7. public   function getLittleBird () : bird {
  8.  
  9. return   new littleEagle () ;
  10. }
  11. }

Abstract Factory and Factory Method are both about generating the new object, while abstract factory cares more about a family of object.

Search-256x256Demo | DownloadDownload Full Project

Enjoy!

多源数据接入 支持校园各业务系统数据接入:包括教务系统(学生成绩、课程信息)、学工系统(奖惩记录、资助信息)、后勤系统(宿舍分配、能耗数据)、图书馆系统(借阅记录、馆藏信息)、一卡通系统(消费数据、门禁记录)等。 接入方式:提供数据库直连(MySQL、SQL Server)、文件导入(CSV、Excel、JSON)、API 接口调用等多种方式,支持实时同步与定时批量同步。 数据标准化与治理 建立校园数据标准体系:统一数据格式(如日期格式、学号编码规则)、定义核心数据元(如 “学生” 包含学号、姓名、专业等必选字段)、规范代码集(如性别代码 “1 - 男,2 - 女”)。 数据清洗:自动检测并处理缺失值、重复值、异常值(如成绩 > 100 分),通过规则引擎实现数据校验(如 “学生年龄需在 16-30 岁之间”)。 元数据管理:记录数据来源、格式、更新频率、负责人等信息,生成数据血缘图谱,追踪数据从产生到应用的全生命周期。 二、数据共享与交换核心功能 分布式数据存储 基于 Hadoop HDFS 实现海量数据存储:结构化数据(成绩、消费记录)存入 HBase,非结构化数据(文档、图片、视频)直接存储于 HDFS,日志类数据通过 Flume 采集至 HDFS。 支持数据分片与副本机制,确保数据高可用(默认 3 副本存储),满足校园 PB 级数据存储需求。 数据交换引擎 构建点对点数据交换通道:各部门系统可通过交换引擎向平台上传数据或申请获取授权数据,支持同步 / 异步交换模式。 交换流程管理:定义数据交换规则(如 “学工系统每日向平台同步新增学生信息”),记录交换日志(成功 / 失败状态、数据量),失败时自动重试。 数据脱敏:对敏感数据(如身份证号、银行卡号)在交换过程中进行脱敏处理(如显示 “110********5678”),兼顾共享与隐私保护。
用户信息管理 支持用户注册(手机号 / 社交账号登录)、个人信息完善(如年龄、性别、饮食禁忌、偏好菜系等)。 记录用户行为数据:浏览历史、收藏 / 点赞美食、评分记录(1-5 星)、消费记录(如外卖订单、到店消费)、搜索关键词等。 美食数据管理 存储美食基础信息:名称、分类(中餐 / 西餐 / 日料等)、子类别(川菜 / 粤菜 / 汉堡等)、食材、口味标签(辣 / 甜 / 清淡等)、价格区间、商家信息(名称、地址、评分)、图片等。 支持商家入驻与信息更新,管理员审核美食数据合规性(如食材描述真实性)。 二、协同过滤推荐核心功能 基于用户的协同过滤(User-based CF) 计算用户相似度:通过用户对美食的评分、浏览记录等行为,使用余弦相似度 / 皮尔逊相关系数识别 “相似用户群体”(如用户 A 和用户 B 对 80% 的川菜评分一致)。 生成推荐:向目标用户推送 “相似用户喜欢但目标用户未体验过” 的美食(如相似用户高分推荐的新川菜馆)。 基于物品的协同过滤(Item-based CF) 计算美食相似度:分析用户对不同美食的共同评分 / 点击行为,挖掘美食间的关联(如 “点过麻婆豆腐的用户中有 70% 也点过回锅肉”)。 生成推荐:为用户推送 “与已喜欢美食相似” 的菜品(如用户刚收藏了水煮鱼,推荐酸菜鱼、毛血旺)。 混合推荐策略 结合两种协同过滤算法结果,根据场景动态调整权重(如冷启动用户优先基于物品的推荐,活跃用户侧重基于用户的推荐)。 融合用户显式偏好(如标注 “不吃辣”)过滤推荐结果,避免无效推荐。 三、用户交互与推荐展示 个性化推荐页 首页展示 “为你推荐” 列表,按推荐优先级排序,显示美食图片、名称、匹配度(如 “98% 的相似用户喜欢”)、用户评分、距离(适用于到店推荐)等。 支持按场景筛选推荐(如 “午餐推荐”“周末聚餐推荐”“性价比推荐”)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值