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

来源:互联网 发布:时时彩遗漏数据软件 编辑:程序博客网 时间:2024/06/05 07:41
创建模式是创建对象
结构模式是组合对象
行为模式是模块和模块之间发生行为


结构模式
代理模式
优先浏览缩略图片
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
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 户外烧烤菜单 烧烤制作方法 海鲜烧烤菜单 烧烤架子多少钱 烧烤用什么油 烧烤都有什么 烧烤食材清单 特色烧烤品种 烧烤可以烤什么 烧烤食材大全 烧烤大全及做法 烧烤烤什么好吃 烧烤有什么危害 烧烤调料怎么配 烧烤食物清单 烧烤配料有哪些 烧烤料怎么配 哪里学烧烤技术 烧烤一般有哪些食材 无烟烧烤设备价格 附近的烧烤店 烧烤技术培训哪家好 烧烤怎么做的 纸上烧烤技术培训 烧烤的做法大全 烧烤培训哪里好 烧烤技术培训班 哪里有学烧烤的地方 烧烤车多少钱一辆 烧烤羊肉串配料 烧烤调味料配方 如何开烧烤摊 烧烤菜谱大全 烧烤技术哪里学 学做烧烤多少钱 烧烤香料配方 学习烧烤要多少钱 烧烤技术配方大全 哪里有学烧烤 烧烤准备什么食材 烧烤材料清单及价格