单例模式
using System;
namespace DesignIntegration{
public class TimeManager{
private static TimeManager _instance;
private static readonly Object locker = new Object();
private TimeManager() { }
public static TimeManager Instance{
get{
//当地一个线程运行到这里时
//此时会对locker对象“枷锁”,
//当第二个线程运行该方法时,首先检测到locker对象为“加锁”状态,
//该线程就会挂起等待第一个线程解锁
if (_instance == null){
lock (locker){
if (locker == null)
_instance = new TimeManager();
}
}
return _instance;
}
}
public void Greet() {
DateTime dateTime = DateTime.Now;
Console.WriteLine($"我是地球Online的时间管理者,现在时间是{dateTime}");
}
}
}
using System;
namespace DesignIntegration{
//工厂模式
public abstract class IItem{
//武器名称
protected string Name { get; set; }
//道具编号
protected int ID { get; set; }
//道具描述
protected string Description { get; set; }
//虚方法 子类可以按照需要决定是否重写
public virtual void Use() {
Console.WriteLine("使用道具的方法");
}
public IItem(string name, int iD, string description){
Name = name;
ID = iD;
Description = description;
}
}
}
namespace DesignIntegration{
public abstract class IItemIWeapon : IItem{
//攻击力
protected float AttackValue {
get;
}
protected IItemIWeapon(string name, int iD, string description,
float attackValue) : base(name, iD, description){
AttackValue = attackValue;
}
//抽象攻击方法
public abstract void Attack();
}
}
using System;
namespace DesignIntegration{
public class IWeaponSword : IItemIWeapon{
public IWeaponSword(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
//装备武器
public override void Use(){
Console.WriteLine($",双手紧握");
}
public override void Attack() {
Console.WriteLine($"{Name}发动攻击");
//可以嵌入桥接模式
}
}
}
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
//角色名字
public string Name { get; set; }
//角色ID
public int ID { get; }
}
}
修改抽象武器基类攻击方法
namespace DesignIntegration{
public abstract class IItemIWeapon : IItem{
//攻击力
protected float AttackValue {
get;
}
protected IItemIWeapon(string name, int iD, string description,
float attackValue) : base(name, iD, description){
AttackValue = attackValue;
}
//抽象攻击方法 - 单体攻击
public abstract void Attack(IPlayer player);
}
}
namespace DesignIntegration{
public abstract class IAction{
public string Name { get; }
//播放动作
public abstract void Behaviour();
public IAction(string name){
Name = name;
}
}
}
武器桥接动作
namespace DesignIntegration{
public abstract class IItemIWeapon : IItem{
//攻击力
protected float AttackValue { get; }
//动作
protected IAction _action = null;
//设置武器动作
public void SetWeaponAction(IAction action) {
_action = action;
}
protected IItemIWeapon(string name, int iD, string description,
float attackValue) : base(name, iD, description){
AttackValue = attackValue;
}
//抽象攻击方法 - 单体攻击
public abstract void Attack(IPlayer player);
}
}
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
//角色名字
public string Name { get; set; }
//角色ID
public int ID { get; }
public IRole(string name,int id) {
Name = name;
ID = id;
}
}
}
namespace DesignIntegration{
public class RoleMonster : IRole{
public RoleMonster(string name, int id) : base(name, id){
}
}
}
更新Program类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignIntegration{
public class Program{
public static void Main(string[] args){
//TimeManager.Instance.Greet();
IRole monster = new RoleMonster("Bug兽",20001);
IItem sword = new IWeaponSword("火焰圣剑", 5001, "中级武器", 15f);
((IItemIWeapon)sword).Attack(monster);
}
}
}
using System;
namespace DesignIntegration{
public class ActionSweepHorizontally : IAction{
public ActionSweepHorizontally(string name) : base(name){
}
public override void Behaviour(){
Console.WriteLine("从左至右横扫");
}
}
}
修改武器子剑类
using System;
namespace DesignIntegration{
public class IWeaponSword : IItemIWeapon{
public IWeaponSword(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
//装备武器
public override void Use(){
Console.WriteLine($"双手紧握");
}
public override void Attack(IRole role) {
if (_action == null)
Console.WriteLine($"{Name}发动攻击");
else {
//播放动作动画
_action.Behaviour();
Console.WriteLine($"{Name}发动攻击");
}
//受攻击方减血
}
}
}
运行
新增动作
using System;
namespace DesignIntegration{
public class ActionRaise : IAction{
public ActionRaise(string name) : base(name){
}
public override void Behaviour(){
//新增动作动画
Console.WriteLine("举过头顶 往下劈砍");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignIntegration{
public class Program{
public static void Main(string[] args){
//TimeManager.Instance.Greet();
IRole monster = new RoleMonster("Bug兽",20001);
IItem sword = new IWeaponSword("火焰圣剑", 5001, "中级武器", 15f);
//((IItemIWeapon)sword).SetWeaponAction(new ActionSweepHorizontally("横扫千钧"));
//切换新动作
((IItemIWeapon)sword).SetWeaponAction(new ActionRaise("下劈"));
((IItemIWeapon)sword).Attack(monster);
}
}
}
修改抽象角色基类
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
//角色名字
public string Name { get; set; }
//角色ID
public int ID { get; }
//最大血量
protected float MaxHp { get; set; }
//当前血量
protected float CurrentHp { get; set; }
public IRole(string name,int id,float maxHp) {
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
}
}
}
修改角色子类
namespace DesignIntegration{
public class RoleMonster : IRole{
public RoleMonster(string name, int id, float maxHp) : base(name, id, maxHp){
}
}
}
修改Program类
修改角色基类代码
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
//角色名字
public string Name { get; set; }
//角色ID
public int ID { get; }
//最大血量
protected float MaxHp { get; set; }
//当前血量
protected float CurrentHp { get; set; }
public IRole(string name,int id,float maxHp) {
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
}
//伤害
public void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
}
}
using System;
namespace DesignIntegration{
public class IWeaponSword : IItemIWeapon{
public IWeaponSword(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
//装备武器
public override void Use(){
Console.WriteLine($"双手紧握");
}
public override void Attack(IRole role) {
if (_action == null)
Console.WriteLine($"{Name}发动攻击");
else {
//播放动作动画
_action.Behaviour();
Console.WriteLine($"{Name}发动攻击");
}
//受攻击方减血
role.TakeDamage(AttackValue);
}
}
}
运行实现掉血
接下来设置玩家控制武器打怪物后使怪物掉血
namespace DesignIntegration{
public class RolePlayer : IRole{
public RolePlayer(string name, int id, float maxHp) : base(name, id, maxHp){
}
}
}
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
public string Name { get; set; }//角色名字
public int ID { get; }//角色ID
protected float MaxHp { get; set; }//最大血量
protected float CurrentHp { get; set; }//当前血量
protected float HitValue { get; set; }//无武器攻击值
protected IItem _item = null;//道具
protected IAction _action = null;//动作
public IRole(string name,int id,float maxHp) {
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
}
//伤害
public void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
}
}
修改角色子类
namespace DesignIntegration{
public class RolePlayer : IRole{
public RolePlayer(string name, int id, float maxHp,
float hitValue) : base(name, id, maxHp, hitValue){
}
}
}
namespace DesignIntegration{
public class RoleMonster : IRole{
public RoleMonster(string name, int id, float maxHp,
float hitValue) : base(name, id, maxHp, hitValue){
}
}
}
修改Program类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignIntegration{
public class Program{
public static void Main(string[] args){
//TimeManager.Instance.Greet();
IRole monster = new RoleMonster("Bug兽",20001,60f,20f);
IItem sword = new IWeaponSword("火焰圣剑", 50001, "中级武器", 15f);
//((IItemIWeapon)sword).SetWeaponAction(new ActionSweepHorizontally("横扫千钧"));
//切换新动作
((IItemIWeapon)sword).SetWeaponAction(new ActionRaise("下劈"));
((IItemIWeapon)sword).Attack(monster);
}
}
}
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
public string Name { get; set; }//角色名字
public int ID { get; }//角色ID
protected float MaxHp { get; set; }//最大血量
protected float CurrentHp { get; set; }//当前血量
protected float HitValue { get; set; }//无武器攻击值
protected IItem _item = null;//道具
protected IAction _action = null;//动作
public IRole(string name,int id, float maxHp, float hitValue){
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
HitValue = hitValue;
}
//伤害
public void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
//使用道具
public virtual void UseItem(IItem item) {
_item = item;
Console.WriteLine($"{Name}使用了{_item.Name}");
}
}
}
using System;
namespace DesignIntegration{
public class RolePlayer : IRole{
public RolePlayer(string name, int id, float maxHp,
float hitValue) : base(name, id, maxHp, hitValue){
}
public override void UseItem(IItem item){
_item = item;
Console.WriteLine($"{Name}从背包中拿出{_item.Name}");
_item.Use();
}
}
}
接下来设定道具的使用动作
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
public string Name { get; set; }//角色名字
public int ID { get; }//角色ID
protected float MaxHp { get; set; }//最大血量
protected float CurrentHp { get; set; }//当前血量
protected float HitValue { get; set; }//无武器攻击值
protected IItem _item = null;//道具
protected IAction _action = null;//动作
public IRole(string name,int id, float maxHp, float hitValue){
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
HitValue = hitValue;
}
//伤害
public void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
//使用道具
public virtual void UseItem(IItem item) {
_item = item;
Console.WriteLine($"{Name}使用了{_item.Name}");
//如果是非武器类道具 则_item使用后设为空
if(!(_item is IItemIWeapon))
_item = null;
}
//攻击方法
public virtual void Attack(IRole role) {
//有武器
if (_item != null && _item is IItemIWeapon){
Console.WriteLine($"{Name}使用{_item.Name}发起攻击");
((IItemIWeapon)_item).Attack(role);
}
else {
Console.WriteLine($"空手发起攻击");
role.TakeDamage(HitValue);
}
}
//使用道具的使用动作
public virtual void SetUseItemAction(IAction action) {
_action = action;
//如果_item属于武器 则设置动作
if(_item is IItemIWeapon)
((IItemIWeapon)_item).SetWeaponAction(action);
}
}
}
增加动作
目前玩家可以打怪物 但怪物不可以打玩家
现在做怪物攻击玩家,首先在角色子类怪物类进行重写
using System;
namespace DesignIntegration{
public class RoleMonster : IRole{
public RoleMonster(string name, int id, float maxHp,
float hitValue) : base(name, id, maxHp, hitValue){
}
public override void Attack(IRole role){
//有武器
if (_item != null && _item is IItemIWeapon)
((IItemIWeapon)_item).Attack(role);
else {
Console.WriteLine($"{Name}用嘴咬向{role.Name}");
role.TakeDamage(HitValue);
}
}
}
}
运行即实现桥接模式
接下来显示玩家的状态信息
添加抽象药水基类
namespace DesignIntegration{
//抽象药水基类
public abstract class IItemIPotion : IItem{
protected IItemIPotion(string name, int iD,
string description) : base(name, iD, description){
}
}
}
namespace DesignIntegration{
//生命值药水
public class IPotionRed : IItemIPotion{
public IPotionRed(string name, int iD,
string description) : base(name, iD, description){
}
}
}
using System;
namespace DesignIntegration{
//工厂模式
public abstract class IItem{
public string Name { get; set; }//武器名称
protected int ID { get; set; }//道具编号
protected string Description { get; set; }//道具描述
protected IRole Role { get; set; } //角色
//虚方法 子类可以按照需要决定是否重写
public virtual void Use() {
Console.WriteLine("使用道具的方法");
}
public IItem(string name, int iD, string description){
Name = name;
ID = iD;
Description = description;
}
}
}
修改角色子类
using System;
namespace DesignIntegration{
public class RolePlayer : IRole{
public RolePlayer(string name, int id, float maxHp,
float hitValue) : base(name, id, maxHp, hitValue){
}
public override void UseItem(IItem item){
_item = item;
//将角色传给道具
_item.Role = this;
Console.Write($"{Name}从背包中拿出{_item.Name}");
_item.Use();
}
}
}
using System;
namespace DesignIntegration{
//生命值药水
public class IPotionRed : IItemIPotion{
public IPotionRed(string name, int iD,
string description) : base(name, iD, description){
}
public override void Use(){
if (Role != null) {
Console.WriteLine($"{Role.Name}仰头喝下{Name}");
//加血
}
}
}
}
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
public string Name { get; set; }//角色名字
public int ID { get; }//角色ID
protected float MaxHp { get; set; }//最大血量
protected float CurrentHp { get; set; }//当前血量
protected float HitValue { get; set; }//无武器攻击值
protected IItem _item = null;//道具
protected IAction _action = null;//动作
public IRole(string name,int id, float maxHp, float hitValue){
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
HitValue = hitValue;
}
//伤害
public virtual void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
//加血
public virtual void AddHp(float hp) {
CurrentHp += hp;
if(CurrentHp > MaxHp)
CurrentHp = MaxHp;
}
//使用道具
public virtual void UseItem(IItem item) {
_item = item;
Console.WriteLine($"{Name}使用了{_item.Name}");
//如果是非武器类道具 则_item使用后设为空
if(!(_item is IItemIWeapon))
_item = null;
}
//攻击方法
public virtual void Attack(IRole role) {
//有武器
if (_item != null && _item is IItemIWeapon){
Console.WriteLine($"{Name}使用{_item.Name}发起攻击");
((IItemIWeapon)_item).Attack(role);
}
else {
Console.WriteLine($"空手发起攻击");
role.TakeDamage(HitValue);
}
}
//使用道具的使用动作
public virtual void SetUseItemAction(IAction action) {
_action = action;
//如果_item属于武器 则设置动作
if(_item is IItemIWeapon)
((IItemIWeapon)_item).SetWeaponAction(action);
}
}
}
namespace DesignIntegration{
//抽象药水基类
public abstract class IItemIPotion : IItem{
//附带效果值
protected float _addValue;
protected IItemIPotion(string name, int iD,string description,
float addValue) : base(name, iD, description){
_addValue = addValue;
}
}
}
using System;
namespace DesignIntegration{
//生命值药水
public class IPotionRed : IItemIPotion{
public IPotionRed(string name, int iD, string description,
float addValue) : base(name, iD, description, addValue){
}
public override void Use(){
if (Role != null) {
Console.WriteLine($"{Role.Name}仰头喝下{Name}");
//加血
Role.AddHp(_addValue);
}
}
}
}
using System;
namespace DesignIntegration{
//抽象角色基类
public abstract class IRole{
public string Name { get; set; }//角色名字
public int ID { get; }//角色ID
protected float MaxHp { get; set; }//最大血量
protected float CurrentHp { get; set; }//当前血量
protected float HitValue { get; set; }//无武器攻击值
protected IItem _item = null;//道具
protected IAction _action = null;//动作
public IRole(string name,int id, float maxHp, float hitValue){
Name = name;
ID = id;
MaxHp = maxHp;
CurrentHp = maxHp;
HitValue = hitValue;
}
//伤害
public virtual void TakeDamage(float damage) {
if (damage < CurrentHp){
CurrentHp -= damage;
Console.WriteLine($"{Name}掉了{damage}滴血");
}
else {
CurrentHp = 0;
Console.WriteLine($"{Name}已经死亡");
}
}
//加血
public virtual void AddHp(float hp) {
CurrentHp += hp;
if(CurrentHp > MaxHp)
CurrentHp = MaxHp;
//拓展 计算增加了多少血 差值{hp}有bug
Console.WriteLine($"{Name}当前生命值增加{hp}");
}
//使用道具
public virtual void UseItem(IItem item) {
_item = item;
Console.WriteLine($"{Name}使用了{_item.Name}");
//如果是非武器类道具 则_item使用后设为空
if(!(_item is IItemIWeapon))
_item = null;
}
//攻击方法
public virtual void Attack(IRole role) {
//有武器
if (_item != null && _item is IItemIWeapon){
Console.WriteLine($"{Name}使用{_item.Name}发起攻击");
((IItemIWeapon)_item).Attack(role);
}
else {
Console.WriteLine($"空手发起攻击");
role.TakeDamage(HitValue);
}
}
//使用道具的使用动作
public virtual void SetUseItemAction(IAction action) {
_action = action;
//如果_item属于武器 则设置动作
if(_item is IItemIWeapon)
((IItemIWeapon)_item).SetWeaponAction(action);
}
}
}
接下来我们再创建一把武器弓
using System;
using System.Collections.Generic;
using System.Linq;
namespace DesignIntegration{
public class IWeaponBow : IItemIWeapon{
public IWeaponBow(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
//装备武器
public override void Use(){
Console.WriteLine($"拉弓");
}
public override void Attack(IRole role){
if (_action == null)
Console.WriteLine($"{Name}发动攻击");
else{
//播放动作动画
_action.Behaviour();
Console.WriteLine($"弯弓搭箭 群体攻击");
}
//受攻击方减血
role.TakeDamage(AttackValue);
}
}
}
【||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||工厂模式||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||】
using System;
namespace DesignIntegration{
//简单道具工厂
public class ItemSimpleFactory{
public static IItem CreateItem(string itemType, int id,
string name, string description, float attackValue) {
IItem item = null;
switch (itemType) {
case "Sword":
item = new IWeaponSword(name,id,description,attackValue);
break;
case "Bow":
item = new IWeaponBow(name,id,description,attackValue);
break;
case "PotionRed":
item = new IPotionRed(name, id, description, attackValue);
break;
default:
throw new ArgumentException("未知物品类型");
}
return item;
}
}
}
【||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||建造者模式||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||】
namespace DesignIntegration{
//抽象武器基类
public abstract class IItemIWeapon : IItem{
protected float AttackValue { set; get; }//攻击力
protected IAction _action = null;//动作
protected float Durability { set; get; } = 10f;//耐久度
//设置武器动作
public void SetWeaponAction(IAction action) {
_action = action;
}
protected IItemIWeapon(string name, int iD, string description,
float attackValue) : base(name, iD, description){
AttackValue = attackValue;
}
//抽象攻击方法 - 单体攻击
public abstract void Attack(IRole role);
}
}
namespace DesignIntegration{
//抽象道具建造者
public abstract class IItemBuilder{
//设置待加工道具
public abstract void SetBaseItem(IItem item);
//添加特殊材料改进物品
public virtual void AddMaterial(string material) { }
//提升锋利度
public virtual void ImproveSharpness() { }
//加固物品的结构 增加耐久度
public virtual void ReinforceStructure() { }
//镶嵌宝石 提供特殊属性加成
public virtual void Embedgem(string coatingType) { }
//添加涂层
public virtual void AddCoating(string cocatingType) { }
//升级
public abstract void Upgrade();
//获取升级后的物品
public abstract IItem GetItem();
}
}
using System;
namespace DesignIntegration{
//具体建造者 - 武器升级
public class BuildWeaponUpgrade : IItemBuilder{
private IItemIWeapon _weapon;
public override IItem GetItem(){
return _weapon;
}
public override void SetBaseItem(IItem item){
_weapon = item as IItemIWeapon;
}
public override void Upgrade(){
Console.WriteLine($"{_weapon.Name}铸造完成");
}
public override void AddMaterial(string material){
Console.WriteLine($"铸造过程中 添加{material}材料");
}
public override void Embedgem(string embedgem){
Console.WriteLine($"铸造过程中 嵌入{embedgem}宝石");
}
}
}
修改抽象武器基类代码
修改jurisdiction建造者代码
using System;
namespace DesignIntegration{
//具体建造者 - 武器升级
public class BuildWeaponUpgrade : IItemBuilder{
private IItemIWeapon _weapon;
public override IItem GetItem(){
return _weapon;
}
public override void SetBaseItem(IItem item){
_weapon = item as IItemIWeapon;
}
public override void Upgrade(){
Console.WriteLine($"{_weapon.Name}铸造完成");
//要抽象不要实现 待完善
_weapon.AttackValue *= 1.2f;
_weapon.Durability *= 1.1f;
}
public override void AddMaterial(string material){
Console.WriteLine($"铸造过程中 添加{material}材料");
}
public override void Embedgem(string embedgem){
Console.WriteLine($"铸造过程中 嵌入{embedgem}宝石");
}
}
}
namespace DesignIntegration{
//道具指挥者
public class BuildDirector{
public IItem Construct(IItemBuilder builder,
IItem baseItem, string material, string gemType){
builder.SetBaseItem(baseItem);
builder.AddMaterial(material);
builder.Embedgem(gemType);
builder.Upgrade();
return builder.GetItem();
}
}
}
namespace DesignIntegration{
public class Program{
public static void Main(string[] args){
//TimeManager.Instance.Greet();
IRole monster = new RoleMonster("Bug兽",20001,60f,20f);
//剑培
IItem sword = new IWeaponSword("火焰圣剑", 50001, "中级武器", 15f);
//((IItemIWeapon)sword).SetWeaponAction(new ActionSweepHorizontally("横扫千钧"));
//切换新动作
//((IItemIWeapon)sword).SetWeaponAction(new ActionRaise("下劈"));
//((IItemIWeapon)sword).Attack(monster);
IRole player = new RolePlayer("小虎", 10001, 30f, 5f);
player.UseItem(sword);
player.SetUseItemAction(new ActionSweepHorizontally("横扫千钧"));
player.Attack(monster);
player.SetUseItemAction(new ActionRaise("下劈"));
player.Attack(monster);
monster.Attack(player);
IItem redpotion = new IPotionRed("生命药水", 50101, "加血药瓶", 50f);
player.UseItem(redpotion);
var weaponBuilder = new BuildWeaponUpgrade();
var director = new BuildDirector();
var upgradedSword = director.Construct(weaponBuilder, sword, "千年玄晶", "红宝石");
}
}
}
using System;
namespace DesignIntegration{
public class Program{
public static void Main(string[] args){
//TimeManager.Instance.Greet();
IRole monster = new RoleMonster("Bug兽",20001,60f,20f);
//剑培
IItem sword = new IWeaponSword("火焰圣剑", 50001, "中级武器", 15f);
//((IItemIWeapon)sword).SetWeaponAction(new ActionSweepHorizontally("横扫千钧"));
//切换新动作
//((IItemIWeapon)sword).SetWeaponAction(new ActionRaise("下劈"));
//((IItemIWeapon)sword).Attack(monster);
IRole player = new RolePlayer("小虎", 10001, 30f, 5f);
player.UseItem(sword);
player.SetUseItemAction(new ActionSweepHorizontally("横扫千钧"));
player.Attack(monster);
player.SetUseItemAction(new ActionRaise("下劈"));
player.Attack(monster);
monster.Attack(player);
IItem redpotion = new IPotionRed("生命药水", 50101, "加血药瓶", 50f);
player.UseItem(redpotion);
var weaponBuilder = new BuildWeaponUpgrade();
var director = new BuildDirector();
var upgradedSword = director.Construct(weaponBuilder, sword, "千年玄晶", "红宝石") as IItemIWeapon;
Console.WriteLine($"铸造后的攻击力是{upgradedSword.AttackValue}");
}
}
}
【||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||装饰者模式||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||】
namespace DesignIntegration{
public interface IEnhancer{
//装饰者模式接口
void ApplyEffect(IRole role);
}
}
using System;
namespace DesignIntegration{
public class EnhancerFrozenEffect : IEnhancer{
private float _freezeChance;//冰冻几率
public EnhancerFrozenEffect(float freezeChance){
_freezeChance = freezeChance;
}
public void ApplyEffect(IRole role){
float randomValue = new Random().Next(0,100)/100f;
if (randomValue < _freezeChance){
Console.WriteLine($"{role.Name}被冰冻了");
//角色内应有被冰冻的状态
//role.Freeze();
}
else {
Console.WriteLine("冰冻失败");
}
}
}
}
namespace DesignIntegration{
//抽象装饰类
public class IWeaponEnhanced : IItemIWeapon{
//增强功能接口引用
private IEnhancer _enhancer = null;
public IWeaponEnhanced(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
//设置增强配件
public void SetEnhancer(IEnhancer enhancer) {
_enhancer= enhancer;
}
public override void Attack(IRole role){
//额外增加魔法攻击
if(_enhancer != null)
_enhancer.ApplyEffect(role);
//写法等同
//_enhancer?.ApplyEffect(role);
}
}
}
using System;
namespace DesignIntegration{
public class EnhancerFrozenMagicSword : IWeaponEnhanced{
public EnhancerFrozenMagicSword(string name, int iD, string description,
float attackValue) : base(name, iD, description, attackValue){
}
public override void Attack(IRole role){
if (_action == null)
Console.WriteLine($"{Name}发动攻击");
else{
//播放动作动画
_action.Behaviour();
Console.WriteLine($"{Name}发动攻击");
}
//受攻击方减血
role.TakeDamage(AttackValue);
//增强魔法效果
base.Attack(role);
}
}
}