美文网首页
设计模式用例 (五)

设计模式用例 (五)

作者: 月巴月巴 | 来源:发表于2019-01-04 17:30 被阅读0次
    1. 模板模式
    interface Job {
        void execute();
    }
    
    abstract class Work implements Job {
        public void doWork() {
            Date begin = new Date();
            System.out.println("Begin working at" + begin);
            execute();
            Date end = new Date();
            System.out.println("End working at" + new Date()  + "\nTotal time is + " + (end.getTime() - begin.getTime()) + "ms");
    
        }
    }
    
    class HardWork extends Work {
        @Override
        public void execute() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // do nothing
            }
        }
    }
    
    public class TemplatePattern {
        public static void main(String[] args) {
            HardWork hw = new HardWork();
            hw.doWork();
        }
    }
    // output
    //Begin working atFri Jan 04 17:29:05 GMT+08:00 2019
    //End working atFri Jan 04 17:29:06 GMT+08:00 2019
    // Total time is + 1045ms
    
    1. Composite模式
    interface Item {
        int getPrice();
        String getName();
    }
    
    class Bundle implements Item {
        private List<Item> items = new ArrayList<>();
        private String name;
    
        public Bundle(String name) {
            this.name = name;
        }
    
        public void add(Item item) {
            items.add(item);
        }
    
        public void remove(Item item) {
            items.remove(item);
        }
    
        public int getPrice() {
            int price = 0;
            for (Item item : items) {
                price += item.getPrice();
            }
            return price;
        }
    
        public String getName() {
            return name;
        }
    
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(name);
            sb.append(": [");
            for (Item item : items) {
                sb.append(item.toString());
                sb.append(", ");
            }
            sb.append("]");
            return sb.toString();
        }
    }
    
    class LineItem implements Item {
        private int price = 0;
        private String name;
    
        public LineItem(int price, String name) {
            this.price = price;
            this.name = name;
        }
    
        public int getPrice() {
            return price;
        }
    
        public String getName() {
            return name;
        }
    
        public String toString() {
            return name;
        }
    }
    
    public class CompositePattern {
       // 购物车中有 两个单独买的物品,以及捆绑销售的1,2,3套餐。其中套餐2包含了套餐4
        public static void main(String[] args) {
            Bundle shoppingCart = new Bundle("Shopping Cart");
            shoppingCart.add(new LineItem(10,"item1"));
            shoppingCart.add(new LineItem(5,"item2"));
    
            Bundle bundle1 = new Bundle("Bundle1");
            bundle1.add(new LineItem(3, "I1B1"));
            bundle1.add(new LineItem(4, "I2B1"));
    
            Bundle bundle2 = new Bundle("Bundle2");
            bundle2.add(new LineItem(5, "I1B2"));
    
            Bundle bundle3 = new Bundle("EmptyBundle3");
    
            Bundle bundle4 = new Bundle("Bundle4");
            bundle4.add(new LineItem(7, "I1B4"));
            bundle2.add(bundle4);
    
            shoppingCart.add(bundle1);
            shoppingCart.add(bundle2);
            shoppingCart.add(bundle3);
    
            System.out.println("Total Price : " + shoppingCart.getPrice());
            System.out.println(shoppingCart);
        }
    }
    
    // output
    //Total Price : 34
    //Shopping Cart: [item1, item2, Bundle1: [I1B1, I2B1, ], Bundle2: [I1B2, Bundle4: [I1B4, ], ], EmptyBundle3: [], ]
    

    3.原型模式

    class Soldier implements Cloneable {
        private String job;
        private Weapon weapon;
    
        public Soldier(String job, Weapon weapon) throws InterruptedException {
            Thread.sleep(1);
            this.job = job;
            this.weapon = weapon;
        }
    
        public String toString() {
            return "Job: " + job + "; Weapon: " + weapon;
        }
    
        @Override
        protected Soldier clone() throws CloneNotSupportedException {
            Soldier soldier = (Soldier) super.clone();
            soldier.weapon = soldier.weapon.clone();
            return soldier;
        }
    
        public void setWeapon(Weapon weapon) {
            this.weapon = weapon;
        }
    }
    
    class Weapon implements Cloneable {
        private String weaponName;
    
        public Weapon(String weaponName) {
            this.weaponName = weaponName;
        }
    
        @Override
        protected Weapon clone() throws CloneNotSupportedException {
            return (Weapon) super.clone();
        }
    
        public String toString() {
            return weaponName;
        }
    }
    public class PrototypePattern {
        public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
            Soldier archer = new Soldier("Archer", new Weapon("Bow"));
            Soldier mage = new Soldier("Mage", new Weapon("Staff"));
            System.out.println("************Test Clone**************");
            Soldier clonedArcher = archer.clone();
            System.out.println(clonedArcher);
            Soldier clonedMage = mage.clone();
            System.out.println(clonedMage);
            System.out.println("************Test DEEP COPY*************");
            clonedMage.setWeapon(new Weapon("Dagger"));
            System.out.println(clonedMage);
            System.out.println(mage);
    
            System.out.println("************Test SPEED*************");
            Date cloneBegin = new Date();
            for (int i = 0; i < 1000; i++) {
                archer.clone();
                mage.clone();
            }
            System.out.println("Clone 1000 soldier takes " + (new Date().getTime() - cloneBegin.getTime()) + "ms");
    
            Date newBegin = new Date();
            for (int i = 0; i < 1000; i++) {
                new Soldier("Archer", new Weapon("Bow"));
                new Soldier("Mage", new Weapon("Staff"));
            }
            System.out.println("New 1000 soldier takes " + (new Date().getTime() - newBegin.getTime()) + "ms");
        }
    }
    // output
    //************Test Clone**************
    //Job: Archer; Weapon: Bow
    //Job: Mage; Weapon: Staff
    //************Test DEEP COPY*************
    //Job: Mage; Weapon: Dagger
    //Job: Mage; Weapon: Staff
    //************Test SPEED*************
    //Clone 1000 soldier takes 1ms
    //New 1000 soldier takes 2093ms
    

    相关文章

      网友评论

          本文标题:设计模式用例 (五)

          本文链接:https://www.haomeiwen.com/subject/vqykrqtx.html