桥接模式-坦克大战-c#

mac2024-04-06  33

概要

将抽象部分与实现部分分离,使它们都可以独立的变化。

类图

运行 

代码 

using System; namespace 桥接模式 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Client client = new Client(); client.main(); Console.ReadLine(); } } interface IAction { void operation(); } interface IImplementor { void operationImp(); } abstract class Abstraction : IAction { protected IImplementor implementor; public void operation() { implementor.operationImp(); } } class Tank: Abstraction { public void setIImplementor(IImplementor implementor) { this.implementor = implementor; } } class Sort : IImplementor { public void operationImp() { Console.WriteLine("发射炮弹"); } } class Run : IImplementor { public void operationImp() { Console.WriteLine("跑"); } } class Client { public void main() { IImplementor sort = new Sort(); IImplementor run = new Run(); Tank tank = new Tank(); tank.setIImplementor(sort); tank.operation(); tank.setIImplementor(run); tank.operation(); } } }

英语

abstraction:  n. 抽象;提取;抽象概念;空想;心不在焉 implementor n. 实现者; 系统 refined adj. [油气][化工][冶] 精炼的; 精确的; 微妙的; 有教养的 concrete adj. 混凝土的; 实在的,具体的; 有形的; vi. 凝结; vt. 使凝固; 用混凝土修筑;  operation n. 操作; 经营; [外科] 手术; [数][计] 运算’

最新回复(0)