文章目录 组合模式 1.引出组合模式 1.院系展示需求 2.组合模式基本介绍 3.组合模式原理类图 4.解决的问题 2.组合模式解决院系展示 1.类图 2.代码实现 1.AbsOrganizationComponent.java 总体抽象类用于存储信息和定义方法 2.University.java 第一层,University 可以管理 College 3.College.java 第二层,可以管理Department 4.Department.java 最底层,没有操作节点的方法 5.Client.java 从大到小创建对象,从小到大组合树结构 6.结果 3.JDK的HashMap使用了组合模式 外观模式 1.引出外观模式 1.影院管理项目 2.传统方式解决 3.传统方案的问题分析以及解决方式 2.外观模式 1.基本介绍 2.原理类图 3.思路分析 4.类图 5.目录结构 6.代码实现 1.device 子系统层 1.DVDPlayer.java 2.Popcorn.java 3.Projector.java 4.Screen.java 2.appearance 外观层,统一调用子系统 3.Client.java 客户端,直接调用外观层,不关系子系统的具体调用 4.结果 7.注意事项 3.外观模式在MyBatis框架的应用
组合模式
1.引出组合模式
1.院系展示需求
2.组合模式基本介绍
3.组合模式原理类图
4.解决的问题
2.组合模式解决院系展示
1.类图
2.代码实现
1.AbsOrganizationComponent.java 总体抽象类用于存储信息和定义方法
package com. sun ;
public abstract class AbsOrganizationComponent { private String name; private String des; public AbsOrganizationComponent ( String name, String des) { this . name = name; this . des = des; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public String getDes ( ) { return des; } public void setDes ( String des) { this . des = des; } protected void add ( AbsOrganizationComponent absOrganizationComponent) { throw new UnsupportedOperationException ( ) ; } protected void remove ( AbsOrganizationComponent absOrganizationComponent) { throw new UnsupportedOperationException ( ) ; } protected abstract void print ( ) ; }
2.University.java 第一层,University 可以管理 College
package com. sun ; import java. util. ArrayList ;
import java. util. List ;
public class University extends AbsOrganizationComponent { List < AbsOrganizationComponent > organizationComponents = new ArrayList < AbsOrganizationComponent > ( ) ; public University ( String name, String des) { super ( name, des) ; } @Override protected void add ( AbsOrganizationComponent absOrganizationComponent) { organizationComponents. add ( absOrganizationComponent) ; } @Override protected void remove ( AbsOrganizationComponent absOrganizationComponent) { organizationComponents. remove ( absOrganizationComponent) ; } @Override protected void print ( ) { System . out. println ( "--------" + super . getName ( ) + "--------" ) ; for ( AbsOrganizationComponent organizationComponent : organizationComponents) { organizationComponent. print ( ) ; } } @Override public void setDes ( String des) { super . setDes ( des) ; } @Override public String getDes ( ) { return super . getDes ( ) ; } @Override public void setName ( String name) { super . setName ( name) ; } @Override public String getName ( ) { return super . getName ( ) ; }
}
3.College.java 第二层,可以管理Department
package com. sun ; import java. util. ArrayList ;
import java. util. List ;
public class College extends AbsOrganizationComponent { List < AbsOrganizationComponent > organizationComponents = new ArrayList < AbsOrganizationComponent > ( ) ; public College ( String name, String des) { super ( name, des) ; } @Override protected void add ( AbsOrganizationComponent absOrganizationComponent) { organizationComponents. add ( absOrganizationComponent) ; } @Override protected void remove ( AbsOrganizationComponent absOrganizationComponent) { organizationComponents. remove ( absOrganizationComponent) ; } @Override protected void print ( ) { System . out. println ( "--------" + super . getName ( ) + "--------" ) ; for ( AbsOrganizationComponent organizationComponent : organizationComponents) { System . out. println ( "--------" + organizationComponent. getName ( ) + "--------" ) ; } } @Override public void setDes ( String des) { super . setDes ( des) ; } @Override public String getDes ( ) { return super . getDes ( ) ; } @Override public void setName ( String name) { super . setName ( name) ; } @Override public String getName ( ) { return super . getName ( ) ; }
}
4.Department.java 最底层,没有操作节点的方法
package com. sun ;
public class Department extends AbsOrganizationComponent { public Department ( String name, String des) { super ( name, des) ; } protected void print ( ) { System . out. println ( "叶子节点:" + getName ( ) ) ; } @Override public void setDes ( String des) { super . setDes ( des) ; } @Override public String getDes ( ) { return super . getDes ( ) ; } @Override public void setName ( String name) { super . setName ( name) ; } @Override public String getName ( ) { return super . getName ( ) ; }
}
5.Client.java 从大到小创建对象,从小到大组合树结构
package com. sun ;
public class Client { public static void main ( String [ ] args) { University university = new University ( "清华" , "niubi" ) ; College computer = new College ( "计算机学院" , "培养计算机人才的" ) ; College infoEngineer = new College ( "信息工程学院" , "培养信息工程人才的" ) ; computer. add ( new Department ( "网络安全" , "网安" ) ) ; computer. add ( new Department ( "软件工程" , "软工" ) ) ; computer. add ( new Department ( "计算机科学与技术" , "计科" ) ) ; infoEngineer. add ( new Department ( "通信工程" , "通信工程不好学" ) ) ; infoEngineer. add ( new Department ( "信息工程" , "信息工程不好学" ) ) ; university. add ( computer) ; university. add ( infoEngineer) ; university. print ( ) ; }
}
6.结果
3.JDK的HashMap使用了组合模式
外观模式
1.引出外观模式
1.影院管理项目
2.传统方式解决
3.传统方案的问题分析以及解决方式
2.外观模式
1.基本介绍
2.原理类图
3.思路分析
4.类图
5.目录结构
6.代码实现
1.device 子系统层
1.DVDPlayer.java
package com. sun. device ;
public class DVDPlayer { private static DVDPlayer instance = new DVDPlayer ( ) ; public static DVDPlayer getInstance ( ) { return instance; } public void on ( ) { System . out. println ( "DVD on" ) ; } public void off ( ) { System . out. println ( "DVD off" ) ; } public void play ( ) { System . out. println ( "DVD is playing" ) ; }
}
2.Popcorn.java
package com. sun. device ;
public class Popcorn { private static Popcorn instance = new Popcorn ( ) ; public static Popcorn getInstance ( ) { return instance; } public void on ( ) { System . out. println ( "Popcorn on" ) ; } public void off ( ) { System . out. println ( "Popcorn off" ) ; } public void pop ( ) { System . out. println ( "Popcorn is poping" ) ; }
}
3.Projector.java
package com. sun. device ;
public class Projector { private static Projector instance = new Projector ( ) ; public static Projector getInstance ( ) { return instance; } public void on ( ) { System . out. println ( "Projector on" ) ; } public void off ( ) { System . out. println ( "Projector off" ) ; } public void focus ( ) { System . out. println ( "Projector is focusing" ) ; }
}
4.Screen.java
package com. sun. device ;
public class Screen { private static Screen instance = new Screen ( ) ; public static Screen getInstance ( ) { return instance; } public void up ( ) { System . out. println ( "Screen up" ) ; } public void down ( ) { System . out. println ( "Screen down" ) ; }
}
2.appearance 外观层,统一调用子系统
package com. sun. appearance ; import com. sun. device. DVDPlayer ;
import com. sun. device. Popcorn ;
import com. sun. device. Projector ;
import com. sun. device. Screen ;
public class HomeTheaterFacade { private DVDPlayer dvdPlayer; private Popcorn popcorn; private Projector projector; private Screen screen; public HomeTheaterFacade ( ) { this . dvdPlayer = DVDPlayer . getInstance ( ) ; this . popcorn = Popcorn . getInstance ( ) ; this . projector = Projector . getInstance ( ) ; this . screen = Screen . getInstance ( ) ; } public void ready ( ) { dvdPlayer. on ( ) ; popcorn. on ( ) ; projector. on ( ) ; screen. up ( ) ; } public void play ( ) { dvdPlayer. play ( ) ; } public void shutdown ( ) { dvdPlayer. off ( ) ; popcorn. off ( ) ; projector. off ( ) ; screen. down ( ) ; } }
3.Client.java 客户端,直接调用外观层,不关系子系统的具体调用
package com. sun ; import com. sun. appearance. HomeTheaterFacade ;
public class Client { public static void main ( String [ ] args) { HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade ( ) ; homeTheaterFacade. ready ( ) ; homeTheaterFacade. play ( ) ; homeTheaterFacade. shutdown ( ) ; }
}
4.结果
7.注意事项
3.外观模式在MyBatis框架的应用