java设计模式进阶_factory-method

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Blacksmith.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//



/**
 * The interface containing method for producing objects.
 *
 */
public interface Blacksmith {
    public Weapon manufactureWeapon(WeaponType type);
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ElfBlacksmith.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//



/**
 * Concrete subclass for creating new objects.
 *
 */
public class ElfBlacksmith implements Blacksmith {
    public Weapon manufactureWeapon(WeaponType type) {
        return new ElfWeapon(type);
    }
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcBlacksmith.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//



/**
 * Concrete subclass for creating new objects.
 *
 */
public class OrcBlacksmith implements Blacksmith {
    public Weapon manufactureWeapon(WeaponType type) {
        return new OrcWeapon(type);
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcWeapon.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//




public class OrcWeapon implements Weapon {

    private WeaponType weaponType;

    public OrcWeapon(WeaponType type) {
        weaponType = type;
    }

    public String toString() {
        return "Orcish " + weaponType;
    }
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ElfWeapon.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//




public class ElfWeapon implements Weapon {

    private WeaponType weaponType;

    public ElfWeapon(WeaponType weaponType) {
        this.weaponType = weaponType;
    }

    public String toString() {
        return "Elven " + weaponType;
    }
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Weapon.java
//  @ Date : 2016/9/6
//  @ Author : 
//
//




public interface Weapon {
}

public enum WeaponType {

    SHORT_SWORD("short sword"),//短刀
    SPEAR("spear"),//矛
    AXE("axe"),
    UNDEFINED("");

    private String title;

    WeaponType(String title)
    {
        this.title = title;
    }

    @Override
    public String toString() {
        return title;
    }
}


/**
 * In Factory Method we have an interface (Blacksmith) with a method for
 * creating objects (manufactureWeapon).The concrete subclasses (OrcBlacksmith,
 * ElfBlacksmith) then override the method to produce objects of their liking.
 *
 */
public class App {

    public static void main(String[] args) {
        Blacksmith blacksmith;
        Weapon weapon;

        blacksmith = new OrcBlacksmith();
        weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
        System.out.println(weapon);
        weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
        System.out.println(weapon);

        blacksmith = new ElfBlacksmith();
        weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
        System.out.println(weapon);
        weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
        System.out.println(weapon);
    }
}

/*
Orcish spear
Orcish axe
Elven short sword
Elven spear*/














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值