using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor){
visitor.VisitLight(this);}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor){
visitor.VisitArmored(this);}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){
unit.Health -=21;}publicvoidVisitArmored(IArmoredUnit unit){
unit.Health -=32;}}
答案2:
publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor){
visitor.VisitLight(this);}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor){
visitor.VisitArmored(this);}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){
unit.Health -=21;}publicvoidVisitArmored(IArmoredUnit unit){
unit.Health -=32;}}
答案3:
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}// Health and Accept() can be encapsulated into a single interface contract// since these are shared by all units.publicinterfaceIUnit{int Health {get;set;}voidAccept(IVisitor visitor);}// Created an abstract Unit class for shared property implementation// and forced adherence to the IUnit patternpublicabstractclassUnit{privateint _health;publicint Health
{get{return _health;}set{ _health =value;}}protectedUnit(){}protectedUnit(int health){this._health = health;}publicabstractvoidAccept(IVisitor visitor);}// Converted LightUnit/ArmoredUnit to Abstract classes inherited from Unit, // so if we create more concrete unit classes we already have the Accept(IVisitor)// implemented correctly.publicabstractclassILightUnit:Unit{protectedILightUnit(int health):base(health){}publicoverridevoidAccept(IVisitor visitor){
visitor.VisitLight(this);}}publicabstractclassIArmoredUnit:Unit{protectedIArmoredUnit(int health):base(health){}publicoverridevoidAccept(IVisitor visitor){
visitor.VisitArmored(this);}}// Note how our concrete units only need a starting health value that// we pass into the constructor, and everything else is handled.// Since the Accept() method is abstract, we can override it in any// class for a specific implementation as needed (see: Marauder)publicclassMarine:ILightUnit{publicMarine():base(100){}}publicclassMarauder:IArmoredUnit{publicMarauder():base(125){}publicoverridevoidAccept(IVisitor visitor){base.Accept(visitor);
Console.WriteLine("Visited!");}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){
unit.Health -=21;}publicvoidVisitArmored(IArmoredUnit unit){
unit.Health -=32;}}
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor)=> visitor.VisitLight(this);}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor)=> visitor.VisitArmored(this);}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit)=> unit.Health -=21;publicvoidVisitArmored(IArmoredUnit unit)=> unit.Health -=32;}
答案6:
using System;publicinterfaceIVisitor{voidVisit(ILightUnit unit);voidVisit(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor){
visitor.Visit(this);}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor){
visitor.Visit(this);}}publicclassTankBullet:IVisitor{publicvoidVisit(ILightUnit unit){
unit.Health -=21;}publicvoidVisit(IArmoredUnit unit){
unit.Health -=32;}}
答案7:
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{privateint _Health =100;publicint Health
{get{return _Health;}set{ _Health =value;}}publicvoidAccept(IVisitor visitor){
visitor.VisitLight(this);}}publicclassMarauder:IArmoredUnit{privateint _Health =125;publicint Health
{get{return _Health;}set{ _Health =value;}}publicvoidAccept(IVisitor visitor){
visitor.VisitArmored(this);}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){
unit.Health -=21;}publicvoidVisitArmored(IArmoredUnit unit){
unit.Health -=32;}}
答案8:
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor){
Health = Health -21;}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor){
Health = Health -32;}}publicclassTankBullet:IVisitor{publicint Health {get{returnthis.Health;}}publicvoidVisitLight(ILightUnit unit){
unit.Accept(this);}publicvoidVisitArmored(IArmoredUnit unit){
unit.Accept(this);}}
答案9:
using System;publicinterfaceIVisitor{voidVisit(IUnit unit);}publicinterfaceIVisitable{voidAccept(IVisitor visitor);}publicabstractclassVisitableUnit<TUnit>: IVisitable,IUnitwhere TUnit : IUnit
{publicint Health {get;set;}publicvoidAccept(IVisitor visitor){
visitor.Visit(this);}}publicinterfaceIUnit{int Health {get;set;}}publicinterfaceILightUnit:IVisitable, IUnit
{}publicinterfaceIArmoredUnit:IVisitable, IUnit
{}publicclassMarine:VisitableUnit<Marine>, ILightUnit
{publicMarine(){
Health =100;}}publicclassMarauder:VisitableUnit<Marauder>, IArmoredUnit
{publicMarauder(){
Health =125;}}publicclassTankBullet:IVisitor{publicvoidVisit(IUnit unit){if(unit is IArmoredUnit)
unit.Health -=32;if(unit is ILightUnit)
unit.Health -=21;}}
答案10:
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}publicMarine(){this.Health =100;}publicvoidAccept(IVisitor visitor){
visitor.VisitLight(this);}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}publicMarauder(){
Health =125;}publicvoidAccept(IVisitor visitor){
visitor.VisitArmored(this);}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){
unit.Health -=21;}publicvoidVisitArmored(IArmoredUnit unit){
unit.Health -=32;}}
答案11:
using System;publicinterfaceIVisitor{voidVisitLight(ILightUnit unit);voidVisitArmored(IArmoredUnit unit);}publicinterfaceILightUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicinterfaceIArmoredUnit{int Health {get;set;}voidAccept(IVisitor visitor);}publicclassMarine:ILightUnit{publicint Health {get;set;}=100;publicvoidAccept(IVisitor visitor){this.Health -=21;}}publicclassMarauder:IArmoredUnit{publicint Health {get;set;}=125;publicvoidAccept(IVisitor visitor){this.Health -=32;}}publicclassTankBullet:IVisitor{publicvoidVisitLight(ILightUnit unit){}publicvoidVisitArmored(IArmoredUnit unit){}}