设计模式--装饰者模式和组合模式

1、装饰者模式

package com.yqj.pattern.bridge;

interface Brand{
    void open();
    void call();
    void close();
}

class Vivo implements Brand{

    @Override
    public void open() {
        System.out.println("Vivo开机");
    }

    @Override
    public void call() {
        System.out.println("Vivo打电话");
    }

    @Override
    public void close() {
        System.out.println("Vivo关机");
    }
}

class XiaoMi implements Brand{

    @Override
    public void open() {
        System.out.println("小米开机");
    }

    @Override
    public void call() {
        System.out.println("小米打电话");
    }

    @Override
    public void close() {
        System.out.println("小米关机");
    }
}

abstract class Phone{
    Brand brand;

    public Phone(Brand brand) {
        this.brand = brand;
    }

    protected void open(){
        brand.open();
    }

    protected void call(){
        brand.call();
    }

    protected void close(){
        brand.close();
    }
}

class FoldedPhone extends Phone{

    public FoldedPhone(Brand brand) {
        super(brand);
    }

    public void open(){
        super.open();
        System.out.println("折叠样式手机");
    }

    public void call(){
        super.call();
        System.out.println("折叠样式手机");
    }

    public void close(){
        super.close();
        System.out.println("折叠样式手机");
    }
}

class UpRightPhone extends Phone{

    public UpRightPhone(Brand brand) {
        super(brand);
    }

    public void open(){
        super.open();
        System.out.println("直立样式手机");
    }

    public void call(){
        super.call();
        System.out.println("直立样式手机");
    }

    public void close(){
        super.close();
        System.out.println("直立样式手机");
    }
}

public class Client{
    public static void main(String[] args) {
        Phone phone = new FoldedPhone(new XiaoMi());
        phone.open();
        phone.call();
        phone.close();
        System.out.println("+++++++++++++");
        UpRightPhone phone2 = new UpRightPhone(new Vivo());
        phone2.open();
        phone2.call();
        phone2.close();
    }
}

2、组合模式

package com.yqj.pattern.composite;

import java.util.ArrayList;
import java.util.List;

abstract class OrganizationComponent {
    private String name;

    public OrganizationComponent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void add(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public void remove(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public abstract void print();
}

class University extends OrganizationComponent {
    private List<OrganizationComponent> colleges = new ArrayList<>();

    public University(String name) {
        super(name);
    }


    @Override
    public void add(OrganizationComponent organizationComponent) {
        colleges.add(organizationComponent);
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        colleges.remove(organizationComponent);
    }

    @Override
    public void print() {
        System.out.println("++++++++++"+this.getName()+"++++++++++");
        for (OrganizationComponent college : colleges) {
            college.print();
        }
    }
}

class College extends OrganizationComponent {
    private List<OrganizationComponent> departments = new ArrayList<>();

    public College(String name) {
        super(name);
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        departments.add(organizationComponent);
    }

    @Override
    public void remove(OrganizationComponent organizationComponent) {
        departments.remove(organizationComponent);
    }

    @Override
    public void print() {
        System.out.println("++++++++++"+this.getName()+"++++++++++");
        for (OrganizationComponent department : departments) {
            department.print();
        }
    }
}

class Department extends OrganizationComponent{

    public Department(String name) {
        super(name);
    }

    @Override
    public void print() {
        System.out.println(this.getName());
    }
}

public class Client {
    public static void main(String[] args) {
        //从大到小创建对象
        //创建学校
        University university = new University("首师大");
        //创建学院
        College computerCollege = new College("计算机学院");
        College chemcialCollege = new College("化学学院");
        //创建系
        Department department1 = new Department("软件工程");
        Department department2 = new Department("计算机科学与技术");
        Department department3 = new Department("通讯工程");
        Department department4 = new Department("化学师范");
        Department department5 = new Department("应用化学");
        //将系加入各学院
        computerCollege.add(department1);
        computerCollege.add(department2);
        computerCollege.add(department3);
        chemcialCollege.add(department4);
        chemcialCollege.add(department5);
        //将学院加入学校
        university.add(computerCollege);
        university.add(chemcialCollege);
        //移除一个系
        chemcialCollege.remove(department4);
        //打印
        university.print();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值