设计模式(bridge模式,Adapter模式,Observe模式,Template模式)
2014-08-25 23:57
281 查看
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模式:某一项任务可以分割成多个子任务,将某些任务抽象出来,让它的子类去完成。
比如:排序并打印结果这项任务,就将排序和打印结果分割出来,打印很简单就在父类中完成,排序比较复杂,而且有很多方法来实现,
就抽象出来,让子类根据不同的情况具体实现
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模式:某一项任务可以分割成多个子任务,将某些任务抽象出来,让它的子类去完成。
比如:排序并打印结果这项任务,就将排序和打印结果分割出来,打印很简单就在父类中完成,排序比较复杂,而且有很多方法来实现,
就抽象出来,让子类根据不同的情况具体实现
相关文章推荐
- 设计模式笔记(4 ADAPTER & BRIDGE)
- 设计模式笔记之 - Abstract Server & Adapter & Bridge
- 几种结构型的设计模式概析【上】——Bridge模式,Adapter模式,Decorator模式【C++实现】
- 设计模式笔记(4 ADAPTER & BRIDGE)
- 设计模式之adapter bridge模式
- Java设计模式(三)Adapter(适配器),Bridge(桥接)
- 设计模式二,Proxy,State,Adapter,Template Method
- [GoF设计模式]Bridge模式和Template模式的C++实现
- Java设计模式 Bridge(桥接)Adapter,command,State,Mediator,Facade
- 设计模式笔记(4 ADAPTER & BRIDGE)
- 设计模式之Adapter(适配器)
- Java设计模式:Adapter(适配器)
- 设计模式二:bridge
- 设计模式----Bridge模式
- 设计模式——Bridge模式
- 设计模式之桥接(Bridge)---对象结构性模式
- 设计模式袖珍版 连续转载之 - Bridge
- 学习设计模式之Bridge模式
- 设计模式——Adapter模式
- 设计模式之Bridge