一、设计模式
1)概念:
设计模式(Design pattern)是一套被反复使用、多数人知晓的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。
2)优点:
(1)使代码编制真正工程化、统一化
(2)确立通用术语,提升代码易读性
(3)使软件更容易修改和维护
(4)降低代码耦合,提升软件扩展性
3)设计模式的分类:Java开发中大致总共有23种设计模式
(1)创建型模式:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式
(2)结构型模式:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
(3)行为型模式:、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录策略模式模式、状 态模式、访问者模式、中介者模式、解释器模式。
如图:
二、创建型模式之简单工厂模式
1)工厂模式家族
简单工厂模式、工厂方法模式、抽象工厂模式
2)什么是工厂模式?
工厂模式是由一个工厂对象决定创建出哪一个具体实例,简单工厂模式是工厂模式家族中最简单最常用的一种
3)简单工厂模式类图
4)工厂方法模式类图
5)抽象工厂模式
三、创建型模式之单例模式
1)单例设计模式:保证当前类有且仅有一个对象,不允许被创建多个实例
2)分类:饿汉式、懒汉式
3)实现思路:
(1)构造函数私有化
(2声明一个本类对象静态引用
(3提供获取当前实例的静态方法
四、结构型模式之装饰者模式
1)定义:在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2)适用场景:
(1) 需要动态的给一个对象添加功能,这些功能可以再动态的撤销
(2) 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实
(3) 当不能采用生成子类的方法进行扩充时类爆炸
3)装饰者模式类图
装饰者模式应用之实例:I/O流体系
五、行为型模式之观察者模式
1)定义:一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知,此种模式通常被用来实现事件处理系统
2)适用场景:
(1) 一个对象状态的更新,需要其他对象同步更新,且其他对象的数量动态可变。
(2) 对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节。
3)观察者模式的类图
观察者模式应用之事件监听器
工厂方法模式实例
package com.test.factorymodle;
/**
* 所有手机的父类:手机模型类
*
* @author lhz
*
*/
public abstract class Phone {
public String type;
public Phone(String type) {
super();
this.type = type;
}
public abstract void call();
}
package com.test.factorymodle;
/**
* 所有手表的父类:手表模型类
*
* @author lhz
*
*/
public abstract class Watch {
public String type;
public Watch(String type) {
super();
this.type = type;
}
public abstract void watch();
}
package com.test.factorymodle;
/**
* 所有电脑的父类:电脑模型
*
* @author lhz
*
*/
public abstract class Computor {
public String type;
public Computor(String type) {
super();
this.type = type;
}
public abstract void play();
}
package com.test.factorymodle;
/**
* 苹果手机模型类
*
* @author lhz
*
*/
public class IPhone extends Phone {
public IPhone(String type) {
super(type);
}
public void call() {
System.out.println("iphone call...");
}
}
package com.test.factorymodle;
/**
* 华为手机模型类
*
* @author lhz
*
*/
public class HWPhone extends Phone {
public HWPhone(String type) {
super(type);
}
public void call() {
System.out.println("huawei call...");
}
}
package com.test.factorymodle;
/**
* 苹果手表模型
*
* @author lhz
*
*/
public class AppleWatch extends Watch {
public AppleWatch(String type) {
super(type);
}
public void watch() {
System.out.println("AppleWatch watch...");
}
}
package com.test.factorymodle;
/**
* 华为手表模型
*
* @author lhz
*
*/
public class HWWatch extends Watch {
public HWWatch(String type) {
super(type);
}
@Override
public void watch() {
System.out.println("huawei watch...");
}
}
package com.test.factorymodle;
/**
* 苹果电脑模型
*
* @author lhz
*
*/
public class AppleComputor extends Computor {
public AppleComputor(String type) {
super(type);
}
public void play() {
System.out.println("applecomputor play...");
}
}
package com.test.factorymodle;
/**
* 华为电脑类:华为电脑模型
*
* @author lhz
*
*/
public class HWComputor extends Computor {
public HWComputor(String type) {
super(type);
}
public void play() {
System.out.println("huaweicomputor play...");
}
}
package com.test.factorymodle;
/**
* 工厂类:用于生产所有产品,手机、手表、电脑
*
* @author lhz
*
*/
public abstract class Factory {
/**
* 生产手机
*
* @param type
* 手机类型
* @return 手机对象
*/
public abstract Phone createPhone(String type);
/**
* 生产手表
*
* @param type
* 手表类型
* @return 手表对象
*/
public abstract Watch createWatch(String type);
/**
* 生产电脑
*
* @param type
* 电脑类型
* @return 电脑对象
*/
public abstract Computor createComputor(String type);
}
package com.test.factorymodle;
/**
* 苹果工厂:生产苹果系列
*
* @author lhz
*
*/
public class AppleFactory extends Factory {
@Override
public Phone createPhone(String type) {
Phone iphone = new IPhone(type);
return iphone;
}
@Override
public Watch createWatch(String type) {
Watch appleWatch = new AppleWatch(type);
return appleWatch;
}
@Override
public Computor createComputor(String type) {
Computor appleComputor = new AppleComputor(type);
return appleComputor;
}
}
package com.test.factorymodle;
/**
* 华为工厂:生产华为系列
*
* @author lhz
*
*/
public class HWFactory extends Factory {
@Override
public Phone createPhone(String type) {
Phone phone = new HWPhone(type);
return phone;
}
@Override
public Watch createWatch(String type) {
Watch watch = new HWWatch(type);
return watch;
}
@Override
public Computor createComputor(String type) {
Computor computor = new HWComputor(type);
return computor;
}
}
package com.test.factorymodle;
public class MainT {
public static void main(String[] args) {
// 需求: 需要苹果手机来打电话
// 1.创建苹果工厂对象
Factory iphone = new AppleFactory();
// 2.通过工厂对象得到苹果手机对象
Phone ip = iphone.createPhone("iphone8");
// 3.用苹果手机打电话
ip.call();
}
}
单例模式
package com.test.simple;
/**
* 学生类:假如只许外界创建一个该类的对象(相当于该类只有唯一一个对象)
*
* @author lags
*
*/
public class Student {
// 2.声明一个本类对象的静态引用
private static Student stu;
private String name;
// 1.构造函数私有化
private Student(String name) {
this.name = name;
}
// 3.提供一个创建本类对象的方法
// 会有同步问题,要用同步锁
public synchronized static Student getStu(String name) {
if (stu == null) {
stu = new Student(name);
}
return stu;
}
public String getName() {
return name;
}
}
package com.test.simple;
public class StudentTest {
public static void main(String[] args) {
Student stu = Student.getStu("lisi");
Student stu1 = Student.getStu("wangwu");
System.out.println(stu.getName());
System.out.println(stu1.getName());
}
}
观察者模式实例
package com.test.observersubject;
import java.util.ArrayList;
/**
* 被观察者父类:发送消息,不管观察者的动作
*
* @author lhx
*
*/
public abstract class Subject {
public ArrayList<Observe> list;
public Subject(ArrayList<Observe> list) {
this.list = list;
}
/**
* 加入观察者
*
* @param obs
*/
public abstract void addObserve(Observe obs);
/**
* 删除观察者
*
* @param obs
*/
public abstract void deleteObserve(Observe obs);
/**
* 通知观察者
*
* @param msg
*/
public abstract void notifyed(String msg);
}
package com.test.observersubject;
import java.util.ArrayList;
/**
* 被观察者A
*
* @author lhx
*
*/
public class ASubject extends Subject {
public ASubject(ArrayList<Observe> list) {
super(list);
}
@Override
public void addObserve(Observe obs) {
list.add(obs);
}
@Override
public void deleteObserve(Observe obs) {
list.remove(this);
}
@Override
public void notifyed(String msg) {
for (int i = 0; i < list.size(); i++) {
list.get(i).listen(msg);
}
}
}
package com.test.observersubject;
/**
* 观察者父类:观察被观察者,只要被观察者发出消息,观察者根据收到的消息作出各自的反应
*
* @author lags
*
*/
public interface Observe {
public void listen(String msg);
}
package com.test.observersubject;
/**
* A观察者
*
* @author lhx
*
*/
public class AObserve implements Observe {
@Override
public void listen(String msg) {
System.out.println(msg + "接收到消息,我回了");
}
}
package com.test.observersubject;
public class BObserve implements Observe {
@Override
public void listen(String msg) {
System.out.println(msg + "接收到消息,我不动");
}
}
package com.test.observersubject;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Observe> list = new ArrayList<Observe>();
ASubject as = new ASubject(list);
AObserve ao = new AObserve();
BObserve bo = new BObserve();
as.addObserve(ao);
as.addObserve(bo);
as.notifyed("ok");
}
}
装饰者模式之实例
package com.test.description;
/**
* 所有cake的父类:原味cake
*
* @author lags
*
*/
public interface Cake {
public abstract float getPrice();
public abstract String getDescription();
}
package com.test.description;
/**
* 原味手抓饼
*
* @author lags
*
*/
public class GraphicsCake implements Cake {
@Override
public float getPrice() {
return 4.0f;
}
@Override
public String getDescription() {
return "原味手抓饼";
}
}
package com.test.description;
/**
* 装饰类:用 于装饰Cake,本质上还是Cake 强关联Cake,即在该类中定义Cake的引用
*
* @author lhx
*
*/
public abstract class Decription implements Cake {
// public float price;
// public String description;
private Cake cake;
public Decription(Cake cake) {
super();
this.cake = cake;
}
@Override
public float getPrice() {
return cake.getPrice();
}
@Override
public String getDescription() {
return cake.getDescription();
}
}
package com.test.description;
/**
* 加牛排的装饰类
*
* @author lags
*
*/
public class BeefDesc extends Decription {
public BeefDesc(Cake cake) {
super(cake);
}
@Override
public float getPrice() {
return super.getPrice() + 3.0f;
}
@Override
public String getDescription() {
return super.getDescription() + "+牛排";
}
}
package com.test.description;
/**
* 加鸡蛋的装饰类
*
* @author lags
*
*/
public class EggDesc extends Decription {
public EggDesc(Cake cake) {
super(cake);
}
@Override
public float getPrice() {
return super.getPrice() + 2.0f;
}
@Override
public String getDescription() {
return super.getDescription() + "+鸡蛋";
}
}
package com.test.description;
public class Test {
public static void main(String[] args) {
GraphicsCake gc = new GraphicsCake();
EggDesc gd = new EggDesc(gc);
BeefDesc bd = new BeefDesc(gd);
System.out.println(bd.getPrice());
System.out.println(bd.getDescription());
}
}