设计模式之适配器模式与装饰者模式

时间:2022-06-27 00:20:44

知识来源极客学院

适配器模式的基本原理

现实生活例子,香港插头与内地插座,使用适配器使得香港插头可以与内地插座连接。

代码示例;

public interface HKPlugs {
public void output();
}
class SmallHKPlugs implements HKPlugs{
@Override
public void output() {
System.out.println("Small HongKong Plugs!");
}
}
public interface MLPlugs {
public void output();
}
class SmallMLPlugs implements MLPlugs{
@Override
public void output() {
System.out.println("Small Mainland Plugs!");
}
}
public class MLPlugsAdapter implements HKPlugs {
private MLPlugs mlPlugs;
public MLPlugsAdapter(MLPlugs mlPlugs){
this.mlPlugs=mlPlugs;
}
@Override
public void output(){
mlPlugs.output();
}
}
public class Test {
public static void main(String[] args) {
HKPlugs mlPlugs=new MLPlugsAdapter(new SmallMLPlugs());
mlPlugs.output();
}
}

output
Small Mainland Plugs!


设计模式之适配器模式与装饰者模式

对象适配器与类适配器

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

类适配器

public class MLPlugsAdapter extends SmallMLPlugs implements HKPlugs {
@Override
public void output(){
super.output();
}
}
public class Test {
public static void main(String[] args) {
HKPlugs mlPlugs=new MLPlugsAdapter();
mlPlugs.output();
}
}

output
Small Mainland Plugs!


从枚举器到迭代器的适配

设计模式之适配器模式与装饰者模式

public class EnumerationIterator implements Iterator<Object> {
private Enumeration enumeration;
public EnumerationIterator(Enumeration enumeration) {
this.enumeration=enumeration;
}
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}

@Override
public Object next() {
return enumeration.nextElement();
}
public void remove(){
throw new UnsupportedOperationException();
}
}

装饰者模式

装饰者模式原理

一、咖啡厅订单系统项目

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

设计模式之适配器模式与装饰者模式

装饰者模式示例演示

public abstract class Drink {
public String description="";
private float price=0f;
public String getDescription() {
return description+"-"+this.getPrice();
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public abstract float cost();
}
public class Coffee extends Drink{
@Override
public float cost(){
return super.getPrice();
}
}
public class Decaf extends Coffee{
public Decaf() {
super.setDescription("Decaf");
super.setPrice(3.0f);
}
}
public class Espresso extends Coffee {
public Espresso() {
super.setDescription("Espresso");
super.setPrice(4.0f);
}
}
public class Decorator extends Drink {
private Drink obj;
public Decorator(Drink obj) {
this.obj=obj;
}
@Override
public float cost() {
return super.getPrice()+obj.cost();
}
public String getDescription(){
return super.description+"-"+super.getPrice()+"&&"+obj.getDescription();
}
}
public class Chocolate extends Decorator {
public Chocolate(Drink obj) {
super(obj);
super.setDescription("Chocolate");
super.setPrice(3.0f);
}

}
public class Milk extends Decorator {
public Milk(Drink obj){
super(obj);
super.setDescription("Milk");
super.setPrice(2.0f);
}
}
public class CoffeeBar {
public static void main(String[] args) {
Drink order=new Decaf();
System.out.println(order.cost());
System.out.println(order.getDescription());
order=new Milk(order);
order=new Chocolate(order);
System.out.println(order.getPrice());
System.out.println(order.getDescription());
}
}

output

3.0
Decaf-3.0
3.0
Chocolate-3.0&&Milk-2.0&&Decaf-3.0

装饰者模式介绍

Java内置装饰者

设计模式之适配器模式与装饰者模式

编写自己的Java I/O装饰者

public class UpperCaseInputStream extends FilterInputStream {

protected UpperCaseInputStream(InputStream in) {
super(in);
}
public int read() throws IOException{
int c=super.read();
return c==-1?c:Character.toUpperCase((char)c);
}
public int read(byte[]b,int offset,int len) throws IOException{
int result=super.read(b, offset, len);
for(int i=0;i<result;i++){
b[i]=(byte)Character.toUpperCase((char)b[i]);
}
return result;
}
}

public class InputTest {
public static void main(String[] args) {
int c;
try {
InputStream in=new UpperCaseInputStream(new BufferedInputStream(new FileInputStream("E:\\test.txt")));
while((c=in.read())>=0){
System.out.print((char)c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


output

SAHIUDSHAIUHISDHAUIH