《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
- Sourceable
public interface Sourceable{
public void method1();
public void method2();
}
- Wrapper-抽象类
public abstract class Wrapper implements Sourceable{
public void method1(){}
public void method2(){}
}
- SourceSub1
public class SourceSub1 extends Wrapper{
public void method1(){
System.out.println(“The sourceable interface’s first Sub”);
}
}
- SourceSub2
public class SourceSub2 extends Wrapper(){
public void method2(){
System.out.println(“The Sourceable interface’s second Sub”);
}
}
- WrapperTest
public class WrapperTest{
public static void main(String[] args){
Sourceable source1=new SourceSub1();
Sourceable source2=new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}
三种适配器模式的应用场景:
类的适配器模式:
当希望一个类转换成满足另一个新接口的类时,可以使用类的适配器模式
创建一个新类,继承原有的类,实现新的接口即可
对象的适配器模式:
当希望一个对象转换成满足另一个新接口的对象时,可以使用对象的适配器模式
创建一个 Wrapper 类,持有原类的一个实例,在 Wrapper 类的方法中,调用实例的方法即可
接口的适配器模式:
当不希望实现一个接口中所有的方法时,可以使用接口的适配器模式
创建一个抽象类 Wrapper,实现所有方法,写其它类时,只要继承抽象类即可
装饰器模式(Decorator)
装饰器模式: 给一个对象动态地增加一些新的功能
装饰器模式要求装饰对象和被装饰对象实现同一个接口, 装饰对象持有被装饰对象的实例
Source 类时被装饰类 ,Decorator 类是装饰类,可以为 Source 类动态地增加一些功能:
- Sourceable
public interface Sourceable{
public void method();
}
- Source
public class Source implements Sourceable{
@Override
public void method(){
System.out.println(“The original method!”);
}
}
- Decorator
public class Decorator implements Sourceable{
private Sourceable source;
public Decorator(Sourceable source){
super();
this.source=source;
}
@Override
public void method(){
System.out.println(“Before decorator!”);
source.method();
System.out.println(“After decorator!”);
}
}
-Test
public class DecoratorTest{
public static void main(String[] args){
Sourceable source=new Source();
Sourceable obj=new Decorator(source);
obj.method();
}
}
装饰器模式应用场景:
-
需要扩展一个类的功能
-
动态地为一个对象增加功能,而且还能动态地撤销(继承的功能是静态的,不能动态增删)
装饰器模式的缺点: 产生过多类似的对象,不易排错
代理模式(Proxy)
- 代理模式: 创建一个代理类,替原对象进行一些操作
- Sourceable
public interface Sourceable{
public void method();
}
- Source
public class Source implements Sourceable{
@Override
public void method(){
System.out.println(“The original method!”);
}
}
- Proxy
public class Proxy implements Sourceable{
private Source source;
public Proxy(){
super();
this.source=new Source;
}
@Override
public void method(){
before();
source.method();
after();
}
public void before(){
System.out.println(“Before Proxy!”);
}
public void after(){
System.out.println(“After Proxy!”);
}
}
- ProxyTest
public class ProxyTest{
public static void main(String[] args){
Sourceable source=new Proxy();
source.method();
}
}
代理模式的应用场景:
-
已有的方法在使用的时候需要对原有的方法进行改进,有两种方法:
-
修改原有的方法来适应: 这样违反了"对扩展开放,对修改关闭"的原则 .不推荐使用
-
采用一个代理类调用原有的方法,且对产生的结果进行控制. 即代理模式
-
使用代理模式,可以将功能划分的更加清晰,有助于后期维护
外观模式(Facade)
在 Spring 中,可以将类与类之间的关系配置到配置文件中
外观模式: 为了解决类与类之间的依赖关系,将类鱼雷之间的关系放到一个 Facade 类中,降低类与类之间的耦合度,该模式中没有涉及到接口
- CPU
public class CPU{
public void startup(){
System.out.println(“CPU startup!”);
}
public void shutdown(){
System.out.println(“CPU shutdown!”);
}
}
- Memory
public class Memory{
public void startup(){
System.out.println(“Memory startup!”);
}
public void shutdown(){
System.out.println(“Memory shutdown!”);
}
}
- Disk
public class Disk{
public void startup(){
System.out.println(“Disk startup!”);
}
public void shutdown(){
System.out.println(“Disk shutdown!”);
}
}
- Computer
public class Computer{
private CPU cpu;
private Memory memory;
private Disk disk;
public Computer(){
cpu=new CPU();
memory=new Memory();
disk=new Disk();
}
public void startup(){
System.out.println(“Start the computer!”);
cpu.startup();
memory.startup();
disk.startup();
System.out.println(“Start the computer finished!”);
}
public void shutdown(){
System.out.println(“Begin to close the computer!”);
cpu.shutdown();
memory.shutdown();
disk.shutdown();
System.out.println(“Computer closed!”);
}
}
-User
public class User{
public static void main(String[] args){
Computer computer=new Computer();
computer.startup();
computer.shutdown();
}
}
如果没有 Computer 类 ,CPU,Memory,Disk 之间会互相持有实例,产生关系,这样会造成严重依赖
修改一个类,可能会带来其它类的修改
有了 Computer 类,各个类之间的关系就放在类 Computer 类里,这样就起到解耦的作用
桥接模式(Bridge)
桥接模式: 将事物和具体实现分开,二者可以各自独立的变化
将抽象化与实现化解耦,使得二者可以独立变化:
-
JDBC 桥 DriverManager:
-
JDBC 连接数据库的时候,在各个数据库之间进行切换,基本不需要改动太多的代码,甚至一点不用改动
-
原因在于 JDBC 提供统一接口,每个数据库提供各自实现,用一个叫作数据库驱动的程序来桥接即可
- Sourceable
public interface Sourceable{
public void method();
}
- SourceSub1
public class SourceSub1 implements Sourceable{
@Override
public void method(){
System.out.println(“This is the first sub!”);
}
}
- SourceSub2
public class SourceSub2 implements Sourceable{
@Override
public void method(){
System.out.println(“This is the second sub!”);
}
}
- 定义一个桥,持有Sourceable的一个实例
public abstract class Bridge{
private Sourceable source;
public void method(){
source.method();
}
public Sourceable getSource(){
return source;
}
public void getSource(Sourceable source){
this.source=source;
}
}
- MyBridge
public class MyBridge extends Bridge{
public void method(){
getSource().method();
}
}
- BridgeTest
public class BridgeTest{
public static void main(String[] args){
Bridge bridge=new MyBridge();
/* 调用第一个对象 */
Sourceable source1=new SourceSub1();
bridge.setSource(source1);
bridge.method();
/* 调用第二个对象 */
Sourceable source2=new SourceSub2();
bridge.setSource(source2);
bridge.method();
}
}
通过对 Bridge 类的调用,实现了对接口 Sourceable 的实现类 SourceSub1 和 SourceSub2 的调用
示例: JDBC 连接原理
组合模式(Composite)
组合模式: 部分-整体模式,在处理类似树形结构的问题时比较方便
- TreeNode
public class TreeNode{
private String name;
private TreeNode parent;
private Vector children=new Vector();
public TreeNode(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public TreeNode getParent(){
return parent;
}
public void setParent(TreeNode parent){
this.parent=parent;
}
/* 添加孩子节点 */
public void add(TreeNode node){
children.add(node);
}
/* 删除孩子节点 */
public void remove(TreeNode node){
children.remove(node);
}
/* 获得孩子节点 */
public Enumeration getChildren(){
return children.elements();
}
}
- Tree
public class Tree{
TreeNode root=null;
public Tree(String name){
root=new TreeNode(name);
}
public void main(String[] args){
Tree tree=new Tree(“A”);
TreeNode nodeB=new TreeNode(“B”);
TreeNode nodeC=new TreeNode(“C”);
nodeB.add(nodeC);
tree.root.add(nodeB);
System.out.println(“Build the tree finished!”);
}
}
总结
谈到面试,其实说白了就是刷题刷题刷题,天天作死的刷。。。。。
为了准备这个“金三银四”的春招,狂刷一个月的题,狂补超多的漏洞知识,像这次美团面试问的算法、数据库、Redis、设计模式等这些题目都是我刷到过的
并且我也将自己刷的题全部整理成了PDF或者Word文档(含详细答案解析)
66个Java面试知识点
架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)
算法刷题(PDF)
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
eration getChildren(){
return children.elements();
}
}
- Tree
public class Tree{
TreeNode root=null;
public Tree(String name){
root=new TreeNode(name);
}
public void main(String[] args){
Tree tree=new Tree(“A”);
TreeNode nodeB=new TreeNode(“B”);
TreeNode nodeC=new TreeNode(“C”);
nodeB.add(nodeC);
tree.root.add(nodeB);
System.out.println(“Build the tree finished!”);
}
}
总结
谈到面试,其实说白了就是刷题刷题刷题,天天作死的刷。。。。。
为了准备这个“金三银四”的春招,狂刷一个月的题,狂补超多的漏洞知识,像这次美团面试问的算法、数据库、Redis、设计模式等这些题目都是我刷到过的
并且我也将自己刷的题全部整理成了PDF或者Word文档(含详细答案解析)
[外链图片转存中…(img-UK6j6Flx-1714668744818)]
66个Java面试知识点
架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)
[外链图片转存中…(img-Bhznc2Dc-1714668744818)]
算法刷题(PDF)
[外链图片转存中…(img-yTus85zQ-1714668744819)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!