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();
}
}