结构型模式:
适配器模式
桥接模式
装饰模式
组合模式
外观模式
享元模式
代理模式
适配器模式
例:普通电源是220V,手机充电需要5V,需要一个适配器将电源电压转换。
将220V对象交给电压转换器
将电压转换器交给手机的充电方法

IVoltage5V
package com.company.demo06adapter;
public interface IVoltage5V {
public int output5V();
}
Voltage220V
package com.company.demo06adapter;
public class Voltage220V {
public int output() {
System.out.println("输出220V电压");
return 220;
}
}
VoltageAdapter
package com.company.demo06adapter;
public class VoltageAdapter implements IVoltage5V {
private Voltage220V v;
public VoltageAdapter(Voltage220V v) {
this.v = v;
}
@Override
public int output5V() {
if (v == null) {
System.out.println("未接插口无法充电");
return 0;
} else {
int output = v.output();
System.out.println("电源适配");
System.out.println("适配完成,输出电压为:" + output);
return output / 44;
}
}
}
Phone
package com.company.demo06adapter;
public class Phone {
void getcharge(IVoltage5V i){
if (i.output5V()==5){
System.out.println("成功充电");
}else {
System.out.println("电压错误无法充电");
}
}
}
Test
package com.company.demo06adapter;
public class Test {
public static void main(String[] args) {
Phone p = new Phone();
p.getcharge(new VoltageAdapter(new Voltage220V()));
}
}
桥接模式
例:市面上共有三种手机:大屏,中屏,小屏手机,同时有三家手机品牌:xiaomi,oppo,vivo。

Brand
package com.company.demo07bridge;
public interface Brand {
void type();
}
Oppo
package com.company.demo07bridge;
public class Oppo implements Brand {
@Override
public void type() {
System.out.println("欢迎使用oppo手机");
}
}
Vivo
package com.company.demo07bridge;
public class Vivo implements Brand {
@Override
public void type() {
System.out.println("欢迎使用vivo手机");
}
}
Xiaomi
package com.company.demo07bridge;
public class Xiaomi implements Brand {
@Override
public void type() {
System.out.println("欢迎使用xiaomi手机");
}
}
Cellphone
package com.company.demo07bridge;
public abstract class Cellphone {
//组合品牌
private Brand brand;
//构造器
public Cellphone(Brand brand) {
this.brand = brand;
}
protected void type(){
this.brand.type();
}
}
SmallScreen
package com.company.demo07bridge;
public class SmallScreen extends Cellphone{
public SmallScreen(Brand brand) {
super(brand);
}
public void type(){
super.type();
System.out.println("小屏幕手机");
}
}
MiddleScreen
package com.company.demo07bridge;
public class MiddleScreen extends Cellphone{
public MiddleScreen(Brand brand) {
super(brand);
}
public void type(){
super.type();
System.out.println("中屏幕手机");
}
}
BigScreen
package com.company.demo07bridge;
public class BigScreen extends Cellphone {
public BigScreen(Brand brand) {
super(brand);
}
public void type() {
super.type();
System.out.println("大屏幕手机");
}
}
Test
package com.company.demo07bridge;
public class Test {
public static void main(String[] args) {
Cellphone oppo1=new BigScreen(new Oppo());
oppo1.type();
Cellphone xiaomi1=new SmallScreen(new Xiaomi());
xiaomi1.type();
}
}
装饰模式
一个类可以有很多不同的类进行修饰
例:煎饼,可以加鸡蛋,火腿肠,培根,蔬菜,加不同的东西,会有不同的价格
创建一个顾客对象
顾客对象购买了相应的产品

