摘一句子,加油生活 :) 2019.9.12
我多想拥抱你,可惜时光之里山南水北,可惜你我之间人来人往。
—— 鸢喜
目录
第一部分,适应设计模式
第一章,Iterator模式(迭代器模式):
第二章,Adapter模式(适配器模式)
第二部分,交给子类.
第三章,Template Method 模式(模板方法)
第四章,factory Method模式(工厂方法)
第三部分,生成实例
第五章,Singleton模式(单例模式)
第六章,Prototype模式(原型模式)
第七章,Builder模式,(建造者模式)
第八章,AbstractFactory模式(抽象工程模式)
第四部分,分开考虑:
第九章,Bridge模式(建造者模式)
(将类的功能层次结构与现实层次结构分离)
第十章,Stratrgy模式(策略模式)
第五部分,一致性:
第十一章 Composite模式()
关于UML类图用idea画的,可以看博客:https://www.cnblogs.com/liruilong/p/11506280.html
迭代器模式:提供一种方法顺序访问迭代器中的每个元素。,而不暴露其内部表示。迭代器模式就是将迭代元素的责任交给迭代器,而不是聚合函数,通过迭代器,使得聚合对象的结构更加简单,不需要关注它元素的遍历,符合单一职责原则.
理解:迭代器模式需要一个迭代器接口(定义遍历集合的方法),一个聚合类接口(定义创建迭代器方法),然后有实现了聚合类接口的存放元素的类和一个实现了迭代器接口的类,用于遍历元素.
我们一般在使用集合的时候用的最多,forEach本身就是迭代器实现的,对于集合类来讲,我们通过集合类获取迭代器之后,集合中的元素都存在于迭代器里了.然后调用迭代器里的方法进行迭代..
package com.liruilong.tx_sjms.Iterator;
/**
* @Description : 遍历集合的接口
* @Author: Liruilong
* @Date: 2019/9/11 0:11
*/
public interface Iterator {
boolean hasNext();
Object next();
}
package com.liruilong.tx_sjms.Iterator;
/**
* @Description : 遍历书籍的类
* @Author: Liruilong
* @Date: 2019/9/11 0:21
*/
public class BooShifkIterator implements Iterator {
private BookShelf bookShelf;
private int index;
public BooShifkIterator(BookShelf bookShelf) {
this.bookShelf = bookShelf;
this.index = 0;
}
@Override
public boolean hasNext() {
if (index < bookShelf.getlength()){
return true;
} else {
return false;
}
}
@Override
public Object next() {
Book book = bookShelf.getBooksAt(index);
index++;
return book;
}
}
package com.liruilong.tx_sjms.Iterator;
/**
* @Description : 表示集合的接口
* @Author: Liruilong
* @Date: 2019/9/11 0:10
*/
public interface Aggregate {
Iterator iterator();
}
package com.liruilong.tx_sjms.Iterator;
/**
* @Description :表示书架的类
* @Author: Liruilong
* @Date: 2019/9/11 0:14
*/
public class BookShelf implements Aggregate {
private Book[] books;
private int last = 0;
// 初始化书架
public BookShelf(int maxsize) {
this.books = new Book[maxsize];
}
// 在书架的最后放入书
public void appendBook(Book book){
this.books[last] = book;
last++;
}
// 获取指定书籍
public Book getBooksAt(int index) {
return books[index];
}
// 获取书架容量
public int getlength() {
return last;
}
@Override
public Iterator iterator() {
return new BooShifkIterator(this);
}
}
适配器模式:在实际情况和需求之间,填补两者之间的差异的设计模式,即将一个类的接口转换为客户期望的另一个接口,
将交流的220V的电由ac适配器转换为直流电100v,ac适配器就充当适配角色.
适配器有两种:类适配器模式(使用继承的适配器)和对象适配器模式(使用委托的适配器).
类适配器模式:
使用继承,即适配器继承于被适配者,实现了需求对象接口,可以理解为需求对象接口将需求告诉适配器(实了现接口方法),适配器对被适配者进行加工,然后满足客户需求.
package com.liruilong.tx_sjms.Adapter;
/**
* @Description : 需求的接口
* @Author: Liruilong
* @Date: 2019/9/11 21:39
*/
public interface Print {
void printWeak();
void printStrong();
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/11 21:43
*/
public class Main {
public static void main(String[] args) {
Print print = new PrintBanner("Hello");
print.printWeak();
print.printStrong();
}
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description : 实际情况
* @Author: Liruilong
* @Date: 2019/9/11 21:28
*/
public class Banner {
private String string;
public Banner(String string) {
this.string = string;
}
public void showWithParen(){
System.out.println("("+string+")");
}
public void showWithAster(){
System.out.println("*"+string+"*");
}
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/11 21:41
*/
public class PrintBanner extends Banner implements Print {
public PrintBanner(String string) {
super(string);
}
@Override
public void printWeak() {
showWithParen();
}
@Override
public void printStrong() {
showWithAster();
}
}
对象适配器模式:
package com.liruilong.tx_sjms.Adapter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 0:42
*/
public abstract class Prints {
public abstract void printWeak();
public abstract void printStrong();
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/11 21:43
*/
public class Main {
public static void main(String[] args) {
Print print = new PrintBanner("Hello");
print.printWeak();
print.printStrong();
}
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description : 实际情况
* @Author: Liruilong
* @Date: 2019/9/11 21:28
*/
public class Banner {
private String string;
public Banner(String string) {
this.string = string;
}
public void showWithParen(){
System.out.println("("+string+")");
}
public void showWithAster(){
System.out.println("*"+string+"*");
}
}
package com.liruilong.tx_sjms.Adapter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/11 21:41
*/
public class PrintBanners extends Prints {
private Banner banner;
public PrintBanners(String string) {
this.banner = new Banner(string);
}
@Override
public void printWeak() {
banner.showWithParen();
}
@Override
public void printStrong() {
banner.showWithAster();
}
}
Main:
模板方法:即在一个方法中定义算法骨架,而将一些步骤延迟到子类,或者讲在父类中定义处理流程框架,在子类中实现具体的处理,
模板方法可以使得子类在不改变算法算法结构的情况下,重新定义算法中的某些步骤.Thread类中,start方法就是一种模板方法模式的应用,对于不同的实现,有不同的run()方法.
Template Method模式中的角色:
package com.liruilong.tx_sjms.TemplaterMethod;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 11:26
*/
public abstract class AbstractDisplay {
public abstract void open();
public abstract void print();
public abstract void close();
public final void display(){
open();
for (int i = 0; i < 5; i++){
print();
}
close();
}
}
package com.liruilong.tx_sjms.TemplaterMethod;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 11:29
*/
public class CharDisplay extends AbstractDisplay {
private char ch;
public CharDisplay(char ch) {
this.ch = ch;
}
@Override
public void open() {
System.out.print("<<");
}
@Override
public void print() {
System.out.print(ch);
}
@Override
public void close() {
System.out.println(">>");
}
}
package com.liruilong.tx_sjms.TemplaterMethod;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 12:14
*/
public class StringDisplay extends AbstractDisplay {
private String string;
private int width;
public StringDisplay(String string) {
this.string = string;
// 保存字符串的字节长度
this.width = string.getBytes().length;
}
@Override
public void open() {
pringLine();
}
@Override
public void print() {
System.out.println("|"+string+"|");
}
@Override
public void close() {
pringLine();
}
private void pringLine(){
System.out.println("+");
for (int i = 0; i < width; i++){
System.out.print("-");
}
System.out.println("+");
}
}
package com.liruilong.tx_sjms.TemplaterMethod;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 12:22
*/
public class Main {
public static void main(String[] args) {
AbstractDisplay display = new CharDisplay('H');
AbstractDisplay display1 = new StringDisplay("你好,世界");
display.display();
display1.display();
}
}
类的层次和抽象类:
Factory Method模式:用Template Method模式来构建生成实例的工厂,就是factory Method模式.只要是FactoryMethod模式,在生成实例时就一定会使用到TemplateMethod模式.
FactoryMethod模式中的登场角色
package com.liruilong.tx_sjms.FactoryMethod.framework;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 19:35
*/
public abstract class Product {
public abstract void use();
}
package com.liruilong.tx_sjms.FactoryMethod.framework;
import com.liruilong.concurrent.Producer_Consuner.Producer;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 19:35
*/
public abstract class Factory {
public final Product create(String owner){
Product p = createProducer(owner);
registerProduct(p);
return p;
}
protected abstract Product createProducer(String owner);
protected abstract void registerProduct(Product product);
}
package com.liruilong.tx_sjms.FactoryMethod.idcard;
import com.liruilong.concurrent.Producer_Consuner.Producer;
import com.liruilong.tx_sjms.FactoryMethod.framework.Product;
/**
* @Description : 一个产品
* @Author: Liruilong
* @Date: 2019/9/12 19:35
*/
public class IDCard extends Product {
private String owner;
IDCard(String owner){
System.out.println("制作"+owner+"的ID卡");
this.owner = owner;
}
@Override
public void use() {
System.out.println("使用"+owner+"的ID卡。");
}
public String getOwner(){
return owner;
}
}
package com.liruilong.tx_sjms.FactoryMethod.idcard;
import com.liruilong.concurrent.Producer_Consuner.Producer;
import com.liruilong.tx_sjms.FactoryMethod.framework.Factory;
import com.liruilong.tx_sjms.FactoryMethod.framework.Product;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 19:36
*/
public class IDCardFactory extends Factory {
private List owners = new ArrayList();
// 生产产品
@Override
protected Product createProducer(String owner) {
return new IDCard(owner);
}
// 注册产品
@Override
protected void registerProduct(Product producer) {
owners.add(((IDCard)producer).getOwner());
}
public List getOwners(){
return owners;
}
}
Main
package com.liruilong.tx_sjms.FactoryMethod;
import com.liruilong.tx_sjms.FactoryMethod.framework.Factory;
import com.liruilong.tx_sjms.FactoryMethod.framework.Product;
import com.liruilong.tx_sjms.FactoryMethod.idcard.IDCardFactory;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/12 20:45
*/
public class Main {
public static void main(String[] args) {
Factory factory = new IDCardFactory();
Product product = factory.create("1");
Product product1 = factory.create("2");
Product product2 = factory.create("3");
Product product3 = factory.create("4");
product.use();
product1.use();
product2.use();
product3.use();
}
}
Singleton模式:Singleton角色中返回一个实例static方法,该方法总会返回一个实例.
Singleton模式中的角色
package com.liruilong.singleton;
/**
* @Author: Liruilong
* @Date: 2019/7/20 17:55
*/
// final 不允许被继承
public final class Singleton {
// 实例变量
private byte[] bate = new byte[1024];
// 私有的构造函数,即不允许外部 new
private Singleton(){ }
/**
* @Author Liruilong
* @Description 双重校验锁单例(Double-Check)+Volatile
* 对懒汉-同步方法的改进,当有两个线程发现singleton为null时,只有一个线程可以进入到同步代码块里。
* 即满足了懒加载,又保证了线程的唯一性
* 不加volition的缺点,有时候可能会报NPE,(JVM运行指令重排序)
* 有可能实例对象的变量未完成实例化其他线程去获取到singleton变量。
* 未完成初始化的实例调用其方法会抛出空指针异常。
* @Date 18:20 2019/7/26
* @Param
* @return
**/
private static volatile Singleton singleton2 = null;
public static Singleton getInstance4() {
if (singleton2 == null){
synchronized (Singleton.class){
if (singleton2 ==null){
singleton2 = new Singleton();
}
}
}
return singleton2;
}
通过复制生成实例,在实际的开发过程中,有时候也会有在不指定类名的前提下生成实例的需求,
理解:原型模式,即原型对象实现了Cloneable接口,具有复制能力.在具体的原型实现类中获取该原型使用复制实例的方法获取,在使用者中直接调用具体原型中获取实例的方法.
原型模式中的角色:
package com.liruilong.tx_sjms.Prototype.framework;
/**
* @Description : 复制功能的接口
* @Author: Liruilong
* @Date: 2019/9/13 14:53
*/
public interface Products extends Cloneable{
void use(String s);
Products createClone();
}
package com.liruilong.tx_sjms.Prototype;
import com.liruilong.tx_sjms.Prototype.framework.Products;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/13 14:52
*/
public class MessageBox implements Products {
private char decochar;
public MessageBox(char decochar) {
this.decochar = decochar;
}
@Override
public void use(String s) {
int length = s.getBytes().length;
for (int i = 0; i < length + 4; i++){
System.out.print(decochar);
}
System.out.println("");
System.out.println(decochar + " " + s + " " + decochar);
for (int i = 0; i
package com.liruilong.tx_sjms.Prototype;
import com.liruilong.tx_sjms.Prototype.framework.Products;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/13 14:53
*/
public class UnderlinePen implements Products {
private char ulchar;
public UnderlinePen(char ulchar) {
this.ulchar = ulchar;
}
@Override
public void use(String s) {
int length = s.getBytes().length;
System.out.println("\"" + s + "\"" );
System.out.print(" ");
for (int i = 0; i < length; i++){
System.out.print(ulchar);
}
System.out.println(" ");
}
@Override
public Products createClone(){
Products p = null;
try {
p = (Products) clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return p;
}
}
package com.liruilong.tx_sjms.Prototype.framework;
import java.util.HashMap;
/**
* @Description : 使用Product接口来复制实例
* @Author: Liruilong
* @Date: 2019/9/13 14:54
*/
public class Manager {
private HashMap showcase = new HashMap();
// 对产品进行注册,面向接口编程
public void register(String name, Products products){
showcase.put(name, products);
}
// 对产品进行复制
public Products create(String protoname){
Products p = (Products) showcase.get(protoname);
return p.createClone();
}
}
package com.liruilong.tx_sjms.Prototype;
import com.liruilong.tx_sjms.Prototype.framework.Manager;
import com.liruilong.tx_sjms.Prototype.framework.Products;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/13 14:52
*/
public class Main {
public static void main(String[] args) {
// 准备
Manager manager = new Manager();
UnderlinePen upen = new UnderlinePen('~');
MessageBox mbox = new MessageBox('*');
MessageBox sbox = new MessageBox('/');
manager.register("strong message", upen);
manager.register("warning box", mbox);
manager.register("slash box", sbox);
// 生成
Products products = manager.create("strong message");
products.use("Hello, world");
Products products1 = manager.create("warning box");
products1.use("Hello, world");
Products products2 = manager.create("slash box");
products2.use("Hello, word");
}
}
建造者模式,封装一个复杂对象的构建过程.
Builder模式中的角色:
package com.liruilong.tx_sjms.Builder;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/15 10:15
*/
public abstract class Builder {
public abstract void makeTitle(String string);
public abstract void maekString(String string);
public abstract void maekItems(String[] items);
public abstract void close();
}
package com.liruilong.tx_sjms.Builder;
import java.io.FileOutputStream;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/15 10:38
*/
public class HTMLBulider extends Builder {
private String filename;
private PrintWriter writer;
@Override
public void makeTitle(String title) {
filename = title + ".html";
try {
writer = new PrintWriter(new FileOutputStream(filename));
}catch (IOException e){
e.printStackTrace();
}
writer.println("" + title + " " );
writer.println("" + title +"
");
}
@Override
public void maekString(String string) {
writer.println("" + string + "
");
}
@Override
public void maekItems(String[] items) {
writer.println("");
for (int i = 0; i < items.length; i++){
writer.println("- " + items[i] + "
");
}
writer.println("/ul");
}
@Override
public void close() {
writer.println("");
writer.close();
}
public String getResoult(){
return filename;
}
}
package com.liruilong.tx_sjms.Builder;
import java.io.FileOutputStream;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/15 10:38
*/
public class HTMLBulider extends Builder {
private String filename;
private PrintWriter writer;
@Override
public void makeTitle(String title) {
filename = title + ".html";
try {
writer = new PrintWriter(new FileOutputStream(filename));
}catch (IOException e){
e.printStackTrace();
}
writer.println("" + title + " " );
writer.println("" + title +"
");
}
@Override
public void maekString(String string) {
writer.println("" + string + "
");
}
@Override
public void maekItems(String[] items) {
writer.println("");
for (int i = 0; i < items.length; i++){
writer.println("- " + items[i] + "
");
}
writer.println("/ul");
}
@Override
public void close() {
writer.println("");
writer.close();
}
public String getResoult(){
return filename;
}
}
package com.liruilong.tx_sjms.Builder;
import java.io.BufferedReader;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/15 10:18
*/
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct(){
builder.makeTitle("Greeting");
builder.maekString("从早到下午");
builder.maekItems(new String[]{"早上好", "下午好"});
builder.maekString("晚上");
builder.maekItems(new String[]{"晚上好", "晚安", "在见"});
builder.close();
}
}
package com.liruilong.tx_sjms.Builder;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/15 10:59
*/
public class Main {
public static void main(String[] args) {
TextBuilder textBuilder = new TextBuilder();
Director director = new Director(textBuilder);
director.construct();
System.out.println(textBuilder.getResult());
HTMLBulider htmlBulider = new HTMLBulider();
Director director1 = new Director(htmlBulider);
director1.construct();
System.out.println(htmlBulider.getResoult());
}
public static void usage(){
System.out.println("Usage:java Main plain 编写纯文本");
System.out.println("Usage: java Main html 编写html");
}
}
AbstractFactory模式,将关联零件组装成产品,即将抽象零件组装为抽象产品.即并不关心零件的具体实现,而只是关心接口API,即仅使用接口将零件组装.
类的层次结构的两个作用,
将类的功能层次和类的实现层次结构分离为两个独立的类层次结构,有利于独立的对他们进行扩展.
Bridge模式中的登场角色:
package com.liruilong.tx_sjms.Bridge;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 2:05
*/
public class Display {
private DisplayImpl impl;
public Display(DisplayImpl impl) {
this.impl = impl;
}
public void open(){
impl.rawOpen();
}
public void print(){
impl.rawPrint();
}
public void close(){
impl.rawClose();
}
public final void display(){
open();
print();
close();
}
}
package com.liruilong.tx_sjms.Bridge;
/**
* @Description : 类的功能层次结构
* @Author: Liruilong
* @Date: 2019/9/17 2:06
*/
public class CountDisplay extends Display{
public CountDisplay(DisplayImpl impl) {
super(impl);
}
public void multiDisplay(int times){
open();
for(int i = 0; i < times; i++ ){
print();
}
close();
}
}
package com.liruilong.tx_sjms.Bridge;
/**
* @Description :类的实现层次结构
* @Author: Liruilong
* @Date: 2019/9/17 2:06
*/
public abstract class DisplayImpl {
public abstract void rawOpen();
public abstract void rawPrint();
public abstract void rawClose();
}
package com.liruilong.tx_sjms.Bridge;
/**
* @Description :类的实现层次结构
* @Author: Liruilong
* @Date: 2019/9/17 2:07
*/
public class StringDisplayImpl extends DisplayImpl {
private String string;
private int width;
public StringDisplayImpl(String string) {
this.string = string;
this.width = string.getBytes().length;
}
@Override
public void rawOpen() {
printLine();
}
@Override
public void rawPrint() {
System.out.println("|" + string + "|");
}
@Override
public void rawClose() {
printLine();
}
private void printLine(){
System.out.print("+");
for (int i = 0; i < width; i++){
System.out.print("-");
}
System.out.println("+");
}
}
package com.liruilong.tx_sjms.Bridge;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 9:33
*/
public class Main {
public static void main(String[] args) {
Display display = new Display(new StringDisplayImpl("Hello, China."));
Display display1 = new CountDisplay(new StringDisplayImpl("Hello. word"));
CountDisplay countDisplay = new CountDisplay(new StringDisplayImpl("Hello, Universe."));
display.display();
display1.display();
countDisplay.multiDisplay(5);
}
}
继承是强关联,委托是弱关联,
策略模式:整体的替换算法,定义了算法簇,每个方法都对应一个具体的算法,策略模式符合开闭原则,即用户可以在不修改原来的基础上选择算法和行为.定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
Strategy模式中具体的角色:
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
package com.liruilong.tx_sjms.Stratrgy;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 17:02
*/
public class Stratrgy1 implements Runnable {
@Override
public void run() {
System.out.println("策略二");
}
}
package com.liruilong.tx_sjms.Stratrgy;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 17:09
*/
public class Stratrgy2 implements Runnable {
@Override
public void run() {
System.out.println("策略二");
}
}
package com.liruilong.tx_sjms.Stratrgy;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 17:10
*/
public class Stratrgy3 implements Runnable {
@Override
public void run() {
System.out.println("策略三");
}
}
package com.liruilong.tx_sjms.Stratrgy;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/17 17:01
*/
public class Main {
public static void main(String[] args) {
Thread thread = null;
thread = new Thread(new Stratrgy1(),"策略一");
thread = new Thread(new Stratrgy2(),"策略二");
thread = new Thread(new Stratrgy3(),"策略三");
thread.start();
}
}
组合模式:将对象组合为树形结构以表示部分于整体的层次关系。能够使内容和内容具有一致性。创建出递归结构的模式。组合模式或复合模式。
在使用组合模式中需要注意叶子对象和组合对象实现相同的接口,即也是组合模式能够将叶子节点和对象节点进行一致性的处理
Composite模式中的角色:
package com.liruilong.tx_sjms.Composite;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/23 0:26
*/
public class File extends Entry{
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
return size;
}
@Override
protected void printList(String string) {
System.out.println(string + "/" + this);
}
}
package com.liruilong.tx_sjms.Composite;
import java.util.ArrayList;
/**
* @Description : 文件夹类
* @Author: Liruilong
* @Date: 2019/9/23 0:26
*/
public class Directory extends Entry {
private String name;
private ArrayList directory = new ArrayList();
public Directory(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
int size = 0;
size = directory.stream().map(( entry) -> entry.getSize()).reduce(size,Integer::sum);
return size;
}
@Override
public Entry add(Entry entry ) throws FileTreatementException {
directory.add(entry);
return this;
}
/**
* @Author Liruilong
* @Description 显示条目
* @Date 0:47 2019/9/23
* @Param [string]
* @return void
**/
@Override
protected void printList(String string) {
System.out.println(string + "/" + this);
directory.stream().forEach((entry -> entry.printList(string + "/" + name)));
}
}
package com.liruilong.tx_sjms.Composite;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/23 0:26
*/
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public Entry add(Entry entry) throws FileTreatementException{
throw new FileTreatementException();
}
public void printList(){
printList("");
}
protected abstract void printList(String string);
public String toString(){
return getName() + "{" + getSize() + "}";
}
}
package com.liruilong.tx_sjms.Composite;
/**
* @Description :
* @Author: Liruilong
* @Date: 2019/9/23 0:27
*/
public class Mian {
public static void main(String[] args) {
System.out.println("Making root entries...");
Directory rootdor = new Directory("root");
Directory bindir = new Directory("bin");
Directory tmpdir = new Directory("tmp");
Directory usrdir = new Directory("usr");
rootdor.add(bindir);
rootdor.add(tmpdir);
rootdor.add(tmpdir);
bindir.add(new File("vi", 10000));
bindir.add(new File("latex", 20000));
rootdor.printList();
System.out.println("");
System.out.println("Making user entries...");
Directory yuki = new Directory("yuki");
Directory hanako = new Directory("hanako");
Directory tomura = new Directory("tomura");
usrdir.add(yuki);
usrdir.add(tomura);
yuki.add(new File("diary.html",100));
yuki.add(new File("Compossite.java",200));
hanako.add(new File("memo.tex", 300));
tomura.add(new File("game.doc", 400));
tomura.add(new File("junk.mail", 500));
rootdor.printList();
}
}