Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- // путешественник
- Driver driver = new Driver();
- // машина
- Auto auto = new Auto();
- // отправляемся в путешествие
- //driver.Travel(auto);
- // встретились пески, надо использовать верблюда
- Camel camel = new Camel();
- driver.Travel(camel);
- // используем адаптер
- //ITransport camelTransport = new CamelToTransportAdapter(camel);
- //// продолжаем путь по пескам пустыни
- //driver.Travel(camelTransport);
- IAnimal autoCamel = new TransportAdapterToCamel(auto);
- driver.Travel(autoCamel);
- Console.Read();
- }
- }
- interface ITransport
- {
- void Drive();
- }
- // класс машины
- class Auto : ITransport
- {
- public void Drive()
- {
- Console.WriteLine("Машина едет по дороге");
- }
- }
- class Driver
- {
- public void Travel(IAnimal transport)
- {
- transport.Move();
- }
- }
- // интерфейс животного
- interface IAnimal
- {
- void Move();
- }
- // класс верблюда
- class Camel : IAnimal
- {
- public void Move()
- {
- Console.WriteLine("Верблюд идет по пескам пустыни");
- }
- }
- // Адаптер от Camel к ITransport
- class CamelToTransportAdapter : ITransport
- {
- Camel camel;
- public CamelToTransportAdapter(Camel c)
- {
- camel = c;
- }
- public void Drive()
- {
- camel.Move();
- }
- }
- class TransportAdapterToCamel : IAnimal
- {
- Auto auto;
- public TransportAdapterToCamel(Auto c)
- {
- auto = c;
- }
- public void Move()
- {
- auto.Drive();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement