1.单例模式
饿汉模式1
复习点:
1 直接 new 一个 static final 的成员变量
public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}
懒汉模式1
复习点:
1 volatile 禁止指令重排序
2 双重检查,线程安全,效率高
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
懒汉模式2(推荐使用)
复习点:
1 jvm 保证线程安全
2 当第一次调用getInstance()时,才会加载SingletonHolder 类,所以是懒汉模式
public class Singleton {
private static class SingletonHolder {
private static final Singleton instance = new Singleton();
}
private Singleton() {
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
枚举(听这种写法是最完美的)
1 这种写法可以防止通过反射获得对象实例
public enum Singleton {
INSTANCE;
}
2.策略模式
复习点:
1.jdk 中的接口Comparator就是策略模式的应用
2.根据不同的策略执行不同的逻辑
//创建一个接口
public interface Strategy {
public int doOperation(int num1, int num2);
}
//创建实现接口的实体类,加
public class OperationAdd implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
//创建实现接口的实体类,减
public class OperationSubtract implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}
//创建 Context 类。
public class Context {
private Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
//使用 Context 来查看当它改变策略 Strategy 时的行为变化。
public class StrategyPatternDemo {
public static void main(String[] args) {
Context context = new Context(new OperationAdd());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));
context = new Context(new OperationSubtract());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));
}
}
3.建造者模式
复习点:
1.为了编程方便,先构建一个比较简单的对象,再一点一点的设置属性
public class Person {
int id;
String name;
int age;
double weight;
int score;
Location loc;
private Person() {}
public static class PersonBuilder {
Person p = new Person();
public PersonBuilder basicInfo(int id, String name, int age) {
p.id = id;
p.name = name;
p.age = age;
return this;
}
public PersonBuilder weight(double weight) {
p.weight = weight;
return this;
}
public PersonBuilder score(int score) {
p.score = score;
return this;
}
public PersonBuilder loc(String street, String roomNo) {
p.loc = new Location(street, roomNo);
return this;
}
public Person build() {
return p;
}
}
}
class Location {
String street;
String roomNo;
public Location(String street, String roomNo) {
this.street = street;
this.roomNo = roomNo;
}
}
4.适配器模式
复习点:
1.
4.外观模式
1.提供统一的门面,外界程序都和这个门面打交道
2.感觉电脑的例子更形象:
电脑整机是 CPU、内存、硬盘的外观。有了外观以后,启动电脑和关闭电脑都简化了
//形状父类
public interface Shape {
void draw();
}
//长方形
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
//正方形
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
//圆形
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
//外观类
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
//都通过外观类调用
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
5.中介者模式
1.在现实生活中,有很多中介者模式的身影,例如QQ游戏平台,聊天室、QQ群、短信平台和房产中介。不论是QQ游戏还是QQ群,它们都是充当一个中间平台,QQ用户可以登录这个中间平台与其他QQ用户进行交流,如果没有这些中间平台,我们如果想与朋友进行聊天的话,可能就需要当面才可以了。
2.mq
6.装饰器模式
1.动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。
2.装饰模式为已有类动态附加额外的功能就像LOL、王者荣耀等类Dota游戏中,英雄升级一样。每次英雄升级都会附加一个额外技能点学习技能。
//Component 英雄接口
public interface Hero {
//学习技能
void learnSkills();
}
//ConcreteComponent 具体英雄盲僧
public class BlindMonk implements Hero {
private String name;
public BlindMonk(String name) {
this.name = name;
}
@Override
public void learnSkills() {
System.out.println(name + "学习了以上技能!");
}
}
//Decorator 技能栏
public class Skills implements Hero{
//持有一个英雄对象接口
private Hero hero;
public Skills(Hero hero) {
this.hero = hero;
}
@Override
public void learnSkills() {
if(hero != null)
hero.learnSkills();
}
}
//ConreteDecorator 技能:Q
public class Skill_Q extends Skills{
private String skillName;
public Skill_Q(Hero hero,String skillName) {
super(hero);
this.skillName = skillName;
}
@Override
public void learnSkills() {
System.out.println("学习了技能Q:" +skillName);
super.learnSkills();
}
}
//ConreteDecorator 技能:W
public class Skill_W extends Skills{
private String skillName;
public Skill_W(Hero hero,String skillName) {
super(hero);
this.skillName = skillName;
}
@Override
public void learnSkills() {
System.out.println("学习了技能W:" + skillName);
super.learnSkills();
}
}
//ConreteDecorator 技能:E
public class Skill_E extends Skills{
private String skillName;
public Skill_E(Hero hero,String skillName) {
super(hero);
this.skillName = skillName;
}
@Override
public void learnSkills() {
System.out.println("学习了技能E:"+skillName);
super.learnSkills();
}
}
//ConreteDecorator 技能:R
public class Skill_R extends Skills{
private String skillName;
public Skill_R(Hero hero,String skillName) {
super(hero);
this.skillName = skillName;
}
@Override
public void learnSkills() {
System.out.println("学习了技能R:" +skillName );
super.learnSkills();
}
}
//客户端:召唤师
public class Player {
public static void main(String[] args) {
//选择英雄
Hero hero = new BlindMonk("李青");
Skills skills = new Skills(hero);
Skills r = new Skill_R(skills,"猛龙摆尾");
Skills e = new Skill_E(r,"天雷破/摧筋断骨");
Skills w = new Skill_W(e,"金钟罩/铁布衫");
Skills q = new Skill_Q(w,"天音波/回音击");
//学习技能
q.learnSkills();
}
}
输出结果:
学习了技能Q:天音波/回音击
学习了技能W:金钟罩/铁布衫
学习了技能E:天雷破/摧筋断骨
学习了技能R:猛龙摆尾
李青学习了以上技能!
6.享元模式
1.共享对象
2.比如常量池,连接池,线程池
public class TestString {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s1 == s2); //true
System.out.println(s1 == s3); //false
System.out.println(s3 == s4); //false
System.out.println(s3.intern() == s1);//true
System.out.println(s3.intern() == s4.intern());//true
}
}
7.桥接模式
复习点:
1.将抽象和实现放在两个不同的类层次中,使它们可以独立地变化。——《Head First 设计模式》
public interface DrawAPI {
public void drawCircle(int radius, int x, int y);
}
public class RedCircle implements DrawAPI {
@Override
public void drawCircle() {
System.out.println("Drawing Circle color: red");
}
}
public class GreenCircle implements DrawAPI {
@Override
public void drawCircle() {
System.out.println("Drawing Circle color: green");
}
}
public abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}
public class Circle extends Shape {
public Circle(DrawAPI drawAPI) {
super(drawAPI);
}
public void draw() {
drawAPI.drawCircle();
}
}
public class BridgePatternDemo {
public static void main(String[] args) {
Shape redCircle = new Circle(new RedCircle());
Shape greenCircle = new Circle(new GreenCircle());
redCircle.draw();
greenCircle.draw();
}
}
执行结果
Drawing Circle color: red
Drawing Circle color: green
8.模板方法
复习点:
1.定义一个操作中的算法的骨架,而将一些步骤延迟到子类中实现。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤
public abstract class Game {
abstract void initialize();
abstract void startPlay();
abstract void endPlay();
//模板,定义算法骨架
public final void play(){
//初始化游戏
initialize();
//开始游戏
startPlay();
//结束游戏
endPlay();
}
}
public class Cricket extends Game {
@Override
void endPlay() {
System.out.println("Cricket Game Finished!");
}
@Override
void initialize() {
System.out.println("Cricket Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Cricket Game Started. Enjoy the game!");
}
}
public class Football extends Game {
@Override
void endPlay() {
System.out.println("Football Game Finished!");
}
@Override
void initialize() {
System.out.println("Football Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Football Game Started. Enjoy the game!");
}
}
public class TemplatePatternDemo {
public static void main(String[] args) {
Game game = new Cricket();
game.play();
System.out.println();
game = new Football();
game.play();
}
}