Java交流君羊【785749075】暗号:67
第⼀节
- 1: MyBatis源码阅读
- 2: 作为⼀个架构师 最基本的职业素养
- 3: 如何优雅的写代码
如何评价⼀个代码⾜够优秀
1: 耗时+空间利⽤率 (空间的话 减少⼀些不必要的内存开销(jvm调优) 利⽤优秀的算法提⾼整个程序性能(不要求会写算法 LRU)
2: 技术栈 (⾃⼰的⼀个技术⼴度和技术深度 【⾃学能⼒=阅读源码+英语⽔平】
3: 是否能够将业务模型转化为实际硬编码。(抽象能⼒)
设计模式
阅读源码的时候,如何读懂源码?
参考别⼈编写同样需求时,他是如何考量问题的,如何解决问题的。
设计模式是什么?
百度⼀下 。 粗浅的认识: 设计模式是经验的总结,规定的⼀些编写代码的套路。但是他不是范式。
设计模式的优势
- 1: 很好的可以在源码中找到精髓
- 2: 增强你的抽象能⼒
- 3: 利于了解⾯向对象
推荐阅读
- 1: ⼤话设计模式
- 2: 设计模式之禅
- 3: 设计模式沉思录 (2000年出版的⼀本叫《OOD沉思录》巨好)
- 4: 研磨设计模式
适配器
适配器的分类
常⻅的适配器模式有三种:对象适配器、接⼝适配器、类适配器。
解决的问题
接⼝和实现类的之间的⼀个继承冲突问题
编写业务代码
- 1: 使⽤抽象类分离接⼝和实现类
- 2: 抽象类中分摊实现类不想实现的接⼝中的⽅法
- 3: 实现类只需要管好⾃⼰。
定义接⼝
/**
* 学⽣守则接⼝
*/
public interface StudnetRole {
//定义家⻓签字的规则
void singnature(int score); }
⽗类
public class WangUncle implements StudnetRole{
@Override
public void singnature(int score) {
if(score<60){
System.out.println("王叔叔替我签字");
}
}
⼦类(实现类)
public class Studnet extends WangUncle{
private int socre;
public Studnet(int socre) {
this.socre = socre;
}
public int getSocre() {
return socre;
}
public void setSocre(int socre) {
this.socre = socre;
}
综上,虽然增加了中间的⽗类,但是问题很明显,虽然⼦类中⼀部分内容让⽗类搞定了,但是⼦类想要实现的功能也被⽗类给搞掉了
优化(将⽗类增加为⼀个抽象类)
定义接⼝
/**
* 学⽣守则接⼝
*/
public interface StudnetRole {
//定义家⻓签字的规则
void singnature(int score);
//⼦类想要实现的⽅法
public void witchMovie();
⽗类
public abstract class WangUncle implements StudnetRole{
@Override
public void singnature(int score) {
if(score<60){
System.out.println("王叔叔替我签字");
}
}
⼦类(实现类)
public class Studnet extends WangUncle{
private int socre;
public Studnet(int socre) {
this.socre = socre;
}
public int getSocre() {
return socre;
}
public void setSocre(int socre) {
this.socre = socre;
}
@Override
public void witchMovie() {
System.out.println("我很开⼼ 去看电影了");
}
哪些实际场景中使⽤到了适配器
Servlet Servlet和GenericServlet类之java.io.InputStreamReader(InputStream
is)
模版设计模式
You don’t call me,I’ll call you back;
解决问题
- 1:算法、业务⻣架固定,核⼼业务或者常被改变的代码延迟到⼦类实现。更加利于扩展。
- 2:⽗类控制⼦类中的执⾏流程
编写⽅式
1:使⽤⽗类,可以是抽象类。将核⼼业务⽅法进⾏抽象。在程序中通过final修饰核⼼的流程业务。
2:编写具体⼦类,⽆法重写⽗类中的final⽅法
3:调⽤⼦类⽅法时,处理处理流程还是⽗类的
测试代码
抽象类
public abstract class Father {
void study(){
System.out.println("day day up good good study");
}
void working(){
System.out.println("love working");
}
abstract void fallInLove();
final void life(){
study();
working();
fallInLove();
}
⼦类
public class Son extends Father{
void study(){
System.out.println("随便学学");
}
void working(){
System.out.println("这个不是主要的。。。");
}
void fallInLove(){
System.out.println("be faithful to one's huasband unto death");
} }
测试类
public class Test {
public static void main(String[] args) {
Father f = new Son();
f.life();
Father f1 = new Father() {
@Override
void fallInLove() {
System.out.println("don't fallINLove,new ");
}
};
f1.life();
}
结果:
随便学学
这个不是主要的。。。
be faithful to one's huasband unto death
day day up good good study
love working
don't fallINLove,new
PS:通过以上案例,我们能够清楚的发现,⼦类将⽗类中的⼀些核⼼代码可以延迟到后期实现。且⽗类已经指定了整个业务的执⾏顺序。开发时只需要考虑⽅法的具体实现,⽽不是何时被调⽤。
Java交流君羊【785749075】暗号:67