目录
- 设计模式
- 1.单例模式
- 2.代理模式
- 3.观察者模式
- 4.模板模式
- ①使用抽象类的模板模式
- ②使用接口的模板模式
设计模式
1.单例模式
public class SingonDemo {
private static SingonDemo singonDemo = new SingonDemo();
private SingonDemo(){
}
public static SingonDemo getInstance(){
return singonDemo;
}
public static void main(String[] args) {
SingonDemo singonDemo1 = SingonDemo.getInstance();
SingonDemo singonDemo2 = SingonDemo.getInstance();
System.out.println(singonDemo1==singonDemo2);
}
}
2.代理模式
代理模式可以在不改变源代码的情况下对目标对象进行访问控制和功能扩展
目标对象(RealSubject) 实际完成功能的对象
代理对象(Proxy)是目标对象的替身,用户和RealSubject的交互必须通过Proxy。
1.代理对象完成前置工作
2.目标对象完成实际工作
3.代理对象完成后置工作
public interface Subject {
void work();
}
public class RealSubject implements Subject {
@Override
public void work() {
System.out.println("RealSubject work");
}
}
public class ProxySubject implements Subject{
private RealSubject realSubject ;
public ProxySubject() {
realSubject = new RealSubject();
}
@Override
public void work() {
System.out.println("before work");
realSubject.work();;
System.out.println("after work");
}
public static void main(String[] args) {
Subject proxySubject = new ProxySubject();
proxySubject.work();
}
}
Prxoy和RealSubject类都实现了Subject接口。这样用户就可以像处理RealSubject一样处理Proxy对象;用户与RealSubject的交互都必须通过Proxy,任何用到RealSubject的地方,都可以用Proxy取代,RealSubject是真正做事的对象。Proxy可以对RealSubject进行访问控制和功能扩展。
创建RealSubjct对象,通常由Proxy负责。
Proxy持有对RealSubject的引用。所以必要时可以将请求转发给RealSubject.
3.观察者模式
观察者模式就是发布订阅模式,发布者发布消息后,订阅者就可以得到消息通知。
发布者
抽象的观察者
具体的观察者
举例:有一个气象站,每天会给观察者推送天气预报
//天气数据
public class WeatherData {
//温度
private int temperature;
//湿度
private int windPower;
public WeatherData() {
}
public WeatherData(int temperature, int windPower) {
this.temperature = temperature;
this.windPower = windPower;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public int getWindPower() {
return windPower;
}
public void setWindPower(int windPower) {
this.windPower = windPower;
}
}
//抽象的观察者
public interface WeatherObserve {
//更新天气数据
void update(WeatherData data);
}
public class RealObserve implements WeatherObserve{
private String name;
public RealObserve(String name) {
this.name = name;
}
@Override
public void update(WeatherData data) {
System.out.println("ttemperature==="+data.getTemperature());
System.out.println("windPower===="+data.getWindPower());
}
}
public class WeatherStation {
public WeatherObserve[] observes = new WeatherObserve[10];
private int count = 0;
public WeatherStation() {
}
//添加观察者 对象订阅
public void registerObserve(WeatherObserve observe) {
if (count <= 9) {
observes[count] = observe;
count++;
}
}
//发布订阅
public void noticeObserve(WeatherData data) {
for (int i = 0; i < count; i++) {
observes[i].update(data);
System.out.println("==========发布成功===========");
}
}
}
public class Test {
public static void main(String[] args) {
WeatherStation weatherStation = new WeatherStation();
weatherStation.registerObserve( new RealObserve("observe1"));
weatherStation.registerObserve(new RealObserve("observe2"));
weatherStation.registerObserve(new RealObserve("observe3"));
WeatherData weatherData = new WeatherData(10, 2);
weatherStation.noticeObserve(weatherData);
}
}
4.模板模式
模板模式就是预先定义一个模板,这个模板包含了一些通用的功能,而加你个某些特定的实现交给子类或者接口来完成。
去银行办理业务:
任何办理的业务都需要:
1.排号 2 办理具体的业务 3.评价
①使用抽象类的模板模式
public abstract class BankBusinessTemplate {
//取号
protected long takeNumber(){
int r = (int) (Math.random() * 1000);
long number = System.currentTimeMillis() + r;
return number;
}
//保存客户反馈
protected void saveEvaluation(long number, String evaluation){
System.out.println("号码: "+ number+ "的评价是: "+ evaluation);
}
//具体的业务,声明抽象方法,交给子类来做
protected abstract String doAction(long number);
//业务流程
public void business(){
long nunmber = takeNumber();
String evaluation = doAction(nunmber);
saveEvaluation(nunmber, evaluation);
}
}
public class NewCard extends BankBusinessTemplate {
@Override
protected String doAction(long number) {
System.out.println("办理开卡业务");
return "五星好评";
}
}
public class LossCard extends BankBusinessTemplate {
@Override
protected String doAction(long number) {
System.out.println("办理挂失的业务");
return "四星好评";
}
}
public class BankTemplateTest {
public static void main(String[] args) {
NewCard newCard = new NewCard();
newCard.business();
LossCard lossCard = new LossCard();
lossCard.business();
}
}
②使用接口的模板模式
public interface Action {
/**
* 办理实际业务
* @param number
* @return
*/
public String doAction(long number);
}
public class LossCard implements Action {
@Override
public String doAction(long number) {
System.out.println("办理挂失的业务");
return "四星好评";
}
}
public class NewCard implements Action {
@Override
public String doAction(long number) {
System.out.println("办理开卡业务");
return "五星好评";
}
}
public class BankBusinessTemplate {
//取号
protected long takeNumber(){
int r = (int) (Math.random() * 1000);
long number = System.currentTimeMillis() + r;
return number;
}
//保存客户反馈
protected void saveEvaluation(long number, String evaluation){
System.out.println("号码: "+ number+ "的评价是: "+ evaluation);
}
//业务流程
public void business(Action action){
long nunmber = takeNumber();
String evaluation = action.doAction(nunmber);
saveEvaluation(nunmber, evaluation);
}
}
public class BankTemplateTest {
public static void main(String[] args) {
BankBusinessTemplate bankBusiness = new BankBusinessTemplate();
Action action1 = new NewCard();
bankBusiness.business(action1);
Action action2 = new LossCard();
bankBusiness.business(action2);
}
}
代码下载:
https://github.com/hufanglei/daily-code