赞
踩
开放封闭原则(Open Close Principle)
里氏代换原则(Liskov Substitution Principle)
依赖倒转原则(Dependence Inversion Principle)
接口隔离原则(Interface Segregation Principle)
迪米特法则(最少知道原则)(Demeter Principle)
单一职责原则(Principle of single responsibility)
优点:
缺点:
private static boolean flag = false; private Singleton() { if (flag == false) { flag = !flag; } else { throw new RuntimeException("单例模式被侵犯!"); } }
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token punctuation">}</span>
(主要使用懒汉和懒汉式)
package com.lijie; //饿汉式 public class Demo1 { // 类初始化时,会立即加载该对象,线程安全,调用效率高 private static Demo1 demo1 = new Demo1();
<span class="token keyword">private</span> <span class="token class-name">Demo1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"私有Demo1构造参数初始化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Demo1</span> <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> demo1<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Demo1</span> s1 <span class="token operator">=</span> <span class="token class-name">Demo1</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Demo1</span> s2 <span class="token operator">=</span> <span class="token class-name">Demo1</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
2.懒汉式
package com.lijie; //懒汉式 public class Demo2 { //类初始化时,不会初始化该对象,真正需要使用的时候才会创建该对象。 private static Demo2 demo2;
<span class="token keyword">private</span> <span class="token class-name">Demo2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"私有Demo2构造参数初始化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">static</span> <span class="token class-name">Demo2</span> <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span>demo2 <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> demo2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Demo2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> demo2<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Demo2</span> s1 <span class="token operator">=</span> <span class="token class-name">Demo2</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Demo2</span> s2 <span class="token operator">=</span> <span class="token class-name">Demo2</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
3.静态内部类
package com.lijie; // 静态内部类方式 public class Demo3 { private Demo3() { System.out.println("私有Demo3构造参数初始化"); }
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">SingletonClassInstance</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">Demo3</span> DEMO_3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Demo3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 方法没有同步</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Demo3</span> <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token class-name">SingletonClassInstance</span><span class="token punctuation">.</span>DEMO_3<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Demo3</span> s1 <span class="token operator">=</span> <span class="token class-name">Demo3</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Demo3</span> s2 <span class="token operator">=</span> <span class="token class-name">Demo3</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
4.枚举单例式
package com.lijie; //使用枚举实现单例模式 优点:实现简单、枚举本身就是单例,由jvm从根本上提供保障!避免通过反射和反序列化的漏洞 缺点没有延迟加载 public class Demo4 { public static Demo4 getInstance() { return Demo.INSTANCE.getInstance(); }
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Demo4</span> s1 <span class="token operator">=</span> <span class="token class-name">Demo4</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Demo4</span> s2 <span class="token operator">=</span> <span class="token class-name">Demo4</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//定义枚举</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">enum</span> <span class="token class-name">Demo</span> <span class="token punctuation">{<!-- --></span> INSTANCE<span class="token punctuation">;</span> <span class="token comment">// 枚举元素为单例</span> <span class="token keyword">private</span> <span class="token class-name">Demo4</span> demo4<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">Demo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"枚举Demo私有构造参数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> demo4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Demo4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Demo4</span> <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> demo4<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
5.双重检测锁方式
package com.lijie; //双重检测锁方式 public class Demo5 {
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">Demo5</span> demo5<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">Demo5</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"私有Demo4构造参数初始化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Demo5</span> <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span>demo5 <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span><span class="token class-name">Demo5</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span>demo5 <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> demo5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Demo5</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> demo5<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token class-name">Demo5</span> s1 <span class="token operator">=</span> <span class="token class-name">Demo5</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Demo5</span> s2 <span class="token operator">=</span> <span class="token class-name">Demo5</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
1.Spring IOC
2.为什么Spring IOC要使用工厂设计模式创建Bean呢
工厂模式分为简单工厂、工厂方法、抽象工厂模式
简单工厂 :用来生产同一等级结构中的任意产品。(不支持拓展增加产品)
工厂方法 :用来生产同一等级结构中的固定产品。(支持拓展增加产品)
抽象工厂 :用来生产不同产品族的全部产品。(不支持拓展增加产品;支持增加产品族)
我下面来使用代码演示一下:
什么是简单工厂模式
代码演示:
package com.lijie;
public interface Car {
public void run();
}
package com.lijie;
public class Bmw implements Car {
public void run() {
System.out.println("我是宝马汽车...");
}
}
package com.lijie;
public class AoDi implements Car {
public void run() {
System.out.println("我是奥迪汽车..");
}
}
package com.lijie;
public class CarFactory {
public static Car createCar(String name) {
if ("".equals(name)) {
return null;
}
if(name.equals("奥迪")){
return new AoDi();
}
if(name.equals("宝马")){
return new Bmw();
}
return null;
}
}
package com.lijie;
public class Client01 {
public static void main(String[] args) {
Car aodi =CarFactory.createCar("奥迪");
Car bmw =CarFactory.createCar("宝马");
aodi.run();
bmw.run();
}
}
单工厂的优点/缺点
什么是工厂方法模式
package com.lijie;
public interface Car {
public void run();
}
package com.lijie;
public interface CarFactory {
Car createCar();
}
package com.lijie;
public class AoDi implements Car {
public void run() {
System.out.println("我是奥迪汽车..");
}
}
package com.lijie;
public class Bmw implements Car {
public void run() {
System.out.println("我是宝马汽车...");
}
}
package com.lijie;
public class AoDiFactory implements CarFactory {
public Car createCar() {
return new AoDi();
}
}
package com.lijie;
public class BmwFactory implements CarFactory {
public Car createCar() {
return new Bmw();
}
}
package com.lijie;
public class Client {
public static void main(String[] args) {
Car aodi = new AoDiFactory().createCar();
Car jili = new BmwFactory().createCar();
aodi.run();
jili.run();
}
}
什么是抽象工厂模式
package com.lijie;
//汽车
public interface Car {
void run();
}
class CarA implements Car{
public void run() {
System.out.println("宝马");
}
}
class CarB implements Car{
public void run() {
System.out.println("摩拜");
}
}
package com.lijie;
//发动机
public interface Engine {
void run();
}
class EngineA implements Engine {
public void run() {
System.out.println("转的快!");
}
}
class EngineB implements Engine {
public void run() {
System.out.println("转的慢!");
}
}
package com.lijie; public interface TotalFactory { // 创建汽车 Car createChair(); // 创建发动机 Engine createEngine(); } //总工厂实现类,由他决定调用哪个工厂的那个实例 class TotalFactoryReally implements TotalFactory { public Engine createEngine() { return new EngineA(); } public Car createChair() { return new CarA(); } }
package com.lijie;
public class Test {
public static void main(String[] args) {
TotalFactory totalFactory2 = new TotalFactoryReally();
Car car = totalFactory2.createChair();
car.run();
TotalFactory totalFactory = new TotalFactoryReally();
Engine engine = totalFactory.createEngine();
engine.run();
}
}
什么是静态代理
package com.lijie;
//接口类
public class UserDao{
public void save() {
System.out.println("保存数据方法");
}
}
package com.lijie;
//运行测试类
public class Test{
public static void main(String[] args) {
UserDao userDao = new UserDao();
userDao.save();
}
}
修改代码,添加代理类
package com.lijie;
//代理类
public class UserDaoProxy extends UserDao {
private UserDao userDao;
public UserDaoProxy(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("开启事物...");
userDao.save();
System.out.println("关闭事物...");
}
}
//添加完静态代理的测试类
public class Test{
public static void main(String[] args) {
UserDao userDao = new UserDao();
UserDaoProxy userDaoProxy = new UserDaoProxy(userDao);
userDaoProxy.save();
}
}
缺点:每个需要代理的对象都需要自己重复编写代理,很不舒服,
优点:但是可以面相实际对象或者是接口的方式实现代理
什么是动态代理
package com.lijie;
//接口
public interface UserDao {
void save();
}
package com.lijie;
//接口实现类
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("保存数据方法");
}
}
package com.lijie; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; // 每次生成动态代理类对象时,实现了InvocationHandler接口的调用处理器对象 public class InvocationHandlerImpl implements InvocationHandler { // 这其实业务实现类对象,用来调用具体的业务方法 private Object target; // 通过构造函数传入目标对象 public InvocationHandlerImpl(Object target) { this.target = target; } //动态代理实际运行的代理方法 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("调用开始处理"); //下面invoke()方法是以反射的方式来创建对象,第一个参数是要创建的对象,第二个是构成方法的参数,由第二个参数来决定创建对象使用哪个构造方法 Object result = method.invoke(target, args); System.out.println("调用结束处理"); return result; } }
package com.lijie; import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) { // 被代理对象 UserDao userDaoImpl = new UserDaoImpl(); InvocationHandlerImpl invocationHandlerImpl = new InvocationHandlerImpl(userDaoImpl); //类加载器 ClassLoader loader = userDaoImpl.getClass().getClassLoader(); Class<?>[] interfaces = userDaoImpl.getClass().getInterfaces(); // 主要装载器、一组接口及调用处理动态代理实例 UserDao newProxyInstance = (UserDao) Proxy.newProxyInstance(loader, interfaces, invocationHandlerImpl); newProxyInstance.save(); } }
CGLIB动态代理原理:
代码演示:
package com.lijie;
//接口
public interface UserDao {
void save();
}
package com.lijie;
//接口实现类
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("保存数据方法");
}
}
package com.lijie; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; //代理主要类 public class CglibProxy implements MethodInterceptor { private Object targetObject; // 这里的目标类型为Object,则可以接受任意一种参数作为被代理类,实现了动态代理 public Object getInstance(Object target) { // 设置需要创建子类的类 this.targetObject = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); enhancer.setCallback(this); return enhancer.create(); } //代理实际方法 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("开启事物"); Object result = proxy.invoke(targetObject, args); System.out.println("关闭事物"); // 返回代理对象 return result; } }
package com.lijie;
//测试CGLIB动态代理
public class Test {
public static void main(String[] args) {
CglibProxy cglibProxy = new CglibProxy();
UserDao userDao = (UserDao) cglibProxy.getInstance(new UserDaoImpl());
userDao.save();
}
}
建造者模式通常包括下面几个角色:
使用场景:
package com.lijie;
//装备类
public class Arms {
//头盔
private String helmet;
//铠甲
private String armor;
//武器
private String weapon;
//省略Git和Set方法...........
}
package com.lijie;
public interface PersonBuilder {
void builderHelmetMurder();
void builderArmorMurder();
void builderWeaponMurder();
void builderHelmetYanLong();
void builderArmorYanLong();
void builderWeaponYanLong();
Arms BuilderArms(); //组装
}
package com.lijie; public class ArmsBuilder implements PersonBuilder { private Arms arms; //创建一个Arms实例,用于调用set方法 public ArmsBuilder() { arms = new Arms(); } public void builderHelmetMurder() { arms.setHelmet("夺命头盔"); } public void builderArmorMurder() { arms.setArmor("夺命铠甲"); } public void builderWeaponMurder() { arms.setWeapon("夺命宝刀"); } public void builderHelmetYanLong() { arms.setHelmet("炎龙头盔"); } public void builderArmorYanLong() { arms.setArmor("炎龙铠甲"); } public void builderWeaponYanLong() { arms.setWeapon("炎龙宝刀"); } public Arms BuilderArms() { return arms; } }
package com.lijie; public class PersonDirector { //组装 public Arms constructPerson(PersonBuilder pb) { pb.builderHelmetYanLong(); pb.builderArmorMurder(); pb.builderWeaponMurder(); return pb.BuilderArms(); } //这里进行测试 public static void main(String[] args) { PersonDirector pb = new PersonDirector(); Arms arms = pb.constructPerson(new ArmsBuilder()); System.out.println(arms.getHelmet()); System.out.println(arms.getArmor()); System.out.println(arms.getWeapon()); } }
例如:
package com.lijie; //模板方法 public abstract class RestaurantTemplate { // 1.看菜单 public void menu() { System.out.println("看菜单"); } // 2.点菜业务 abstract void spotMenu(); // 3.吃饭业务 public void havingDinner(){ System.out.println("吃饭"); } // 3.付款业务 abstract void payment(); // 3.走人 public void GoR() { System.out.println("走人"); } //模板通用结构 public void process(){ menu(); spotMenu(); havingDinner(); payment(); GoR(); } }
package com.lijie;
public class RestaurantGinsengImpl extends RestaurantTemplate {
void spotMenu() {
System.out.println("人参");
}
void payment() {
System.out.println("5快");
}
}
package com.lijie;
public class RestaurantLobsterImpl extends RestaurantTemplate {
void spotMenu() {
System.out.println("龙虾");
}
void payment() {
System.out.println("50块");
}
}
package com.lijie;
public class Client {
public static void main(String[] args) {
//调用第一个模板实例
RestaurantTemplate restaurantTemplate = new RestaurantGinsengImpl();
restaurantTemplate.process();
}
}
package com.lijie;
//阿里短信消息
public interface AliSmsService {
void sendSms();
}
package com.lijie;
public class AliSmsServiceImpl implements AliSmsService {
public void sendSms() {
System.out.println("阿里短信消息");
}
}
package com.lijie;
//发送邮件消息
public interface EamilSmsService {
void sendSms();
}
package com.lijie;
public class EamilSmsServiceImpl implements EamilSmsService{
public void sendSms() {
System.out.println("发送邮件消息");
}
}
package com.lijie;
//微信消息推送
public interface WeiXinSmsService {
void sendSms();
}
package com.lijie;
public class WeiXinSmsServiceImpl implements WeiXinSmsService {
public void sendSms() {
System.out.println("发送微信消息推送");
}
}
package com.lijie; public class Computer { AliSmsService aliSmsService; EamilSmsService eamilSmsService; WeiXinSmsService weiXinSmsService; public Computer() { aliSmsService = new AliSmsServiceImpl(); eamilSmsService = new EamilSmsServiceImpl(); weiXinSmsService = new WeiXinSmsServiceImpl(); } //只需要调用它 public void sendMsg() { aliSmsService.sendSms(); eamilSmsService.sendSms(); weiXinSmsService.sendSms(); } }
package com.lijie;
public class Client {
public static void main(String[] args) {
//普通模式需要这样
AliSmsService aliSmsService = new AliSmsServiceImpl();
EamilSmsService eamilSmsService = new EamilSmsServiceImpl();
WeiXinSmsService weiXinSmsService = new WeiXinSmsServiceImpl();
aliSmsService.sendSms();
eamilSmsService.sendSms();
weiXinSmsService.sendSms();
//利用外观模式简化方法
new Computer().sendMsg();
}
}
我们Spring框架中的多例就是使用原型。
package com.lijie; import java.util.ArrayList; public class User implements Cloneable { private String name; private String password; private ArrayList<String> phones; protected User clone() { try { User user = (User) super.clone(); //重点,如果要连带引用类型一起复制,需要添加底下一条代码,如果不加就对于是复制了引用地址 user.phones = (ArrayList<String>) this.phones.clone();//设置深复制 return user; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return null; } //省略所有属性Git Set方法...... }
package com.lijie; import java.util.ArrayList; public class Client { public static void main(String[] args) { //创建User原型对象 User user = new User(); user.setName("李三"); user.setPassword("123456"); ArrayList<String> phones = new ArrayList<>(); phones.add("17674553302"); user.setPhones(phones); //copy一个user对象,并且对象的属性 User user2 = user.clone(); user2.setPassword("654321"); //查看俩个对象是否是一个 System.out.println(user == user2); //查看属性内容 System.out.println(user.getName() + " | " + user2.getName()); System.out.println(user.getPassword() + " | " + user2.getPassword()); //查看对于引用类型拷贝 System.out.println(user.getPhones() == user2.getPhones()); } }
//默认引用类型为浅复制,这是设置了深复制
user.phones = (ArrayList<String>) this.phones.clone();
package com.lijie;
//策略模式 定义抽象方法 所有支持公共接口
abstract class PayStrategy {
// 支付逻辑方法
abstract void algorithmInterface();
}
package com.lijie;
class PayStrategyA extends PayStrategy {
void algorithmInterface() {
System.out.println("微信支付");
}
}
package com.lijie;
class PayStrategyB extends PayStrategy {
void algorithmInterface() {
System.out.println("支付宝支付");
}
}
package com.lijie;
class PayStrategyC extends PayStrategy {
void algorithmInterface() {
System.out.println("银联支付");
}
}
package com.lijie;// 使用上下文维护算法策略
class Context {
PayStrategy strategy;
public Context(PayStrategy strategy) {
this.strategy = strategy;
}
public void algorithmInterface() {
strategy.algorithmInterface();
}
}
package com.lijie;
class ClientTestStrategy {
public static void main(String[] args) {
Context context;
//使用支付逻辑A
context = new Context(new PayStrategyA());
context.algorithmInterface();
//使用支付逻辑B
context = new Context(new PayStrategyB());
context.algorithmInterface();
//使用支付逻辑C
context = new Context(new PayStrategyC());
context.algorithmInterface();
}
}
实现有两种方式:
package com.lijie;
//观察者的接口,用来存放观察者共有方法
public interface Observer {
// 观察者方法
void update(int state);
}
package com.lijie;
// 具体观察者
public class ObserverImpl implements Observer {
// 具体观察者的属性
private int myState;
public void update(int state) {
myState=state;
System.out.println("收到消息,myState值改为:"+state);
}
public int getMyState() {
return myState;
}
}
package com.lijie; import java.util.Vector; //定义主题,以及定义观察者数组,并实现增、删及通知操作。 public class Subjecct { //观察者的存储集合,不推荐ArrayList,线程不安全, private Vector<Observer> list = new Vector<>(); // 注册观察者方法 public void registerObserver(Observer obs) { list.add(obs); } // 删除观察者方法 public void removeObserver(Observer obs) { list.remove(obs); } // 通知所有的观察者更新 public void notifyAllObserver(int state) { for (Observer observer : list) { observer.update(state); } } }
package com.lijie;
//具体主题
public class RealObserver extends Subjecct {
//被观察对象的属性
private int state;
public int getState(){
return state;
}
public void setState(int state){
this.state=state;
//主题对象(目标对象)值发生改变
this.notifyAllObserver(state);
}
}
package com.lijie; public class Client { public static void main(String[] args) { // 目标对象 RealObserver subject = new RealObserver(); // 创建多个观察者 ObserverImpl obs1 = new ObserverImpl(); ObserverImpl obs2 = new ObserverImpl(); ObserverImpl obs3 = new ObserverImpl(); // 注册到观察队列中 subject.registerObserver(obs1); subject.registerObserver(obs2); subject.registerObserver(obs3); // 改变State状态 subject.setState(300); System.out.println("obs1观察者的MyState状态值为:"+obs1.getMyState()); System.out.println("obs2观察者的MyState状态值为:"+obs2.getMyState()); System.out.println("obs3观察者的MyState状态值为:"+obs3.getMyState()); // 改变State状态 subject.setState(400); System.out.println("obs1观察者的MyState状态值为:"+obs1.getMyState()); System.out.println("obs2观察者的MyState状态值为:"+obs2.getMyState()); System.out.println("obs3观察者的MyState状态值为:"+obs3.getMyState()); } }
史上最全Java面试宝典,BAT大厂面试必备。整理不易,建议先关注点赞加收藏
序号 | 名称 | 地址 |
---|---|---|
1 | Java基础面试题(91道含答案) | (点击查看) |
2 | Java并发编程面试题 (123道含答案) | (点击查看) |
3 | Java异常面试题 (33道含答案) | (点击查看) |
4 | Java虚拟机(JVM)面试题(51道含答案) | (点击查看) |
5 | Java集合面试题(52道含答案) | (点击查看) |
6 | Linux面试题(50道含答案) | (点击查看) |
7 | Memcache面试题(23道含答案) | (点击查看) |
8 | Mybatiss面试题 (37道含答案) | (点击查看) |
9 | MySQL面试题(40道含答案) | (点击查看) |
10 | Netty面试题(49道含答案) | (点击查看) |
11 | Nginx面试题(23道含答案) | (点击查看) |
12 | RabbitMQ面试题(22道含答案) | (点击查看) |
13 | Redis面试题(70道含答案) | (点击查看) |
14 | SpringBoot面试题(44道含答案) | (点击查看) |
15 | SpringCloud面试题(49道含答案) | (点击查看) |
16 | SpringMVC面试题(29道含答案) | (点击查看) |
17 | Spring面试题(75道含答案) | (点击查看) |
18 | TCP、UDP、Socket、Http网络编程面试题(47道含答案) | (点击查看) |
19 | Tomcat面试题(15道含答案) | (点击查看) |
20 | Zookeeper面试题(28道含答案) | (点击查看) |
21 | 多线程面试题(60道含答案) | (点击查看) |
22 | 设计模式面试题(14道含答案) | (点击查看) |
23 | BIO、NIO、AIO、Netty面试题(35道含答案) | (点击查看) |
24 | Dubbo面试题(47道含答案) | (点击查看) |
25 | ElasticSearch面试题(31道含答案) | (点击查看) |
26 | Git常用命令(63条) | (点击查看) |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。