Prototype模式 通过复制生成实例

框架:

1、接口

package com.polo.proto.fremework;

public interface Product extends Cloneable{

    public abstract void use(String s);
    public abstract Product createClone();
}

2、使用者

package com.polo.proto.fremework;

import java.util.HashMap;

/**
 * @program: data-structure
 * @description: 框架
 * @author: Miller.FAN
 * @create: 2020-02-08 14:28
 **/
public class Manager {
    private HashMap showCase = new HashMap();

    public void register(String name, Product proto) {
        showCase.put(name,proto);
    }

    public Product create(String protoName) {
        Product p = (Product) showCase.get(protoName);
        return p.createClone();
    }
}

3、避免重复代码出现

package com.polo.proto.fremework;

/**
 * @program: data-structure
 * @description: 实现接口的公共部分
 * @author: Miller.FAN
 * @create: 2020-02-08 19:16
 **/
public class AbstractPhoto implements Product{
    @Override
    public void use(String s) {

    }

    @Override
    public Product createClone() {
        Product p = null;
        try {
            p = (Product)clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return p;
    }

}

使用

1、子类一

package com.polo.proto.son;

import com.polo.proto.fremework.AbstractPhoto;
import com.polo.proto.fremework.Product;

/**
 * @program: data-structure
 * @description: son of product
 * @author: Miller.FAN
 * @create: 2020-02-08 14:35
 **/
public class MessageBox extends AbstractPhoto implements Product {

    private String decochar;

    public MessageBox(String decochar) {
        this.decochar = decochar;
    }


    @Override
    public void use(String s) {

        int length = s.getBytes().length;
        String printChar = "";
        for(int i = 0; i < length + 4; i++) {
            printChar = printChar + decochar;
        }
        System.out.println(printChar);
        System.out.println("");
        System.out.println(decochar + " " + s + " " + decochar);
        System.out.println("");
        System.out.println(printChar);
        System.out.println("");

    }

}

2、子类二

package com.polo.proto.son;

import com.polo.proto.fremework.AbstractPhoto;
import com.polo.proto.fremework.Product;

/**
 * @program: data-structure
 * @description: 下划线
 * @author: Miller.FAN
 * @create: 2020-02-08 15:21
 **/
public class UnderLinePen extends AbstractPhoto implements Product {

    private String underLineString;

    public UnderLinePen(String underLineString) {
        this.underLineString = underLineString;
    }

    @Override
    public void use(String s) {
        System.out.println(" " + s + " ");
        int length = s.getBytes().length;
        String printChar = "";
        for(int i = 0; i < length + 4; i++) {
            printChar = printChar + underLineString;
        }
        System.out.println(printChar);
        System.out.println("");
    }

}

3、测试

package com.polo.proto.test;

import com.polo.proto.fremework.Manager;
import com.polo.proto.fremework.Product;
import com.polo.proto.son.MessageBox;
import com.polo.proto.son.UnderLinePen;

/**
 * @program: data-structure
 * @description: main
 * @author: Miller.FAN
 * @create: 2020-02-08 15:25
 **/
public class protoTest {

    public static void main(String[] args) {

        //准备
        Manager manager = new Manager();
        MessageBox messageBox = new MessageBox("*");
        UnderLinePen underLinePen = new UnderLinePen("_");

        manager.register("waring box", messageBox);
        manager.register("strong message" , underLinePen);

        //生成
        Product p1 = manager.create("waring box");
        Product p2 = manager.create("strong message");
        p1.use("miller");
        p2.use("baby");
    }
}

结果:

**********

* miller *

**********

 baby 
________

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值