软件设计方法-YuZhou.ppt

上传人:ga****84 文档编号:374137 上传时间:2018-09-28 格式:PPT 页数:21 大小:2.73MB
下载 相关 举报
软件设计方法-YuZhou.ppt_第1页
第1页 / 共21页
软件设计方法-YuZhou.ppt_第2页
第2页 / 共21页
软件设计方法-YuZhou.ppt_第3页
第3页 / 共21页
软件设计方法-YuZhou.ppt_第4页
第4页 / 共21页
软件设计方法-YuZhou.ppt_第5页
第5页 / 共21页
点击查看更多>>
资源描述

1、创建型设计模式,工厂方法模式 (factory method pattern)单例模式(singleton pattern)抽象工厂模式 (abstract factory pattern)生成器模式 (builder pattern)原型模式(prototype pattern),目录,工厂方法模式,工厂方法模式实例:,采用new的if-else设计,工厂方法模式,工厂方法模式实例(2):,客户端代码,工厂方法模式实例(3):,8,单例模式,Singleton: a class that has only one instance,9,Singleton pattern,singleton:

2、 an object that is the only object of its typeensures that a class has at most one instanceprovides a global access point to that instancetakes responsibility of managing that instance away from the programmer (illegal to construct more instances)provide accessor method that allows users to see the

3、(one and only) instancepossibly the most known / popular design pattern! (this should tell you something),10,Restricting objects, continued,One way to avoid creating objects:use static methods insteadMath, System, JOptionPaneis this a good alternative choice? Why or why not?,Problem: lacks flexibili

4、tyExample: static methods cant be passed as an argument to a method, nor returnedProblem: cannot be extendedExample: static methods cant be subclassed and overridden like a singletons could be,11,make constructor(s) private so that they can not be called from outsidedeclare a single static private i

5、nstance of the classwrite a public getInstance() or similar method that allows access to the single instancepossibly protect / synchronize this method to ensure that it will work in a multi-threaded program,Implementing Singleton,12,Singleton sequence diagram,13,consider a singleton class RandomGene

6、rator that generates random numberspublic class RandomGenerator private static RandomGenerator gen = new RandomGenerator(); public static RandomGenerator getInstance() return gen; private RandomGenerator() .possible problem: always creates the instance, even if it isnt used,Singleton example,14,vari

7、ation: dont create the instance until needed/ Generates random numbers.public class RandomGenerator private static RandomGenerator gen = null; public static RandomGenerator getInstance() if (gen = null) gen = new RandomGenerator(); return gen; .What could go wrong with this version?,Singleton exampl

8、e 2,15,variation: solve concurrency issue by locking/ Generates random numbers.public class RandomGenerator private static RandomGenerator gen = null; public static synchronized RandomGenerator getInstance() if (gen = null) gen = new RandomGenerator(); return gen; .Is anything wrong with this versio

9、n?,Singleton example 3,16,variation: solve concurrency issue without unnecessary locking/ Generates random numbers.public class RandomGenerator private static RandomGenerator gen = null; public static RandomGenerator getInstance() if (gen = null) synchronized (RandomGenerator.class) / must test agai

10、n - can you see why? / sometimes called test-and-test-and-set (TTS) if (gen = null) gen = new RandomGenerator(); return gen; ,Singleton example 4,抽象工厂(Abstract Factory),抽象工厂示例,创建支持多视感风格标准的用户界面工具包:支持多种视感标准;不同标准之间具有可移植性;,生成器模式(Builder),原型模式(Prototype),Summarize the differences among the five creational design patterns?Difference between copy constructor and prototype pattern?,Q1:,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文资料库 > 毕业论文

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。