Design Pattern 14-Bridge

xiaoxiao2021-03-06  56

/ * * Open abstraction and behavior, each independent, but can dynamically bind. * * For example, a cup of coffee is an example, and the subclasses are four: medium cups and milk, large cups and milk, CC Can't add milk, big cup does not add milk.

However, we note that there is a concept overlap in the four subclasses above, and can be considered from another angle. These four classes are actually a combination of two roles: abstraction and behavior, where abstraction is: medium cup and big cup; behavior For: adding milk and can't add milk (such as add orange juice plus apple juice).

Implementing the four subclasses have a fixed binding relationship between abstraction and behavior. If the behavior of adding grape juice is increased in the future, it must add two classes: Cup plus grape juice and cup of grape juice. Obviously confusion, the spread is extremely poor.

* / using system;

Namespace pattern {///

/// summary description for class1. /// /////

Public Abstract Class Coffee {CoffeeImp CoffeeImp;

Public void setCoffeeImp () {this.coffeemep = coffeeImpsingleton.getthecoffeeImp (); // added, add sugar} public coffeeimp getCoffeeImp () {return this.coffeemep;}

Public Abstract void PourCoffee (); // is a big cup, or a small cup}

///

/// is an unpailed behavior /// public abstract class coffeeimp {public abstract void PourCoffeeImp ();

// Medium Cup Public Class MediumCoffee: Coffee {Public MediumCoffee () {setCoffeeImp ();

Public override void pourCoffee () {coffeeimp coffeeimp = this.getCoffeeImp (); // We use repetitions to show a cup or a big cup, repeat 2 times is Medium Cup for (int i = 0; i <2; i ) ) {

CoffeeImp.PourCoffeeImp ();}}}

// Cup public class supersizecoffee: coffee {public supersizeCoffee () {setCoffeeImp ();

Public override void PourCoffee () {coffeeImp coffeeimp = this.getCoffeeImp (); // We use the number of repetitions is a cup or a big cup, repeat 5 times is a big cup for (int i = 0; i <5; i ) ) {

CoffeeImp.PourCoffeeImp ();}}}

// Milk PUBLIC CLASS MILKCOFEEIMP: CoffeeImp {Public MilkCoffeeImp () {}

Public override void PourCoffeeImp () {System.Console .writeline ("Add Delicious Milk");}}

// Do not add Public Class FragranticCoffeeImp: CoffeeImp {public fragrantcoffeeImp () {}

Public override void PourCoffeeImp () {system.console .writeline ("What doesn't add, fragrant");}}} public class coffeeimpsingleton {private static coffeeImp coffeeimp;

Public coffeeImpsingleton (CoffeeImp CoffeeImpin) {coffeeImp = coffeeImpin;

Public static coffeeimp getthecoffeeImp () {return coffeeimp;}}

} // // Bridge mode /// / / Take out milk // coffeeimpsingleton coffeeImpsingleton = new coffeeImpsingleton (new milkcoffee); //// / 中 中 加 加 / // console.writeline ("Cup plit "); // MediumCoffee MediumCoffee = new mediumCoffee (); // mediumCoffee.PourCoffee (//// console.writeline (" big cup plus milk "); // / Big cup of milk // SuperSizeCoffee SupersizeCoffee = New supersizeCoffee (); // supersizeCoffee.PourCoffee ();

转载请注明原文地址:https://www.9cbs.com/read-111827.html

New Post(0)