本文已参与「新人创作礼」活动,一起开启掘金创作之路。
- 个人简介:微信公众号关注:SteveCode。为您分享更多的知识学术。生于忧患死于安乐
- 专注Java技术干货分享,Java基础技术、数据结构、相关工具、Spring全家桶、intellij idea......
外观模式
蛋糕外观模式 Code
类
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: AbsCake
* Date: 2022-04-09 15:29
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-09 15:29
*/
public abstract class AbsCake {
/**
* 开启
*/
public abstract void start();
/**
* 关闭
*/
public abstract void end();
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: Baking
* Date: 2022-04-09 15:10
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述:烘烤
*
* @author Songxianyang
* @date 2022-04-09 15:10
*/
public class Baking extends AbsCake{
@Override
public void start() {
System.out.println("开启烘烤机器准备蛋糕");
}
@Override
public void end() {
System.out.println("关闭烘烤机");
}
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: CakeShopFacade
* Date: 2022-04-09 15:05
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述: 蛋糕外观类
* 我得理解:将功能复杂的调用关系封装在一个方法中,调用端调用即可。比较好理解
* 例子:蛋糕店: 水果机器、烘烤机、打蛋器、奶油机、、、、
*
* @author Songxianyang
* @date 2022-04-09 15:05
*/
public class CakeShopFacade {
/**
* 使用聚合的关系来使用对象 对 类的使用不够灵活 用着玩 学习外观模式
* 面向接口编程 整着玩
*/
private Baking baking;
private Cream cream;
private Fruits fruits;
private Whisk whisk;
public CakeShopFacade(Baking baking, Cream cream, Fruits fruits, Whisk whisk) {
this.baking = baking;
this.cream = cream;
this.fruits = fruits;
this.whisk = whisk;
}
public void startIn() {
baking.start();
cream.start();
fruits.start();
whisk.start();
}
public void end() {
baking.end();
cream.end();
fruits.end();
whisk.end();
}
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: Cream
* Date: 2022-04-09 15:28
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述:奶油机器
*
* @author Songxianyang
* @date 2022-04-09 15:28
*/
public class Cream extends AbsCake{
@Override
public void start() {
System.out.println("奶油机器开始打奶油");
}
@Override
public void end() {
System.out.println("奶油机器关闭");
}
//、、、、
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: Fruits
* Date: 2022-04-09 15:09
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述: 水果机器
*
* @author Songxianyang
* @date 2022-04-09 15:09
*/
public class Fruits extends AbsCake{
@Override
public void start() {
System.out.println("各种水果已经做好");
}
@Override
public void end() {
System.out.println("关闭水果机器");
}
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: Test
* Date: 2022-04-09 15:44
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-09 15:44
*/
public class Test {
public static void main(String[] args) {
CakeShopFacade facade = new CakeShopFacade(new Baking(), new Cream(), new Fruits(), new Whisk());
facade.startIn();
}
}
/*******************************************************************************
* Package: com.example.demo.design.facade
* Type: Whisk
* Date: 2022-04-09 15:10
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.design.facade;
/**
* 功能描述:打蛋器
*
* @author Songxianyang
* @date 2022-04-09 15:10
*/
public class Whisk extends AbsCake{
@Override
public void start() {
System.out.println("打蛋器开始打蛋");
}
@Override
public void end() {
System.out.println("关闭打蛋器");
}
}
测试结果
mybaitis 底层 外感模式 源码分析
/*******************************************************************************
* Package: com.example.demo.jdk
* Type: MybatisFacade
* Date: 2022-04-09 15:45
*
* Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
*
* You may not use this file except in compliance with the License.
*******************************************************************************/
package com.example.demo.jdk;
import org.apache.ibatis.session.Configuration;
/**
* 功能描述:外观模式下的mybatis
*
* @author Songxianyang
* @date 2022-04-09 15:45
*/
public class MybatisFacade {
public static void main(String[] args) {
new Configuration();
}
}
分析如下
public MetaObject newMetaObject(Object object) {
return MetaObject.forObject(object, this.objectFactory, this.objectWrapperFactory, this.reflectorFactory);
}
其中调用的方法:
public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
return object == null ? SystemMetaObject.NULL_META_OBJECT : new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}
在掉构造器
private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
this.originalObject = object;
this.objectFactory = objectFactory;
this.objectWrapperFactory = objectWrapperFactory;
this.reflectorFactory = reflectorFactory;
if (object instanceof ObjectWrapper) {
this.objectWrapper = (ObjectWrapper)object;
} else if (objectWrapperFactory.hasWrapperFor(object)) {
this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
} else if (object instanceof Map) {
this.objectWrapper = new MapWrapper(this, (Map)object);
} else if (object instanceof Collection) {
this.objectWrapper = new CollectionWrapper(this, (Collection)object);
} else {
this.objectWrapper = new BeanWrapper(this, object);
}
}