脚本宝典收集整理的这篇文章主要介绍了【设计模式从入门到精通】00-设计模式简介,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1)软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案。
这个术语是由埃里希.伽玛(Erich Gamma)等人在1990年代从建筑设计领域引入到计算机科学的
2)大厦VS简易房
3)拿实际工作经历来说,当一个项目开发完后,如果客户提出增新功能,怎么办?
4)如果项目开发完后,原来程序员离职,你接手维护该项目怎么办?(维护性、可读性、规范性)
5)目前程序员门槛越来越高,一线 IT 公司(大厂),都会问你在实际项目中使用过什么设计模式,怎样使用的,解决了什么问题。
6)设计模式在软件中哪里?面向对象【OO】=》功能模块【设计模式 + 算法(数据结构)】=》框架【多种设计模式】=》架构【服务器集群】
7)如果想成为合格软件工程师,那就花时间来研究下设计模式是非常必要的
原型设计模式
1)有请使用 UML 类图画出原型模式核心角色
2)原型设计模式的深拷贝和浅拷贝是什么,并写出深拷贝的两种方式的源码(重写 clone 方法实现深拷贝、使用序列化来实现深拷贝)
3)在 Spring 框架中哪里使用到原型模式,并对源码进行分析
4)Spring 中原型 bean 的创建,就是原型模式的应用
5)代码分析 + Debug 源码
设计模式七大原则
要求:
状态模式
金融借贷平台项目:借贷平台的订单,有审核-发布-抢单等等步骤,随着操作的不同,会改变订单的状态,项目中的这个模块实现就会使用到状态模式,请你使用状态模式进行设计,并完成实际代码
问题分析:这类代码难以应对变化,在添加一种状态时,我们需要手动添加 if/else ,在添加一种功能时,要对所有的状态进行判断。因此代码会变得越来越臃肿,并且一旦没有处理某个状态,便会发生极其严重的BUG,难以维护
解释器设计模式
1)介绍解释器设计模式是什么?
2)画出解释器设计模式的 UML 类图,分析设计模式中的各个角色是什么?
3)请说明 Spring 的框架中,哪里使用到了解释器设计模式,并做源码级别的分析
单例设计模式
单例设计模式一共有几种实现方式?请分别用代码实现,并说明各个实现方式的优点和实点?
单例设计模式一共有8种写法,后面我们会依次讲到
编写软件过程中,程序员面临着来自耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性等多方面的挑战
设计模式是为了让程序(软件),具有更好的
分享金句
设计模式包含了面向对象的精髓,“懂了设计模式,你就懂了面向对象分析和设计(OOA/D)的精要”
Scott Mavers 在其巨著《Effective C++》就曾经说过:C++ 老手和 C++ 新手的区别就是前者手背上有很多伤疤
设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础(即:设计模式为什么这样设计的依据)设计模式
常用的七大原则有:
基本介绍
对类来说的,即一个类应该只负责一项职责。如类A负责两个不同职责:职责1,职责2。当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解为A1,A2
应用实例
1)以交通工具案例讲解
2)看老师代码演示
3)方案1[分析说明]
public class SingleResponsibility1 {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.run("汽车");
vehicle.run("轮船");
vehicle.run("飞机");
}
}
/**
* 方式1的分析
* 1.在方式1的run方法中,违反了单一职责原则
* 2.解决的方案非常的简单,根据交通工具运行方法不同,分解成不同类即可
*/
class Vehicle{
public void run(String type){
if ("汽车".equals(type)) {
System.out.println(type + "在公路上运行...");
} else if ("轮船".equals(type)) {
System.out.println(type + "在水面上运行...");
} else if ("飞机".equals(type)) {
System.out.println(type + "在天空上运行...");
}
}
}
4)方案2[分析说明]
public class SingleResponsibility2 {
public static void main(String[] args) {
RoadVehicle roadVehicle = new RoadVehicle();
roadVehicle.run("汽车");
WaterVehicle waterVehicle = new WaterVehicle();
waterVehicle.run("轮船");
AirVehicle airVehicle = new AirVehicle();
airVehicle.run("飞机");
}
}
/**
* 方案2的分析
* 1.遵守单一职责原则
* 2.但是这样做的改动很大,即将类分解,同时修改客户端
* 3.改进:直接修改Vehicle类,改动的代码会比较少=>方案3
*/
class RoadVehicle{
public void run(String type){
System.out.println(type + "在公路上运行...");
}
}
class WaterVehicle{
public void run(String type){
System.out.println(type + "在水面上运行...");
}
}
class AirVehicle{
public void run(String type){
System.out.println(type + "在天空上运行...");
}
}
public class SingleResponsibility3 {
public static void main(String[] args) {
Vehicle2 vehicle = new Vehicle2();
vehicle.run("汽车");
vehicle.runWater("轮船");
vehicle.runAir("飞机");
}
}
/**
* 方式3的分析
* 1.这种修改方法没有对原来的类做大的修改,只是增加方法
* 2.这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责
*/
class Vehicle2{
public void run(String type){
System.out.println(type + "在公路上运行...");
}
public void runWater(String type){
System.out.println(type + "在水面上运行...");
}
public void runAir(String type){
System.out.println(type + "在天空上运行...");
}
}
总结
基本介绍
应用实例
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
@override
public void operation1() {
System.out.println("B 实现了 operation1");
}
@Override
public void operation2() {
System.out.println("B 实现了 operation2");
}
@Override
public void operation3() {
System.out.println("B 实现了 operation3");
}
@Override
public void operation4() {
System.out.println("B 实现了 operation4");
}
@Override
public void operation5() {
System.out.println("B 实现了 operation5");
}
}
class D implements Interface1 {
@Override
public void operation1() {
System.out.println("D 实现了 operation1");
}
@Override
public void operation2() {
System.out.println("D 实现了 operation2");
}
@Override
public void operation3() {
System.out.println("D 实现了 operation3");
}
@Override
public void operation4() {
System.out.println("D 实现了 operation4");
}
@Override
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
/**
* A类通过接口Interface1依赖(使用)B类,但是只会用到1,2,3方法
*/
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
/**
* C类通过接口Interface1依赖(使用)D类,但是只会用到1,4,5方法
*/
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
问题与改进
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1, Interface2 {
@Override
public void operation1() {
System.out.println("B 实现了 operation1");
}
@Override
public void operation2() {
System.out.println("B 实现了 operation2");
}
@Override
public void operation3() {
System.out.println("B 实现了 operation3");
}
}
class D implements Interface1, Interface3 {
@Override
public void operation1() {
System.out.println("D 实现了 operation1");
}
@Override
public void operation4() {
System.out.println("D 实现了 operation4");
}
@Override
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
/**
* A类通过接口Interface1,Interface2依赖(使用)B类,但是只会用到1,2,3方法
*/
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
/**
* C类通过接口Interface1,Interface3依赖(使用)D类,但是只会用到1,4,5方法
*/
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
基本介绍
应用实例
1)请编程完成 Person 接收消息的功能
2)实现方案 1 + 分析说明
/**
* 方式1分析
* 1.简单,比较容易想到
* 2.如果我们获取的对象是微信,短信等等,则新增类,同时 Peron也要增加相应的接收方法
* 3.解决思路:
* 引入一个抽象的接口IReceiver,表示接收者,这样Person类与接口IReceiver发生依赖
* 因为Email,Weixin等等属于接收的范围,他们各自实现IReceiver接口就ok,这样我们就符号依赖倒转原则
*/
class Email {
public String getInfo() {
return "电子邮件信息:Hello World!";
}
}
class Person {
public void receive(Email email) {
System.out.println(email.getInfo());
}
}
3)实现方案 2 + 分析说明
interface IReceiver {
String getInfo();
}
class Email implements IReceiver {
@Override
public String getInfo() {
return "电子邮件信息:Hello World!";
}
}
class Weixin implements IReceiver {
@Override
public String getInfo() {
return "微信消息:Hello World!";
}
}
class ShortMessage implements IReceiver {
@Override
public String getInfo() {
return "短信信息:Hello World!";
}
}
class Person {
public void receive(IReceiver receiver) {
System.out.println(receiver.getInfo());
}
}
1)接口传递
//方式1:通过接口传递实现依赖
//开关的接口
interface IOpenAndClose {
void open(ITV tv);//抽象方法,接收接口
}
//ITV接口
interface ITV {
void play();
}
//实现接口
class OpenAndClose implements IOpenAndClose {
public void open(ITV tv){
tv.play();
}
}
2)构造方法传递
//方式2:通过构造函数实现依赖
//开关的接口
interface IOpenAndClose {
void open();//抽象方法
}
//ITV接口
interface ITV {
public void play();
}
//实现接口
class OpenAndClose implements IOpenAndClose {
private ITV tv; // 成员
public OpenAndClose(ITV tv){ // 构造器
this.tv = tv;
}
public void open(){
this.tv.play();
}
}
3)setter 方式传递
//方式3,通过setter方法传递
interface IOpenAndClose{
void open();//抽象方法
void setTv(ITV tv);
}
//ITV接口
interface ITV{
void play();
}
//实现接口
class OpenAndClose implements IOpenAndClose{
private ITV tv;
public void setTv(ITV tv){
this.tv=tv;
}
public void open(){
this.tv.play();
}
}
注意事项和细节
OO 中继承性的思考和说明
基本介绍
一个程序引出的问题和思考
先看个程序,思考下问题和解决思路
public void test() {
A a = new A();
System.out.println("11-3=" + a.func1(11, 3));
System.out.println("1-8=" + a.func1(1, 8));
System.out.println("---------------------");
B b = new B();
System.out.println("11-3=" + b.func1(11, 3));
System.out.println("1-8=" + b.func1(1, 8));
System.out.println("11+3+9=" + b.func2(11, 3));
}
class A {
//返回两个数的差
public int func1(int num1, int num2) {
return num1 - num2;
}
}
class B extends A {
@Override
public int func1(int num1, int num2) {
return num1 + num2;
}
//增加了一个新功能:完成两个数相加,然后和9求和
public int func2(int num1, int num2) {
return func1(num1, num2) + 9;
}
}
解决方法
1)我们发现原来运行正常的相减功能发生了错误。原因就是类 B 无意中重写了父类的方法,造成原有功能出现错误。在实际编程中,我们常常会通过重写父类的方法完成新的功能,这样写起来虽然简单,但整个继承体系的复用性会比较差。特别是运行多态比较频繁的时候
2)通用的做法是:原来的父类和子类都继承一个更通俗的基类,原有的继承关系去掉,采用依赖、聚合、组合等关系代替
3)改进方案
//创建一个更加基础的基类
class Base {
//将更基础的成员和方法写到Base类中
}
class A extends Base {
//返回两个数的差
public int func1(int num1, int num2) {
return num1 - num2;
}
}
class B extends Base {
//如果B需要使用A类的方法,使用组合关系
private A a;
public int func1(int num1, int num2) {
return num1 + num2;
}
//增加了一个新功能:完成两个数相加,然后和9求和
public int func2(int num1, int num2) {
return func1(num1, num2) + 9;
}
public int func3(int num1, int num2) {
return this.a.func1(num1, num2);
}
}
基本介绍
看一段代码
一个画图形的功能,类图设计如下:
class GraphicEditor {
public void drawShape(Shape s) {
if (s.m_type == 1) {
drawRectangle(s);
} else if (s.m_type == 2) {
drawCircle(s);
} else if (s.m_type == 3) {
drawTriangle(s);
}
}
public void drawRectangle(Shape r) {
System.out.println("矩形");
}
public void drawCircle(Shape r) {
System.out.println("圆形");
}
public void drawTriangle(Shape r) {
System.out.println("三角形");
}
}
class Shape {
public int m_type;
}
class RectangleShape extends Shape {
RectangleShape() {
m_type = 1;
}
}
class CircleShape extends Shape {
CircleShape() {
m_type = 2;
}
}
class TriangleShape extends Shape {
TriangleShape() {
m_type = 3;
}
}
方式 1 的优缺点
方式 1 的改进的思路分析
把创建 Shape 类做成抽象类,并提供一个抽象的 draw 方法,让子类去实现即可
这样我们有新的图形种类时,只需要让新的图形类继承 Shape,并实现 draw 方法即可
使用方的代码就不需要修改,满足了开闭原则
方式 2 来解决
1)方式 2 的设计方案:定义一个 Shape 抽象类
2)看代码示例
class GraphicEditor {
public void drawShape(Shape s) {
s.draw();
}
}
abstract class Shape {
int m_type;
public abstract void draw();
}
class RectangleShape extends Shape {
RectangleShape() {
m_type = 1;
}
@Override
public void draw() {
System.out.println("矩形");
}
}
class CircleShape extends Shape {
CircleShape() {
m_type = 2;
}
@Override
public void draw() {
System.out.println("圆形");
}
}
class TriangleShape extends Shape {
TriangleShape() {
m_type = 3;
}
@Override
public void draw() {
System.out.println("三角形");
}
}
3)从方式 2 看,代码满足了 OCP 原则
基本介绍
应用实例
1)有一个学校,下属有各个学院和总部,现要求打印出学校总部员工 ID 和学院员工的 id
2)编程实现上面的功能,看代码演示
//总部员工
class Employee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
//学院员工
class CollegeEmployee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
//学院员工管理 类
class CollegeManager {
public List<collegeemployee> getAllEmployee() {
List<collegeemployee> list = new ArrayList<>();
CollegeEmployee collegeEmployee;
for (int i = 0; i < 10; i++) {
collegeEmployee = new CollegeEmployee();
collegeEmployee.setId("学院员工id=" + i);
list.add(collegeEmployee);
}
return list;
}
}
//总部员工管理类
class SchoolManager {
public List<employee> getAllEmployee() {
List<employee> list = new ArrayList<>();
Employee employee;
for (int i = 0; i < 5; i++) {
employee = new Employee();
employee.setId("总部员工id=" + i);
list.add(employee);
}
return list;
}
public void printAllEmployee(CollegeManager sub) {
System.out.println("--------------学院员工--------------");
List<collegeemployee> list1 = sub.getAllEmployee();
for (CollegeEmployee collegeEmployee : list1) {
System.out.println(collegeEmployee.getId());
}
System.out.println("---------------总部员工-------------");
List<employee> list2 = this.getAllEmployee();
for (Employee employee : list2) {
System.out.println(employee.getId());
}
}
}
应用实例改进
注意事项和细节
基本介绍
原则是尽量使用合成/聚合的方式,而不是使用继承
1)UML—-Unified modeling language UML(统一建模语言),是一种用于软件系统分析和设计的语言工具,它用于帮助软件开发人员进行思考和记录思路的结果
2)UML 本身是一套符号的规定,就像数学符号和化学符号一样,这些符号用于描述软件模型中的各个元素和他们之间的关系,比如类、接口、实现、泛化、依赖、组合、聚合等
画 UML 图与写文章差不多,都是把自己的思想描述给别人看,关键在于思路和条理,UML图分类:
说明
public class Person {
private Integer id;
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
只要是在类中用到了对方,那么他们之间就存在依赖关系。如果没有对方,连编译都通过不了
public class PersonServiceBean {
// 类的成员属性
private PersonDAO personDao;
// 方法接收的参数类型
public void save(Person person) {
}
// 方法的返回类型
public IDCard getIDCard(Integer personid) {
return null;
}
// 方法中使用到
public void modify() {
Department department = new Department();
}
}
UML 类图
小结
泛化关系实际上就是继承关系,它是依赖关系的特例
public abstract class DaoSupport {
public void save(Object entity) {
}
public void delete(Object id) {
}
}
public class PersonServiceBean extends DaoSupport {
}
UML 类图
小结
实现关系实际上就是 A 类实现 B 类,它是依赖关系的特例
public interface PersonService {
void delete(Integer id);
}
public class PersonServiceBean implements PersonService {
@Override
public void delete(Integer id) {
System.out.println("delete...");
}
}
UML 类图
关联关系实际上就是类与类之间的联系,它是依赖关系的特例
public class Person {
private IDCard card;
}
public class IDCard {}
UML 类图
public class Person {
private IDCard card;
}
public class IDCard {
private Person person;
}
UML 类图
聚合关系表示的是整体和部分的关系,整体与部分可以分开。聚合关系是关联关系的特例,所以它具有关联的导航性与多重性
如:一台电脑由键盘(keyboard)、显示器(monitor),鼠标等组成;组成电脑的各个配件是可以从电脑上分离出来的,使用带空心菱形的实线来表示:
public class Mouse {}
public class Monitor {}
public class Computer {
private Mouse mouse;
private Monitor monitor;
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
}
UML 类图
组合关系也是整体与部分的关系,但是整体与部分不可以分开
如果我们认为 Mouse、Monitor 和 Computer 是不可分离的,则升级为组合关系
public class Mouse {}
public class Monitor {}
public class Computer {
private Mouse mouse = new Mouse();
private Monitor monitor = new Monitor();
}
UML 类图
再看一个案例,在程序中我们定义实体:Person 与 IDCard、Head,那么 Head 和 Person 就是组合,IDCard 和 Person 就是聚合
public class IDCard{}
public class Head{}
public class Person{
private IDCard card;
private Head head = new Head();
}
UML 类图
但是如果在程序中,Person 实体中定义了对 IDCard 进行级联删除,即删除 Person 时连同 IDCard 一起删除,那么 IDCard 和 Person 就是组合了
设计模式分为至种类型,共 23 种
注意:不同的书籍上对分类和名称略有差别
以上是脚本宝典为你收集整理的【设计模式从入门到精通】00-设计模式简介全部内容,希望文章能够帮你解决【设计模式从入门到精通】00-设计模式简介所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。