HandPancake
package com.company.demo10decorator;
public interface HandPancake {
/**
* 提供手抓饼
*/
String offerHandPancake();
/**计算手抓饼的价格
* @return
*/
Integer calcCost();
}
Decorator
package com.company.demo10decorator.dec;
import com.company.demo10decorator.HandPancake;
/**
* Created by noteless on 2018/9/6.
* Description:装饰器类实现了手抓饼接口,具有了手抓饼的类型
*/
public abstract class Decorator implements HandPancake {
private HandPancake handPancake;
protected Decorator(HandPancake handPancake){
this.handPancake = handPancake;
}
/**提供手抓饼
* @return
*/
@Override
public String offerHandPancake() {
return handPancake.offerHandPancake();
}
/**提供手抓饼的价格
* @return
*/
@Override
public Integer calcCost() {
return handPancake.calcCost();
}
}
Bacon
package com.company.demo10decorator.dec;
import com.company.demo10decorator.HandPancake;
public class Bacon extends Decorator {
public Bacon(HandPancake handPancake){
super(handPancake);
}
@Override
public String offerHandPancake() {
return super.offerHandPancake()+" 加培根";
}
@Override
public Integer calcCost() {
return super.calcCost()+4;
}
}
Egg
package com.company.demo10decorator.dec;
import com.company.demo10decorator.HandPancake;
public class Egg extends Decorator {
public Egg(HandPancake handPancake){
super(handPancake);
}
@Override
public String offerHandPancake() {
return super.offerHandPancake()+"加鸡蛋";
}
@Override
public Integer calcCost() {
return super.calcCost()+2;
}
}
Sausage
package com.company.demo10decorator.dec;
import com.company.demo10decorator.HandPancake;
public class Sausage extends Decorator {
public Sausage(HandPancake handPancake){
super(handPancake);
}
@Override
public String offerHandPancake() {
return super.offerHandPancake()+" 加香肠";
}
@Override
public Integer calcCost() {
return super.calcCost()+3;
}
}
Vegetable
package com.company.demo10decorator.dec;
import com.company.demo10decorator.HandPancake;
public class Vegetable extends Decorator {
public Vegetable(HandPancake handPancake) {
super(handPancake);
}
@Override
public String offerHandPancake() {
return super.offerHandPancake() + " 加青菜";
}
@Override
public Integer calcCost() {
return super.calcCost() + 1;
}
}
NotelessHandPancake
package com.company.demo10decorator;
public class NotelessHandPancake implements HandPancake {
/**
* 提供noteless 家的手抓饼一份
*/
@Override
public String offerHandPancake() {
return " noteless 家的手抓饼";
}
/**计算 noteless 家 一份手抓饼的价格
* @return
*/
@Override
public Integer calcCost() {
return 3;
}
}
Customer
package com.company.demo10decorator;
public class Customer {
private String name;
Customer(String name) {
this.name = name;
}
public void buy(HandPancake handPancake) {
System.out.println(name + "购买了 : " + handPancake.offerHandPancake() +
" 一份, 花了 : " + handPancake.calcCost() + "块钱~");
System.out.println();
}
}
Test
package com.company.demo10decorator;
import com.company.demo10decorator.dec.Bacon;
import com.company.demo10decorator.dec.Egg;
import com.company.demo10decorator.dec.Sausage;
import com.company.demo10decorator.dec.Vegetable;
/**
* 手抓饼3块
* Sausage 烤肠 3块
* Bacon 培根 4块
* Egg 鸡蛋2块
* Vegetable 青菜 1块
*/
public class Test {
public static void main(String[] strings) {
//有一个顾客wenwen,他想吃手抓饼了,他加了一根烤肠 又加了培根
Customer customerC = new Customer("wenwen");
customerC.buy(new Bacon(new Sausage(new NotelessHandPancake())));
}
}
组合模式
例:创建文件类,可以是文件夹,图片,文本,视频

File
package com.company.demo09composite;
public abstract class File {
String name;
public File(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void display();
}
Folder
package com.company.demo09composite;
import java.util.ArrayList;
import java.util.List;
public class Folder extends File {
private List<File> files;
public Folder(String name) {
super(name);
files=new ArrayList<>();
}
@Override
public void display() {
for(File file : files){
file.display();
}
}
public void add(File file){
files.add(file);
}
public void remove(File file){
files.remove(file);
}
}
Image
package com.company.demo09composite;
class Image extends File{
public Image(String name) {
super(name);
}
public void display() {
System.out.println("这是图像文件,文件名:" + super.getName());
}
}
Text
package com.company.demo09composite;
public class Text extends File{
public Text(String name) {
super(name);
}
public void display() {
System.out.println("这是文本文件,文件名:" + super.getName());
}
}
Video
package com.company.demo09composite;
public class Video extends File {
public Video(String name) {
super(name);
}
public void display() {
System.out.println("这是影像文件,文件名:" + super.getName());
}
}
Test
package com.company.demo09composite;
public class Test {
public static void main(String[] args) {
Folder f = new Folder("课程");
Folder f1 = new Folder("一");
Folder f2 = new Folder("二");
Folder f3 = new Folder("三");
f.add(f1);
f.add(f2);
f.add(f3);
Text txt1 = new Text("text1.txt");
Text txt2 = new Text("text2.txt");
Text txt3 = new Text("text3.txt");
Image jpg1 = new Image("image1.jpg");
Image jpg2 = new Image("image2.jpg");
Image jpg3 = new Image("image3.jpg");
Video mp41 = new Video("video1.mp4");
Video mp42 = new Video("video2.mp4");
Video mp43 = new Video("video3.mp4");
f1.add(txt1);
f1.add(jpg1);
f1.add(mp41);
f2.add(txt2);
f2.add(jpg2);
f2.add(mp42);
f3.add(txt3);
f3.add(jpg3);
f3.add(mp43);
}
}
文章转载自Hello 帅帅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




