Java设计模式
在Java的设计模式中,一般认为有23种设计模式,但是常用的并没有这么多,下面是看资料整理的常用的设计模式,在学习的过程中,个人除了建造者模式和观察者模式比较难理解外,其它的都比较好掌握和理解,由于是自己整理的,难免有错误,欢迎指出问题所在!
单例模式
饿汉模式
/** * 单例设计模式 * 一般来说有两种设计模式 饿汉模式 和 懒汉模式 * @author tqb * */
public class Singleton {
// 饿汉模式
// 创建对象
private static Singleton instance = new Singleton();
// 私有化构造器 禁止new对象
private Singleton(){}
// 返回对象
public static Singleton getInstance(){
return instance;
}
}
懒汉模式
public class Singleton {
// 饿汉模式
private static Singleton instance = null;
// 私有化
private Singleton(){}
// 返回对象
public static Singleton getInstance(){
if (instance == null){
// 静态修饰的方法内不可以使用this关键字 采用反射
synchronized (Singleton.class) {// 保证线程安全 加锁
if (instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
工厂模式
工厂方法模式
普通工厂模式
package designPattern;
import org.junit.Test;
/** * 工厂模式 * ———————————|—————————— * | | * 工厂方法模式 抽象工厂模式(一旦进行扩展程序,就需要修改源码,违背编程原则,推荐使用) * ———————|———————— * | | | * 普通工厂 多个工厂 静态工厂(给多个工厂的方法加上静态修饰符,直接调用方法) * @author tqb * */
public class Factory1 {
/** * 普通工厂 */
@Test
public void test1(){
SendFactory factory = new SendFactory();
// Sender sender = factory.build("mail");
// Sender sender = factory.build("qq");
Sender sender = factory.build("test");
sender.send();//java.lang.NullPointerException
}
}
interface Sender{
public void send();
}
class MailSend implements Sender{
@Override
public void send() {
System.out.println("Mail---Send");
}
}
class QQSend implements Sender{
@Override
public void send() {
System.out.println("QQ---Send");
}
}
class SendFactory{
public Sender build(String type){
if (type.equals("mail")){
return new MailSend();
}else if (type.equals("qq")){
return new QQSend();
}else{
System.out.println("Error:error class type!");
return null;
}
}
}
多个工厂模式
package designPattern;
import org.junit.Test;
/** * 工厂模式 * ———————————|—————————— * | | * 工厂方法模式 抽象工厂模式(一旦进行扩展程序,就需要修改源码,违背编程原则,推荐使用) * ———————|———————— * | | | * 普通工厂 多个工厂 静态工厂 * @author tqb * */
public class Factory2 {
/** * 多个工厂 */
@Test
public void test(){
SendFactory factory = new SendFactory();
Sender mail = factory.mailBuild();
Sender qq = factory.qqBuild();
mail.send();
qq.send();
}
}
interface Sender{
public void send();
}
class MailSend implements Sender{
@Override
public void send() {
System.out.println("Mail---Send");
}
}
class QQSend implements Sender{
@Override
public void send() {
System.out.println("QQ---Send");
}
}
class SendFactory{
public Sender mailBuild(){
return new MailSend();
}
public Sender qqBuild(){
return new QQSend();
}
}
静态工厂模式
将多个工厂模式的方法用静态修饰
抽象工厂模式
package designPattern;
import org.junit.Test;
/** * 工厂模式 * ———————————|—————————— * | | * 工厂方法模式 抽象工厂模式(一旦进行扩展程序,就需要修改源码,违背编程原则,扩展性强) * ———————|———————— * | | | * 普通工厂 多个工厂 静态工厂 * @author tqb * */
public class Factory3 {
/** * 抽象工厂模式 */
@Test
public void test(){
// 创建工厂
SendFactory factory = new MailSendFactory();
Sender sender = factory.build();
sender.send();
SendFactory factory1 = new QQSendFactory();
Sender sender1 = factory1.build();
sender1.send();
}
}
interface Sender{
public void send();
}
class MailSend implements Sender{
@Override
public void send() {
System.out.println("Mail---Send");
}
}
class QQSend implements Sender{
@Override
public void send() {
System.out.println("QQ---Send");
}
}
interface SendFactory{
public Sender build();
}
class MailSendFactory implements SendFactory{
@Override
public Sender build() {
return new MailSend();
}
}
class QQSendFactory implements SendFactory{
@Override
public Sender build() {
return new QQSend();
}
}
建造者模式
这个不是很好理解,推荐大家看这个例子可能好一些 菜鸟教程-建造者模式
适配者模式
类的适配者模式
package designPattern;
import org.junit.Test;
/** * 适配器模式 * |—————————————————|—————————————————| * 类的适配器模式 对象的适配器模式 接口的适配器模式 * @author tqb * */
public class AdapterDemo {
/** * 类的适配器模式 */
@Test
public void test(){
Animals animals = new Adapter();
animals.eat();
animals.love();
}
}
/** * 适配器模式的核心 需要将接口中没有所需功能的类和用户需要的接口进行适配 使得该接口拥有该功能 * @author tqb * */
class Adapter extends AnimalLove implements Animals{
*//**
* 我们不想实现动物爱了这个方法,因为这个方法已经有人实现了
* 所以我们直接继承该方法,使得接口和AnimalLove类进行适配
*//*
@Override
public void eat() {
System.out.println("动物吃饭");
}
}
class AnimalLove{
public void love(){
System.out.println("动物恋爱了");
}
}
interface Animals{
public void love();
public void eat();
}
对象的适配者模式
package designPattern;
import org.junit.Test;
/** * 适配器模式 * |—————————————————|—————————————————| * 类的适配器模式 对象的适配器模式 接口的适配器模式 * @author tqb * */
public class AdapterDemo2 {
/** * 对象的适配器模式 * 不再继承需要的类,使用构造函数将类的对象传入 */
@Test
public void test(){
Adapter a = new Adapter(new AnimalLove());
a.eat();
a.love();
}
}
class Adapter implements Animals{
private AnimalLove al = null;
public Adapter (AnimalLove al){
this.al = al;
}
@Override
public void eat() {
System.out.println("动物吃饭!");
}
@Override
public void love() {
al.love();
}
}
class AnimalLove{
public void love(){
System.out.println("动物恋爱了");
}
}
interface Animals{
public void love();
public void eat();
}
接口的适配者模式
package designPattern;
import org.junit.Test;
/** * 适配器模式 * |—————————————————|—————————————————| * 类的适配器模式 对象的适配器模式 接口的适配器模式 * @author tqb * */
public class AdapterDemo3 {
/** * 接口的适配器模式 * 当接口中有很多的方法,我们实现它的时候就必须实现它的所有方法 * 这时候,我们就用一个抽象适配器类实现它的所有方法,我们只需要继承适配器类,重写需要的方法 */
@Test
public void test(){
Human1 h = new Human1();
h.head();
h.body();
h.foot();
}
}
interface Human{
public void head();
public void body();
public void foot();
public void family();
}
abstract class HumanAdapter implements Human{
@Override
public void head() {
}
@Override
public void body() {
}
@Override
public void foot() {
}
@Override
public void family(){
}
}
class Human1 extends HumanAdapter{
@Override
public void head() {
System.out.println("大头");
}
@Override
public void body() {
System.out.println("瘦小的身材");
}
@Override
public void foot() {
System.out.println("胖胖的脚");
}
}
装饰模式
package designPattern;
import org.junit.Test;
/** * 装饰者模式 * 实现同一接口 * 装饰类中拥有被装饰类的实例 * @author tqb * */
public class DecoratorDemo {
@Test
public void test(){
Animals a = new CatDecorator(new Cat());
a.eat();
}
}
interface Animals{
public void eat();
}
class Cat implements Animals{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
class CatDecorator implements Animals{
private Cat cat;
public CatDecorator(Cat cat) {
this.cat = cat;
}
@Override
public void eat() {
System.out.println("吃鱼前洗猫爪");
cat.eat();
System.out.println("吃鱼后漱口");
}
}
策略模式
package designPattern;
import org.junit.Test;
/** * 策略模式 * 常用于算法决策系统 * 设计一个接口 为实现类提供同一的方法 * 另外可以设计一个辅助类,提供辅助函数,非必需 * 由用户选择使用的具体算法 * @author tqb * */
public class StrategyDemo {
@Test
public void test(){
Calculator c = new Plus();
System.out.println(c.calculate("1+1"));
}
}
interface Calculator{
// 具体的计算
public int calculate(String exp);
}
class Plus extends Helpers implements Calculator{
@Override
public int calculate(String exp) {
// 将exp进行分割 调用辅助类的辅助函数
int[] i = split(exp, "\\+");
return i[0] + i[1];
}
}
class Minus extends Helpers implements Calculator{
@Override
public int calculate(String exp) {
int[] i = split(exp, "-");
return i[0] - i[1];
}
}
class Helpers{
public int[] split(String exp, String opt){
// 传入的分隔符应该为正则表达式
String[] split = exp.split(opt);
int a[] = new int[split.length];
a[0] = Integer.valueOf(split[0]);
a[1] = Integer.valueOf(split[1]);
return a;
}
}
观察者模式
所谓观察者模式,就是当一个对象变化时,其它依赖该对象的对象都会收到通知,并且随着变化,对象之间是一种一对多的关系