您的位置:首页 > 房产 > 建筑 > 一天了解23种设计模式

一天了解23种设计模式

2024/10/6 10:28:09 来源:https://blog.csdn.net/Wistain/article/details/139750372  浏览:    关键词:一天了解23种设计模式

  学习设计模式之前,我们要知道为什么需要设计模式?想想你在初中、高中时是怎么做数学题的?每遇到一个难题,你不需要重头开始思考解决方法,而是有一定的解题“套路”。设计模式就像是解题的“套路”,一个好的“套路”应该适用于大多数题目,设计模式也是如此,一个好的设计模式不仅可以解决目前的问题,还可以很好地应对将来的变化。对于Java而言,只有运用好设计模式才能迈进J2EE的门槛。用一句话概括:设计模式就是为了提高代码的复用性,降低程序的耦合。

A. 创建模式

  1. 单例模式(Singleton)

  单例模式的目的是保证一个类只有一个实例,并且提供一个接口使用该实例。
  最佳实践:建立目录;建立数据库连接。
  一般单例模式通常有两种形式:饿汉式、懒汉式。

//饿汉式,在类的加载阶段就会创建该类的实例对象
public class girlFriend {private String name;private girlFriend(String name){this.name = name;}private static girlFriend gf = new girlFriend("gf");public static girlFriend getInstance(){return gf;}
}
//懒汉式,在类的初始化阶段创建该类的实例对象
public class girlFriend {private String name;private girlFriend(String name) {this.name = name;}private static girlFriend gf = null;public static synchronized girlFriend getInstance() {if(gf == null){gf = new girlFriend("gf");}return gf;}
}

懒汉式和饿汉式的区别:

  • 创建对象时机不同。
  • 懒汉式是线程不安全的,需要加锁;饿汉式是线程安全的。
  2. 工厂模式(Factory)

  工厂模式用于创建实例对象,一般有三种形式:简单工厂模式、工厂模式、抽象工厂模式。
  最佳实践:对象创建需要重用;未知该类需要创建多少子类对象
  ①简单工厂模式:核心工厂根据不同参数创建不同的对象(直接生产产品)。

public class simpleFactory {public static product createProduct(int type) {if(type == 1) {return new product1();} else if(type == 2){return new product2();}return null;}public static void main(String[] args) {product product1 = simpleFactory.createProduct(1);product1.print();}
}abstract class product{public abstract void print();
}class product1 extends product{public void print(){System.out.println("产品1");}
}class product2 extends product{public void print(){System.out.println("产品2");}
}

  ②工厂模式:核心工厂的子类决定生产产品。核心工厂只需要定义创建对象的接口,这样可以使类的实例化延迟到其子类。

public class factory {public static void main(String[] args) {productFactory p1f = new product1Factory();product p = p1f.getProduct();p.print();}
}interface product{void print();
}interface productFactory{product getProduct();
}class product1 implements product{public void print(){System.out.println("产品1");}
}class product2 implements product{public void print(){System.out.println("产品2");}
}class product1Factory implements productFactory{@Overridepublic product getProduct(){return new product1();}
}class product2Factory implements productFactory{@Overridepublic product getProduct(){return new product2();}
}

  ③抽象工厂模式:具体工厂类可以创建多个对象。

public class factory {public static void main(String[] args) {specificFactory sf = new specificFactory();Computer legion = sf.getLegionComputer();Phone apple = sf.getApplePhone();legion.print();apple.print();}
}//产品1->电脑
interface Computer{void print();
}
class legion implements Computer{@Overridepublic void print() {System.out.println("legion");}
}//产品2->手机
interface Phone{void print();
}
class apple implements Phone{@Overridepublic void print() {System.out.println("apple");}
};//抽象工厂
interface abstractFactory{Computer getLegionComputer();Phone getApplePhone();
}//具体工厂
class specificFactory implements abstractFactory{@Overridepublic Computer getLegionComputer() {return new legion();}@Overridepublic Phone getApplePhone() {return new apple();}
}
  3. 建造者模式(Builder)

  建造者模式适用于将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的对象,解耦过程与产品。
  经典的Builder模式用Builder接口定义如何创建复杂对象,用Director类构建最后的复杂对象,即组装最后成品。
  最佳实践:创建属性很多的复杂对象,以下代码实现(最佳实践中并没有使用到Director类构建最后的对象,而是通过链式结构控制构建过程;也没有单独定义Builder接口,而是使用静态内部类代替Builder接口的实现类。这也说明了设计模式不是原封不动地套用,需要开发者在实践中具体问题具体分析)。

// Person类->含有很多字段的复杂对象
class Person {private String name;private int age;private String gender;private String address;private String phone;private String email;private int id;private String password;private String city;private String state;private Person(){}public static class personBuilder{ // 静态内部类,代替了Builder接口的实现类Person p = new Person();public personBuilder setInfo(String name, int age, String gender){p.name = name;p.age = age;p.gender = gender;return this;}public personBuilder setAddress(String address){p.address = address;return this;}public personBuilder setPhone(String phone){p.phone = phone;return this;}public personBuilder setAccount(int id, String password, String email, String state){p.id = id;p.password = password;p.email = email;p.state = state;return this;}public personBuilder setCity(String city){p.city = city;return this;}public Person build(){return p;}}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +", address='" + address + '\'' +", phone='" + phone + '\'' +", email='" + email + '\'' +", id=" + id +", password='" + password + '\'' +", city='" + city + '\'' +", state='" + state + '\'' +'}';}
}public class Builder{public static void main(String[] args) {//链式结构构建对象,代替Director类功能Person p = new Person.personBuilder().setInfo("wistain", 22, "male").setAccount(1, "123", "123456@qq.com", "在职").build();System.out.println(p);}
}

B. 结构模式

C. 行为模式

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com