http cutewebi tistory com cutewebigmail com BRIDGE PATTERN
http: //cutewebi. tistory. com cutewebi@gmail. com 김희정 BRIDGE PATTERN
Example_Two steps
Example_Switch
Example_Window
Example_Seat
sample code in C# Structural example using System; ab. Operation(); namespace // Change implemention and call Do. Factory. Gang. Of. Four. Bridge. Structur ab. Implementor = new al Concrete. Implementor. B(); { ab. Operation(); // Main. App test application // Wait for user Console. Read(); class Main. App } { } static void Main() { Abstraction ab = new Refined. Abstraction(); // Set implementation and call ab. Implementor = new Concrete. Implementor. A();
sample code in C# Structural example // "Abstraction" // "Implementor" class Abstraction abstract class Implementor { protected Implementor implementor; { // Property public abstract void Operation(); public Implementor } { set{ implementor = value; } } public virtual void Operation() { implementor. Operation(); } }
sample code in C# Structural example // "Refined. Abstraction" class Refined. Abstraction : Abstraction { public override void Operation() { implementor. Operation(); } }
sample code in C# Structural example // "Concrete. Implementor. A" class Concrete. Implementor. A : Implementor { public override void Operation() { Console. Write. Line("Concrete. Implementor. A Operation"); } } // "Concrete. Implementor. B" class Concrete. Implementor. B : Implementor { public override void Operation() { Console. Write. Line("Concrete. Implementor. B Operation"); } } }
sample code in C# Structural example
sample code in C# Real World example using System; customers. Data = new Customers. Data() using System. Collections; // Exercise the bridge namespace customers. Show(); Do. Factory. Gang. Of. Four. Bridge. Real. Worl customers. Next(); d customers. Show(); { customers. Next(); customers. Show(); // Main. App test application class Main. App customers. New("Henry Velasquez"); { customers. Show. All(); static void Main() // Wait for user { Console. Read(); // Create Refined. Abstraction } Customers customers = } new Customers("Chicago"); // Set Concrete. Implementor
sample code in C# Real World example // "Abstraction" class Customers. Base { private Data. Object data. Object; protected string group; public Customers. Base(string group) { this. group = group; }
sample code in C# Real World example // Property public virtual void Show() public Data. Object Data { data. Object. Show. Record(); } { public virtual void Show. All() set{ data. Object = value; } { get{ return data. Object; } Console. Write. Line("Customer Group: } " + group); public virtual void Next() data. Object. Show. All. Records(); } { data. Object. Next. Record(); } } public virtual void Prior() { data. Object. Prior. Record(); } public virtual void New(string name) { data. Object. New. Record(name); } public virtual void Delete(string name) { data. Object. Delete. Record(name); }
sample code in C# Real World example // "Refined. Abstraction" ---"); class Customers : Customers. Base } { } // Constructor // "Implementor" public Customers(string group) : abstract class Data. Object { base(group) public abstract void Next. Record(); { public abstract void Prior. Record(); } public abstract void New. Record(string public override void Show. All() name); { public abstract void // Add separator lines Delete. Record(string name); Console. Write. Line(); Console. Write. Line ("----------- public abstract void Show. Record(); public abstract void Show. All. Records(); ---"); } base. Show. All(); Console. Write. Line ("-----------
sample code in C# Real World example // "Concrete. Implementor" class Customers. Data : Data. Object { private Array. List customers = new Array. List(); private int current = 0; public Customers. Data() { // Loaded from a database customers. Add("Jim Jones"); customers. Add("Samual Jackson"); customers. Add("Allen Good"); customers. Add("Ann Stills"); customers. Add("Lisa Giolani"); } public override void Next. Record() { if (current <= customers. Count - 1) { current++; } } public override void Prior. Record() { if (current > 0) { current--; } }
sample code in C# Real World example public override void New. Record(string public override void Show. All. Records() name) { { foreach (string name in customers) customers. Add(name); { } Console. Write. Line(" " + name); public override void } } Delete. Record(string name) } { } customers. Remove(name); } public override void Show. Record() { Console. Write. Line(customers[curren t]); }
sample code in C# Real World example
Example_Image
Example_Image
Example_Image C# Implementation // Structural Pattern: BRIDGE using System; //Base class for OS implemenations interface Image. Imp { void Do. Paint(string str); } //Windows specific implemenation class Win. Imp : Image. Imp { public void Do. Paint(string str) { Console. Write. Line(str + " WIN OS"); } } //Abstract class for all image paintings class Image { public void Set. Image. Imp(Image. Imp ip) { imp. To. Use = ip; } public virtual void Method(string s 1) { } protected Image. Imp imp. To. Use; }
Example_Image C# Implementation //BMP specific paintings class BMPImage : Image { override public void Method(string s 1) { string s 2 = s 1 + " BMP IMAGE"; imp. To. Use. Do. Paint(s 2); } } //Client class My. Paint { public Image Set. Up. Method() { Image im = new BMPImage(); // BMP IMAGE Image. Imp win = new Win. Imp(); // WIN OS im. Set. Image. Imp(win); return im; } } class My. Main { public static void Main() { My. Paint mp = new My. Paint(); Image im = mp. Set. Up. Method(); im. Method("PAINTING-->"); } }
관련패턴 Example (Example_GUI Component)
- Slides: 46