设计模式(三)结构模式和行为模式(代理模式、享元模式)(观察者模式、命令模式)

创建模式是创建对象
结构模式是组合对象
行为模式是模块和模块之间发生行为


结构模式
代理模式
优先浏览缩略图片
class ImageProxy{//代理类
private ImageDisplay id;
public ImageProxy(ImageDisplay id){
this.id = id;
}
public void displayImage(){
System.out.println("显示缩略图片");//基本不消耗内存
}
public void mousePress(){//鼠标点击缩略图
id.displayImage();
}
}


class ImageDisplay{
public void displayImage(){
System.out.println("显示实际图片");//消耗内存
}
}


public class Proxyl{
public static void main(String args[]){
ImageProxy proxy = new ImageProxy(new ImageDisplay());
proxy.displayImage();
proxy.mousePress();
}
}
改进代理模式
class ImageProxy implements IImage{//代理类
private IImage iimage;
public ImageProxy(IImage iimage){
this.iimage = iimage;
}
public void displayImage(){
System.out.println("显示缩略图片");//基本不消耗内存
}
public void mousePress(){//鼠标点击缩略图
iimage.displayImage();
}
}
interface IImage{
public abstract void displayImage();
}
class ImageDisplay implements IImage{
public void displayImage(){
System.out.println("显示实际图片");//消耗内存
}
}


public class Proxyl{
public static void main(String args[]){
ImageProxy proxy = new ImageProxy(new ImageDisplay());
proxy.displayImage();//显示缩略图片
proxy.mousePress();//显示实际图片
}
}
=======================================================================
享元模式
享元多而且小可以用
可以将相同字符串储存在池中,可以节省内存
class Word{
public String content;
public String key;
public Word(String content,String key){
System.out.println("");
this.content = content;
this.key = key;
}
}
//管理享元用的池,可以用HashMap存储
class WordPool{
private static HashMap pool = new HashMap();
public static Word getWord(String key,String content){
Word word = (Word)pool.get(key);;
if(word==null){
word = new Word(content.key);
pool.put(key.word);
}
return word;
}
}
public class Flyweight1{
public static void main(String args[]){
Word wl = WordPool.getWord("刘德华","001");
Word w2 = WordPool.getWord("张学友","002");
Word w3 = WordPool.getWord("刘德华","001");
}
}
*****************************************************************
行为模式
观察者模式
网址产品价格发生变化会通知会员
//产品类,如果changePrice调用,一定通知Customer
//目标:Product不用关心要通知谁,Customer也不关心是谁发出
//Product是被观察者,Customer是观察者
import java.util.*
class Product extends Observable{
private String pname;
private double price;
public Product(String pname,double price){
this.pname = pname;
this.price = price;
}
public void changePrice(double newPrice){
this.price = newPrice;
//通知观察者,设置变化点,表示要同事
this.setChanged();
//通知所有观察者并给消息
this.notifyObservers("通知:价格变化");
//自动调用观察的update方法
}
}
//顾客类
class Customer implements Observer{
//参数1是来源,2是值
public void update(Observable o,Object arg){
System.out.println(arg);
}
private String cname;
public Customer(String cname){
this.cname = cname;
}
public void printInfo(){
System.out.println(cname);
}


}


public class Observer1{
public static void main(String args[]){
Product pro = new Product("白菜",12);
Customer cus = new Customer("张三");
pro.addObserver(cus)//观察者和被观察者要绑定,可以设置多个观察者
pro.changePrice(13);
}
}
打印:通知:价格变化了
改进观察者模式
import java.util.*
class Product extends Observable{
private String pname;
private double price;
public void setObserver(Observer observer){//可以用spring
this.addObserver(observer);
}
public Product(String pname,double price){
this.pname = pname;
this.price = price;
}
public void changePrice(double newPrice){
this.price = newPrice;
//通知观察者,设置变化点,表示要同事
this.setChanged();
//通知所有观察者并给消息
this.notifyObservers("通知:价格变化");
//自动调用观察的update方法
}
}
//顾客类
class Customer implements Observer{
//参数1是来源,2是值
public void update(Observable o,Object arg){
System.out.println(arg);
}
private String cname;
public Customer(String cname){
this.cname = cname;
}
public void printInfo(){
System.out.println(cname);
}


}


public class Observer1{
public static void main(String args[]){
Product pro = new Product("白菜",12);
Customer cus = new Customer("张三");
pro.setObserver(cus)//观察者和被观察者要绑定,可以设置多个观察者
pro.changePrice(13);
}
}
**************************************************************************
命令模式
调用几个命令,命令来自不同模块,封装命令一起发出,隐藏命令的来源
interface Command{
public void executeCommand();
}
class Module1 implements Command{
public void fun1(){
System.out.println("一些功能");
}
//public void fun2()...
//以下功能在客户端调用 命令1 时调用
public void executeCommand(){
System.out.println("显示模块1");
}
}
class Module2 implements Command{
public void fun1(){
System.out.println("一些功能");
}
//public void fun2()...
//以下功能在客户端调用 命令2 时调用
public void executeCommand(){
System.out.println("显示模块2");
}
}


class CommandConstructor{
//将每一个模块和命令映射,不复杂生成模块,可以用配置文件对应
private static HashMap commandPool = new HashMap();
public static void constructCommandPool(){
//以下为测试用,实际中不一定在这里new,各个模块引用可能从外界传递过来
Module1 module1 = new Module1();
Module2 module2 = new Module2();
commandPool.put("命令1",module1);
commandPool.put("命令2",module2);
}
public static Command getCommand(String key){
return (Command)commandPool.get(key);
}
}


//客户端调用命令时,不用关心来源,只要调用就可以
public class Command1{
public static void main(String args[]){
String command = "命令1";
CommandConstructor.constructCommandPool();
Command c1 = CommandConstructor.getCommand(command);
cl.executeCommand();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值