Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have java model like this:

C#
public class AttackCalcObserver {

	public boolean checkStatus(AttackStatus status) {
		return false;
	}

}


C#
public class AttackStatusObserver extends AttackCalcObserver {

	protected int value;
	protected AttackStatus status;

	/**
	 * @param value
	 * @param status
	 */
	public AttackStatusObserver(int value, AttackStatus status) {
		this.value = value;
		this.status = status;
	}
}


main class:

C#
public class AlwaysDodgeEffect {

	public void startEffect(final Effect effect) {
		AttackCalcObserver acObserver = new AttackStatusObserver(value, AttackStatus.DODGE) {

			@Override
			public boolean checkStatus(AttackStatus status) {
				if (status == AttackStatus.DODGE) {
					if (value <= 1)
						effect.endEffect();
					else
						value--;

					return true;
				}
				else
					return false;
			}

		};
	}

}


What I have tried:

this is what i tried in c#. The classes AttackCalcObserver and AttackStatusObserver are same in java.

so this is AlwaysDodgeEffect class in c#:

C#
public class AlwaysDodgeEffect
    {
        public override void startEffect(Effect effect)
        {
            AttackCalcObserver acObserver = new AttackStatusObserver(value, AttackStatus.DODGE)
            {
                //what i need todo here?
            };
        }
    }
Posted
Updated 16-Dec-16 8:53am
Comments
Richard MacCutchan 16-Dec-16 10:57am    
I think you would need to use Events and Delegates to achieve the same effect.
EADever 16-Dec-16 20:50pm    
Can you give me an example?
Richard MacCutchan 17-Dec-16 4:10am    
Ramza360 has given you one below. Or you could look at the samples at C# Tutorials (C#)[^].

1 solution

This can be accomplished fairly easily with delegates.

Example:

C#
public delegate boolean CheckStatusDelegate(AttackStatusObserver observer);

public class AttackStatusObserver {
        protected int value;    
        protected AttackStatus status;
        protected CheckStatusDelegate statusDelegate;

        public int Value {
            get { return this.value; }
            set { this.value = value; }
        }

        public AttackStatus Status {
            get { return this.status; }
        }

	/**
	 * @param value
	 * @param status
	 */
	public AttackStatusObserver(int value, AttackStatus status, CheckStatusDelegate statusDelegate) {
		this.value = value;
		this.status = status;
	        this.statusDelegate = statusDelegate;
        }

        public override bool checkStatus(int value, AttackStatus status) {
            if (null != statusDelegate) {
                // Only do return if you do not wish to process anything else
                return statusDelegate(this);
            }

            // otherwise process normal checkStatus
            //
        }
}

public class AlwaysDodgeEffect {
    public override void startEffect(Effect effect) {
        AttackCalcObserver acObserver = new AttackStatusObserver(value, AttackStatus.DODGE, CheckStatusCallback);
        // Alternatively you can have an event or even the constructor call this internally.
        acObserver.checkStatus(AttackStatus.DODGE);
    }

    private bool CheckStatusCallback(AttackStatusObserver observer) {
        if (observer.AttackStatus == AttackStatus.DODGE) {
           if (observer.Value <= 1)
                effect.endEffect();
           else
                observer.Value--;

           return true;
        }
        else
            return false;
    }
}


Another approach would be to use the Visitor pattern.
Implementing the Visitor pattern requires that you add a few methods to the base class and creating a Visitor object. Its a bit more complex but can be VERY powerful.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900