设计模式(bridge模式,Adapter模式,Observe模式,Template模式)

android中读取位图bitmap时,分给虚拟机图片的堆栈8M的大小,很容易发生OutOfMemory异常


1, bridge模式:将抽象和实现分离出来。
它可以解决继承的缺点:如果继承层次很多,并且某一个方法在多层的类中可能出现变化,或者说这个方法不确定,如果某一个继承层次中的类想修改这个方法,就做不到。
因为这样可能对它的子类有影响。
这个时候就可以使用bridge模式,将这个不确定的方法抽象出来,如果要使用的时候搭一个桥过去,使用它。
例子:三种对话框Dialog,这三种对话框都可以实现隐藏hide,闪烁Shrink,跳跃Jump。如果不使用bridge模式需要定义九个类,
使用了bridge模式只需定义六个类。


interface IEffect {
public void doEffect();
}


class HideEffect implements IEffect{
public void doEffect(){
System.out.println("hide");
}
}
class ShrinkEffect implements IEffect{
public void doEffect(){
System.out.println("shrink");
}
}
class JumpEffect implements IEffect{
public void doEffect(){
System.out.println("Jump");
}
}


abstract class Dialog {
IEffect effectImpl;


Dialog(IEffect effectImpl){
this.effectImpl = effectImpl;
}

public IEffect getEffect() {
return this.effectImpl;
}


public abstract void doSomething();
}


class BigDialog extends Dialog {
BigDialog(IEffect effectImpl) {
super(effectImpl);
}


public void doSomething() {
System.out.println("big dialog: ");
this.effectImpl.doEffect();
}
}


class MiddleDialog extends Dialog {
MiddleDialog(IEffect effectImpl) {
super(effectImpl);
}


public void doSomething() {
System.out.println("middle dialog: ");
this.effectImpl.doEffect();
}
}


class SmallDialog extends Dialog {
SmallDialog(IEffect effectImpl) {
super(effectImpl);
}


public void doSomething() {
System.out.println("small dialog: ");
this.effectImpl.doEffect();
}
}


public class Client {
public static void main(String[] args) {
IEffect jump = new JumpEffect();
Dialog smallJump = new SmallDialog(jump);
smallJump.doSomething();
}
}


2, Adapter模式:
将多个不兼容的类组合在一起,利用了java中的多实现的技术。
例子:提供一个类既可以画圆型也可以画方形;
interface ICircle{
public void drawCircle();
}
interface ISquare{
public void drawSquare();
}
class Circle implements ICircle{
public void drawCircle() {
System.out.println("draw circle");
}
}
class Square implements ISquare{
public void drawSquare(){
System.out.println("draw square");
}
}


class HydridShape implements ICircle, ISquare{
private ICircle mCircle;
private ISquare mSquare;

public HydridShape(ICircle circle){
mCircle = circle;
}
public HydridShape(ISquare squre){
mSquare = squre;
}
public void drawCircle() {
mCircle.drawCircle();
}
public void drawSquare(){
mSquare.drawSquare();
}
}


public class Client {
public static void main(String[] args){
HydridShape hs = new HydridShape(new Circle());
HydridShape hs2 = new HydridShape(new Square());

hs.drawCircle();
hs2.drawSquare();
}
}


3,Observe模式:在被观察的对象中关联(保存)了所有观察它的对象,一旦被观察者的状态发生了变化,就通知它上面的所有观察者update
例子:一个购物网站的首页上是由许多的页面构成,但这些页面都会按照不同的方式显示某一个商品的信息。
商品就是被观察的对象,这些UI页面看着是观察者对象,一旦商品信息发生了变化,就需要立即更新这些UI
abstract class UI{
Product product;
UI(Product product){
this.product = product;
}

public abstract void update();
}


class UI1 extends UI{
UI1(Product product){
super(product);
}
public void update(){
System.out.println("update UI1 info - " + "name: " + product.getName() + ", price: " + product.getPrice());
}
}


class UI2 extends UI{
UI2(Product product){
super(product);
}
public void update(){
System.out.println("update UI2 info - " + "name: " + product.getName()  + ", price: " + product.getPrice());
}
}


class UI3 extends UI{
UI3(Product product){
super(product);
}
public void update(){
System.out.println("update UI3 info - " + "name: " + product.getName()  + ", price: " + product.getPrice());
}
}


abstract class Product{
private String name;
private String price;

List<UI> arrayList = null;
Product(String name, String price){
this.name = name;
this.price = price;
arrayList = new ArrayList<UI>();
}
public void addUI(UI ui){
arrayList.add(ui);
}
public void deleteUI(UI ui){
arrayList.remove(ui);
}
public String getName(){
return this.name;
}
public String getPrice(){
return this.price;
}
public void setName(String name){
this.name = name;
}
public void setPrice(String price){
this.price = price;
}
public void notifyUI(){
for(UI ui : arrayList){
ui.update();
}
}
public abstract void updateProduct(String name, String price);
}


class Laptop extends Product{
Laptop(String name, String price){
super(name, price);
}
public void updateProduct(String name, String price){
setName(name);
setPrice(price);
notifyUI();
}
}


public class Client{
public static void main(String[] args){
Product laptop = new Laptop("kindle", "900");
UI ui1 = new UI1(laptop);
UI ui2 = new UI2(laptop);
UI ui3 = new UI3(laptop);
laptop.addUI(ui1);
laptop.addUI(ui2);
laptop.addUI(ui3);
laptop.updateProduct("kindle", "1000");
}
}


4, Template模式:某一项任务可以分割成多个子任务,将某些任务抽象出来,让它的子类去完成。
比如:排序并打印结果这项任务,就将排序和打印结果分割出来,打印很简单就在父类中完成,排序比较复杂,而且有很多方法来实现,
就抽象出来,让子类根据不同的情况具体实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值