Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AdapterExample
- {
- // We try to adapt a robot to be a human!
- // We need an adaptor class for this. Example below...
- abstract class Human
- {
- public abstract string GetName();
- public abstract int GetIQ();
- }
- abstract class Robot
- {
- public abstract string GetName();
- public abstract int GetMemory();
- }
- class Student : Human
- {
- string name;
- int IQ;
- public override string GetName()
- {
- return name;
- }
- public override int GetIQ()
- {
- return IQ;
- }
- public Student(string name, int IQ)
- {
- this.name = name;
- this.IQ = IQ;
- }
- }
- class Robot2Human : Human
- {
- Robot robot;
- public Robot2Human(Robot r)
- {
- robot = r;
- }
- public override string GetName()
- {
- return robot.GetName();
- }
- public override int GetIQ()
- {
- return robot.GetMemory();
- }
- }
- class StarWarsRobot : Robot
- {
- string name;
- int memory;
- public override string GetName()
- {
- return name;
- }
- public override int GetMemory()
- {
- return memory;
- }
- public StarWarsRobot(string name, int memory)
- {
- this.name = name;
- this.memory = memory;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Robot r2d2 = new StarWarsRobot("R2D2", 1024);
- Human human = new Robot2Human(r2d2);
- Console.WriteLine(human.GetIQ()); // 1024
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement