工厂模式
把具体创建产品的细节封装起来,你要什么产品,我给你什么产品即可。
简单工厂模式
工厂方法模式
缓存层:抽象类
抽象工厂模式
缓存层是:接口
原型模式
问题:
原型模式的源码分析:Spring 中的 getBean()这个方法低层实现。是否选择单例、和原型模式。
自己的Code
/*******************************************************************************
* Package: com.example.demo.design.prototype
* Type: User
* Date: 2022-03-06 14:22
*
* 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.prototype;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
/**
* 功能描述: 原型模式分析:
* 无线创建相同对象 但是对象的地址值都不是相同的
*
*
* @author Songxianyang
* @date 2022-03-06 14:22
*/
@Data
@Slf4j
public class User implements Cloneable {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
}
@Override
protected Object clone() {
User user =null;
try {
user = (User) super.clone();
} catch (CloneNotSupportedException e) {
log.error("clone: ", "无法或不应克隆对象");
}
return user;
}
}
实现:
/*******************************************************************************
* Package: com.example.demo.design.prototype
* Type: Test
* Date: 2022-03-06 14: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.prototype;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-06 14:29
*/
public class Test {
public static void main(String[] args) {
User user = new User("sxy", 22);
System.out.println(user);
System.out.println(user.clone());
System.out.println(user.clone());
System.out.println(user==user.clone());
}
}
建造者模式
源码盘点
项目截图
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: BuilderHouse
* Date: 2022-03-06 21:14
*
* 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.builder;
/**
* 功能描述: 抽象的一个构建者
*
* @author Songxianyang
* @date 2022-03-06 21:14
*/
public interface BuilderHouse {
House house = new House();
/**
* 地基
*/
void foundation();
/**
* 砌墙
*/
void buildWall();
/**
* 封顶
*/
void capping();
/**
* 构建对象
* @return house
*/
default House builder() {
return house;
}
}
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: Commander
* Date: 2022-03-06 21: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.builder;
/**
* 功能描述: 指挥者:负责怎么去执行 :行为
*
* @author Songxianyang
* @date 2022-03-06 21:29
*/
public class Commander {
BuilderHouse builderHouse = null;
public Commander(BuilderHouse builderHouse) {
this.builderHouse = builderHouse;
}
public House builder () {
builderHouse.foundation();
builderHouse.buildWall();
builderHouse.capping();
return builderHouse.builder();
}
}
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: HighHouse
* Date: 2022-03-06 21: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.builder;
/**
* 功能描述: 高楼
*
* @author Songxianyang
* @date 2022-03-06 21:28
*/
public class HighHouse implements BuilderHouse {
@Override
public void foundation() {
System.out.println("高楼打地基");
}
@Override
public void buildWall() {
System.out.println("高楼砌墙");
}
@Override
public void capping() {
System.out.println("高楼 吊顶");
}
}
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: House
* Date: 2022-03-06 21:11
*
* 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.builder;
import lombok.Data;
/**
* 功能描述: 房子是一个产品类
*
* @author Songxianyang
* @date 2022-03-06 21:11
*/
@Data
public class House {
private String spar;
private Integer height;
}
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: PlainHouse
* Date: 2022-03-06 21:20
*
* 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.builder;
/**
* 功能描述:普通房子 实现类
*
* @author Songxianyang
* @date 2022-03-06 21:20
*/
public class PlainHouse implements BuilderHouse {
@Override
public void foundation() {
System.out.println("普通房子打地基");
}
@Override
public void buildWall() {
System.out.println("普通房子砌墙");
}
@Override
public void capping() {
System.out.println("普通房子 吊顶");
}
}
/*******************************************************************************
* Package: com.example.demo.design.builder
* Type: Test
* Date: 2022-03-06 21:33
*
* 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.builder;
/**
* 功能描述:测试类
*
* @author Songxianyang
* @date 2022-03-06 21:33
*/
public class Test {
public static void main(String[] args) {
//普通房子
PlainHouse plainHouse = new PlainHouse();
Commander commander = new Commander(plainHouse);
commander.builder();
System.out.println("-------------------");
//高楼大厦
HighHouse highHouse = new HighHouse();
Commander high = new Commander(highHouse);
high.builder();
}
}
测试截图
源码分析
new StringBuilder();
建造者 建造一个产品需要那些步骤:Appendable
具体建造出什么样的抽象对象具有什么功能:AbstractStringBuilder
指挥者:具体指挥返回什么样的对象StringBuilder、StringBuffer
适配器模式-结构化
类适配器
上代码
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Voltage
* Date: 2022-03-12 10:14
*
* 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.adapter.classadapter;
/**
* 功能描述:5V电压转换器
*
* @author Songxianyang
* @date 2022-03-12 10:14
*/
public class Change extends Source implements VoltageAdapter {
@Override
public String output5v() {
String v220 = super.output();
v220="5v";
return v220;
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Phone
* Date: 2022-03-12 10: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.adapter.classadapter;
/**
* 功能描述:手机充电
*
* @author Songxianyang
* @date 2022-03-12 10:28
*/
public class Phone {
private VoltageAdapter adapter;
public Phone(VoltageAdapter adapter) {
this.adapter = adapter;
}
public void chargePhone() {
System.out.println("我得手机以及充上"+adapter.output5v());
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter
* Type: Source
* Date: 2022-03-12 10:03
*
* 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.adapter.classadapter;
/**
* 类适配器
* 功能描述:电源类220v 被适配器
*
* @author Songxianyang
* @date 2022-03-12 10:03
*/
public class Source {
public String output() {
return "220v";
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Test
* Date: 2022-03-12 10:34
*
* 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.adapter.classadapter;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-12 10:34
*/
public class Test {
public static void main(String[] args) {
Phone phone = new Phone(new Change());
phone.chargePhone();
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: SourceAdapter
* Date: 2022-03-12 10:06
*
* 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.adapter.classadapter;
/**
* 功能描述: 充电器适配器
*
* @author Songxianyang
* @date 2022-03-12 10:06
*/
public interface VoltageAdapter {
/**
* 输出5v电压
* @return s
*/
String output5v();
}
对象适配器
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Voltage
* Date: 2022-03-12 10:14
*
* 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.adapter.objadapter;
/**
* 功能描述:5V电压转换器 :适配器
*
* @author Songxianyang
* @date 2022-03-12 10:14
*/
public class Change {
private Source source = new Source();
public String output5v() {
String v220 = source.output();
v220="5v";
return v220;
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Phone
* Date: 2022-03-12 10: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.adapter.objadapter;
/**
* 功能描述:手机充电
*
* @author Songxianyang
* @date 2022-03-12 10:28
*/
public class Phone {
private Change change= new Change();
public void chargePhone() {
System.out.println("我得手机以及充上"+change.output5v());
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter
* Type: Source
* Date: 2022-03-12 10:03
*
* 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.adapter.objadapter;
/**
* 类适配器
* 功能描述:电源类220v 被适配器
*
* @author Songxianyang
* @date 2022-03-12 10:03
*/
public class Source {
public String output() {
return "220v";
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Test
* Date: 2022-03-12 10:34
*
* 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.adapter.objadapter;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-12 10:34
*/
public class Test {
public static void main(String[] args) {
Phone phone = new Phone();
phone.chargePhone();
}
}
接口适配器
结构:
代码以及优化
/*******************************************************************************
* Package: com.example.demo.design.adapter.interfaceadapter
* Type: AVoltageAdapter
* Date: 2022-03-12 15:48
*
* 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.adapter.interfaceadapter;
/**
* 功能描述:抽象的一个多种电压 初始化
*
* @author Songxianyang
* @date 2022-03-12 15:48
*/
public abstract class AbsVoltageAdapter implements IVoltageAdapter {
@Override
public String v5() {
return null;
}
@Override
public String v21() {
return null;
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.interfaceadapter
* Type: V
* Date: 2022-03-12 15:47
*
* 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.adapter.interfaceadapter;
/**
* 功能描述: 多种电压 接口
*
* @author Songxianyang
* @date 2022-03-12 15:47
*/
public interface IVoltageAdapter {
/**
* 充手机
* @return s
*/
public String v5();
/**
* 充电车
* @return s
*/
String v21();
/*
......
*/
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.interfaceadapter
* Type: Phone
* Date: 2022-03-12 15:56
*
* 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.adapter.interfaceadapter;
/**
* 功能描述:手机需要5v电压
*
* @author Songxianyang
* @date 2022-03-12 15:56
*/
public class Phone {
private AbsVoltageAdapter abs;
public Phone(AbsVoltageAdapter abs) {
this.abs = abs;
}
public void change() {
System.out.println("小米手机已经充上"+abs.v5());
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.interfaceadapter
* Type: PhoneAdapter
* Date: 2022-03-12 15:53
*
* 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.adapter.interfaceadapter;
/**
* 功能描述:手机适配器5v
* 优化点
* @author Songxianyang
* @date 2022-03-12 15:53
*/
public class PhoneAdapter extends AbsVoltageAdapter {
@Override
public String v5() {
return "5v";
}
}
/*******************************************************************************
* Package: com.example.demo.design.adapter.classadapter
* Type: Test
* Date: 2022-03-12 10:34
*
* 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.adapter.interfaceadapter;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-12 10:34
*/
public class Test {
public static void main(String[] args) {
Phone phone = new Phone(new PhoneAdapter());
phone.change();
}
}
源码分析 mvc
源码 分析
DispatcherServlet dispatcherServlet = new DispatcherServlet();
DispatcherServlet:{
方法:
doDispatch(HttpServletRequest request){
// 获取 request
HttpServletRequest processedRequest = request;
// 执行器对象:Handler
HandlerExecutionChain mappedHandler = null;
// 获取映射执行器对象
mappedHandler = this.getHandler(processedRequest);
// 获取具体的 HandlerAdapter 的适配器
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
// 不同的适配器 返回不同的 mv
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//被调用的代码
//遍历获取具体的handler 并返回
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
if (this.handlerAdapters != null) {
Iterator var2 = this.handlerAdapters.iterator();
while(var2.hasNext()) {
HandlerAdapter adapter = (HandlerAdapter)var2.next();
if (adapter.supports(handler)) {
return adapter;
}
}
}
throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}
}
//接口 :具体的实现 有6个适配器
public interface HandlerAdapter {
handler 标记
boolean supports(Object handler);
@Nullable
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
@Deprecated
long getLastModified(HttpServletRequest request, Object handler);
}
}
适配器到此结束;
桥接模式-结构化
代码分享
类图
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: Brand
* Date: 2022-03-12 20:47
*
* 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.bridg;
/**
* 功能描述:为手机品牌做的接口
*
* @author Songxianyang
* @date 2022-03-12 20:47
*/
public interface Brand {
/**
* 打电话
*/
void call();
/**
* 上网
*/
void net();
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: Flat
* Date: 2022-03-12 20:55
*
* 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.bridg;
/**
* 功能描述:平板 手机
*
* @author Songxianyang
* @date 2022-03-12 20:55
*/
public class FlatPhone extends StylePhone {
private String name = "平板:";
public FlatPhone(Brand brand) {
super(brand);
}
@Override
public void call() {
System.out.println(name);
super.call();
}
@Override
protected void net() {
System.out.println(name);
super.net();
}
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: FlipPhone
* Date: 2022-03-12 20:55
*
* 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.bridg;
/**
* 功能描述:翻盖 手机
*
* @author Songxianyang
* @date 2022-03-12 20:55
*/
public class FlipPhone extends StylePhone {
private String name = "翻盖:";
public FlipPhone(Brand brand) {
super(brand);
}
@Override
public void call() {
System.out.println(name);
super.call();
}
@Override
protected void net() {
System.out.println(name);
super.net();
}
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: XiaoMPhone
* Date: 2022-03-12 20:49
*
* 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.bridg;
/**
* 功能描述:小米手机
*
* @author Songxianyang
* @date 2022-03-12 20:49
*/
public class HuaweiPhone implements Brand {
@Override
public void call() {
System.out.println("Huawei手机打电话");
}
@Override
public void net() {
System.out.println("Huawei手机上网");
}
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: StylePhone
* Date: 2022-03-12 20:53
*
* 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.bridg;
/**
* 功能描述:手机样式
*
* @author Songxianyang
* @date 2022-03-12 20:53
*/
public abstract class StylePhone {
private Brand brand;
public StylePhone(Brand brand) {
this.brand = brand;
}
// 开始搭桥
protected void call(){
brand.call();
}
protected void net() {
brand.net();
}
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: Test
* Date: 2022-03-12 20:51
*
* 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.bridg;
/**
* 功能描述:桥接模式 : 抽象类与接口分开
*
* @author Songxianyang
* @date 2022-03-12 20:51
*/
public class Test {
public static void main(String[] args) {
StylePhone flatPhone = new FlatPhone(new XiaoMiPhone());
flatPhone.call();
flatPhone.net();
System.out.println("----------------------");
StylePhone flipPhone = new FlipPhone(new XiaoMiPhone());
flipPhone.call();
flipPhone.net();
System.out.println("-----------------");
StylePhone huawei = new FlipPhone(new HuaweiPhone());
huawei.call();
huawei.net();
}
}
/*******************************************************************************
* Package: com.example.demo.bridg
* Type: XiaoMPhone
* Date: 2022-03-12 20:49
*
* 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.bridg;
/**
* 功能描述:小米手机
*
* @author Songxianyang
* @date 2022-03-12 20:49
*/
public class XiaoMiPhone implements Brand{
@Override
public void call() {
System.out.println("Xiaomi手机打电话");
}
@Override
public void net() {
System.out.println("Xiaomi手机上网");
}
}
jdbc源码分析
没有分析明白
桥接总结
装饰者设计模式
需求
代码结构
详细代码 展示
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: AmericanoCoffee
* Date: 2022-03-13 15:08
*
* 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.decorator.master;
/**
* 功能描述:美式咖啡
*
* @author Songxianyang
* @date 2022-03-13 15:08
*/
public class AmericanoCoffee extends Coffee{
public AmericanoCoffee() {
setName("美式咖啡");
setPrice(2.0f);
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: ChinaCoffee
* Date: 2022-03-13 15:12
*
* 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.decorator.master;
/**
* 功能描述:中国咖啡
*
* @author Songxianyang
* @date 2022-03-13 15:12
*/
public class ChinaCoffee extends Coffee{
public ChinaCoffee() {
setName("中国咖啡");
setPrice(2.0f);
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: Coffee
* Date: 2022-03-13 15:07
*
* 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.decorator.master;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-13 15:07
*/
public class Coffee extends Drink {
@Override
float count() {
return getPrice();
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: DecoratorSeasone
* Date: 2022-03-13 15:14
*
* 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.decorator.master;
/**
* 功能描述: 装饰者 调料
*
* @author Songxianyang
* @date 2022-03-13 15:14
*/
public class DecoratorSeason extends Drink{
private Drink drink;
public DecoratorSeason(Drink drink) {
this.drink = drink;
}
@Override
float count() {
// getPrice 当前调料的钱 drink.count() 上一次计算的钱
return getPrice()+drink.count();
}
@Override
public void setName(String name) {
super.setName(name+"&&"+drink.getName());
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: Drink
* Date: 2022-03-13 15:01
*
* 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.decorator.master;
/**
* 功能描述: 饮料类 抽象起来
*
* @author Songxianyang
* @date 2022-03-13 15:01
*/
public abstract class Drink {
private String name;
private float price;
/**
* 计算价格
* @return
*/
abstract float count();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: Milk
* Date: 2022-03-13 15:21
*
* 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.decorator.master;
/**
* 功能描述:调料 牛奶
*
* @author Songxianyang
* @date 2022-03-13 15:21
*/
public class Milk extends DecoratorSeason{
public Milk(Drink drink) {
super(drink);
setName("调料 牛奶");
setPrice(1.0f);
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator.master
* Type: SoyBean
* Date: 2022-03-13 15:23
*
* 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.decorator.master;
/**
* 功能描述:豆浆
*
* @author Songxianyang
* @date 2022-03-13 15:23
*/
public class SoyBean extends DecoratorSeason{
public SoyBean(Drink drink) {
super(drink);
setName("豆浆");
setPrice(2.0f);
}
}
/*******************************************************************************
* Package: com.example.demo.design.decorator
* Type: Test
* Date: 2022-02-20 13:36
*
* 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.decorator.master;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-02-20 13:36
*/
public class Test {
public static void main(String[] args) {
//普通一杯 中国咖啡
Drink order = new ChinaCoffee();
System.out.println(order.getPrice());
// 中国咖啡+牛奶
order= new Milk(order);
System.out.println("咖啡总价"+ order.count());
System.out.println(order.getName());
// 中国咖啡+豆浆
order=new SoyBean(order);
System.out.println("咖啡总价"+ order.count());
System.out.println(order.getName());
// 中国咖啡+豆浆+豆浆
order=new SoyBean(order);
System.out.println("咖啡总价"+ order.count());
System.out.println(order.getName());
}
}
测试截图
注意的点: 在装饰器类:DecoratorSeason 中两个的方法 。有说明
装饰器 挺绕的 面试经常问
IO 分析
/*******************************************************************************
* Package: com.example.demo.jdk
* Type: Io
* Date: 2022-03-15 22:25
*
* 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 java.io.DataInputStream;
import java.io.FileInputStream;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-03-15 22:25
*/
public class Io {
public static void main(String[] args) throws Exception {
/**
* FilterInputStream 装饰器
* protected volatile InputStream in; 饮料
*DataInputStream 咖啡
*/
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(""));
System.out.println(dataInputStream.read());
dataInputStream.close();
}
}
外观模式
蛋糕外观模式 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);
}
}
享元 模式
结构
/*******************************************************************************
* Package: com.example.demo.design.enjoy
* Type: Test
* Date: 2022-04-09 21:20
*
* 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.enjoy;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-09 21:20
*/
public class Test {
public static void main(String[] args) {
WebsiteFactory factory = new WebsiteFactory();
Website websiteWX = factory.getWebsite("微信公众号");
websiteWX.use(new User("SteveCode"));
Website websiteWX1 = factory.getWebsite("微信公众号");
websiteWX1.use(new User("SteveCode"));
Website websiteCSDN = factory.getWebsite("CSDN");
websiteCSDN.use(new User("SteveCode."));
System.out.println(factory.size());
}
}
/*******************************************************************************
* Package: com.example.demo.design.enjoy
* Type: User
* Date: 2022-04-09 21:07
*
* 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.enjoy;
import lombok.Data;
/**
* 功能描述: 外部状态 :用户类
* 一直发生变化的对象 :外部状态
*
* @author Songxianyang
* @date 2022-04-09 21:07
*/
@Data
public class User {
private String name;
public User(String name) {
this.name = name;
}
}
/*******************************************************************************
* Package: com.example.demo.design.enjoy
* Type: Website
* Date: 2022-04-09 21:08
*
* 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.enjoy;
/**
* 功能描述:网站 具体实现
*
* @author Songxianyang
* @date 2022-04-09 21:08
*/
public class Website extends WebsiteAbstract {
private String type;
public Website(String type) {
this.type = type;
}
@Override
public void use(User user) {
System.out.println("当前网站正在使用中。。。。"+"该类型是:"+type+",用户:"+user.getName()+"-体验良好");
}
}
/*******************************************************************************
* Package: com.example.demo.design.enjoy
* Type: WebsiteAbstract
* Date: 2022-04-09 21:06
*
* 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.enjoy;
/**
* 功能描述: 享元模式 :共享 对象
* 网站案例
* 抽象类
* @author Songxianyang
* @date 2022-04-09 21:06
*/
public abstract class WebsiteAbstract {
/**
* 网站的使用
*/
public abstract void use(User user);
}
/*******************************************************************************
* Package: com.example.demo.design.enjoy
* Type: WebsiteFactory
* Date: 2022-04-09 21:13
*
* 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.enjoy;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 功能描述: 网站工厂
*
* @author Songxianyang
* @date 2022-04-09 21:13
*/
public class WebsiteFactory {
private Map<String, Website> map = new HashMap<>();
public Website getWebsite(String type) {
//如果没有该类型就创建
if (Objects.isNull(map.get(type))) {
map.put(type,new Website(type));
return map.get(type);
}
// 有就直接使用
return map.get(type);
}
public int size() {
return map.size();
}
}
执行结果:
Integer 源码分析享元模式
/*******************************************************************************
* Package: com.example.demo.jdk
* Type: Enjoy
* Date: 2022-04-09 21:58
*
* 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;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-09 21:58
*/
public class Enjoy {
public static void main(String[] args) {
Integer integer1 = Integer.valueOf(19);
Integer integer12 = Integer.valueOf(19);
Integer integer2 = Integer.valueOf(200);
Integer integer22 = Integer.valueOf(200);
System.out.println(integer1 == integer12);
System.out.println(integer2 == integer22);
}
// 分析 先从cache中找对应的 池中的数据。数据范围-128到127
// 否则就创建新的对象
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
}
代理模式:
静态代理
优缺点
代码结构
Code实现
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: ILog
* Date: 2022-04-10 13:40
*
* 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.proxy.staticproxy;
/**
* 功能描述: 日志接口
* 静态代理:
* 优点:对目标方法 功能增强
* 缺点:不易于维护
* @author Songxianyang
* @date 2022-04-10 13:40
*/
public interface ILog {
/**
* 打印日志
*/
void pint();
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: ILogImpl
* Date: 2022-04-10 13:40
*
* 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.proxy.staticproxy;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 13:40
*/
public class LogImpl implements ILog {
@Override
public void pint() {
System.out.println("打印日志");
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: LogProxy
* Date: 2022-04-10 13:42
*
* 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.proxy.staticproxy;
/**
* 功能描述:
* 实现对方法的增强
* @author Songxianyang
* @date 2022-04-10 13:42
*/
public class LogProxy implements ILog {
private ILog iLog;
public LogProxy(ILog iLog) {
this.iLog = iLog;
}
@Override
public void pint() {
//这边写的可以是业务方法:比喻:事务的开启
System.out.println("开启事务-----");
//执行目标方法:可以做判断 是否有异常 有 回滚 无 提交
iLog.pint();
//假设 无
System.out.println("提交事务-----");
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: Test
* Date: 2022-04-10 13:47
*
* 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.proxy.staticproxy;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 13:47
*/
public class Test {
public static void main(String[] args) {
// 目标类
ILog log = new LogImpl();
// 代理类
LogProxy proxy = new LogProxy(log);
// 调用代理中的方法
proxy.pint();
}
}
运行截图
动态代理
结构
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: ILog
* Date: 2022-04-10 13:40
*
* 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.proxy.dynamic;
/**
* 功能描述: 日志接口
* 动态代理:jdk 代理 接口代理
* 优点:对目标方法 功能增强
* 缺点:易于维护
* @author Songxianyang
* @date 2022-04-10 13:40
*/
public interface ILog {
/**
* 打印日志
*/
void pint();
/**
* 打印一个数
*/
void num( int i);
int count(int j);
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.staticproxy
* Type: ILogImpl
* Date: 2022-04-10 13:40
*
* 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.proxy.dynamic;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 13:40
*/
public class LogImpl implements ILog {
@Override
public void pint() {
System.out.println("打印日志");
}
@Override
public void num(int i) {
System.out.println(i);
}
@Override
public int count(int j) {
return j;
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.dynamic
* Type: LogProxyFactory
* Date: 2022-04-10 14:13
*
* 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.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 功能描述:
* 代理工厂
* @author Songxianyang
* @date 2022-04-10 14:13
*/
public class LogProxyFactory {
/**
* 目标对象
*/
private Object target;
public LogProxyFactory(Object target) {
this.target = target;
}
/**
* 获取目标对象实例
* @return
*/
public Object getTargetExample() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("jdk 代理开始");
// 两个参数进行说明 1、目标类 2、参数数组
Object invoke = method.invoke(target, args);
System.out.println("jdk 代理结束");
return invoke;
}
});
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.dynamic
* Type: Test
* Date: 2022-04-10 14:21
*
* 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.proxy.dynamic;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 14:21
*/
public class Test {
public static void main(String[] args) {
ILog log = new LogImpl();
LogProxyFactory factory = new LogProxyFactory(log);
// 强制类型转换 要转成 接口的类型 特别奇怪
ILog logn = (ILog) factory.getTargetExample();
//logn.pint();
//logn.num(99);
System.out.println(logn.count(9));
}
}
运行测试
cglib 动态代理
结构
/*******************************************************************************
* Package: com.example.demo.design.proxy.cjlib
* Type: Log
* Date: 2022-04-10 16:56
*
* 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.proxy.cjlib;
/**
* 功能描述:cglib 代理
* 对象代理 并不需要来代理接口
*
*
* @author Songxianyang
* @date 2022-04-10 16:56
*/
public class Log {
public void pint() {
System.out.println("SteveCode 在学习 cglib 动态代理 哈哈");
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.cjlib
* Type: ProxyFactory
* Date: 2022-04-10 16:57
*
* 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.proxy.cjlib;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 16:57
*/
public class ProxyFactory implements MethodInterceptor {
/**
* 目标对象
*/
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
/**
* 获取代理对象 比较重要
*/
public Object getTargetExample() {
// 创建工具类
Enhancer enhancer = new Enhancer();
// 设置父类
enhancer.setSuperclass(target.getClass());
// 设置回调函数
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}
/**
* 方法增强
* @param o
* @param method 代理对象中的执行方法
* @param args 参数
* @param methodProxy 代理的方法
* @return
* @throws Throwable
*/
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("cglib 代理方法的增强~~~~");
Object invoke = method.invoke(target, args);
System.out.println("cglib 代理方法的提交~~~~");
return invoke;
}
}
/*******************************************************************************
* Package: com.example.demo.design.proxy.cjlib
* Type: Test
* Date: 2022-04-10 17: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.proxy.cjlib;
/**
* 功能描述:
*
* @author Songxianyang
* @date 2022-04-10 17:10
*/
public class Test {
public static void main(String[] args) {
// 目标对象
Log log = new Log();
//获取代理工厂
ProxyFactory factory = new ProxyFactory(log);
// 或缺代理对象
Log log2 = (Log) factory.getTargetExample();
// 执行被代理的方法
log2.pint();
}
}
运行结果
模板方法模式
结构
代码
/*******************************************************************************
* Package: com.example.demo.design.template
* Type: AbsBeanMlik
* Date: 2022-04-10 17:43
*
* 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.template;
/**
* 功能描述: 制作不同口味豆浆
* 口味:发生改变 (抽象)
* 制作过程 都是相同的 (模板)
*
* 模板方法:行为型模式
*
*
* @author Songxianyang
* @date 2022-04-10 17:43
*/
public abstract class AbsBeanMilk {
/**
* 口味 调味料
*/
public abstract void dressing();
/**
* 制作模板
*/
public void template() {
//选材
putBean();
// 什么口味
dressing();
// 豆浆机开始打磨
beanMachine();
}
/**
* 什么类型 的 大豆
*/
public abstract void putBean();
/**
*
*/
public void beanMachine() {
System.out.println("中国制造:SteveCode牌 豆浆机 开始打磨");
}
}
/*******************************************************************************
* Package: com.example.demo.design.template
* Type: Strawberry
* Date: 2022-04-10 17:52
*
* 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.template;
/**
* 功能描述: 草莓味
*
* @author Songxianyang
* @date 2022-04-10 17:52
*/
public class Strawberry extends AbsBeanMilk{
@Override
public void dressing() {
System.out.println("草莓味");
}
@Override
public void putBean() {
System.out.println("普通大豆");
}
}
/*******************************************************************************
* Package: com.example.demo.design.template
* Type: A
* Date: 2022-04-10 17:42
*
* 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.template;
/**
* 功能描述:模板方法模式
*
* @author Songxianyang
* @date 2022-04-10 17:42
*/
public class Test {
public static void main(String[] args) {
AbsBeanMilk strawberry = new Strawberry();
strawberry.template();
System.out.println("-------");
AbsBeanMilk watermelon = new Watermelon();
watermelon.template();
}
}
/*******************************************************************************
* Package: com.example.demo.design.template
* Type: Watermelon
* Date: 2022-04-10 17:54
*
* 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.template;
/**
* 功能描述: 西瓜味
*
* @author Songxianyang
* @date 2022-04-10 17:54
*/
public class Watermelon extends AbsBeanMilk{
@Override
public void dressing() {
System.out.println("西瓜味");
}
@Override
public void putBean() {
System.out.println("优质大豆");
}
}