Saturday, 15 June 2013

c# - Design pattern Adapter -



c# - Design pattern Adapter -

i'm learning adapter pattern. have code. it's pattern? sqliteconnection, sqlitecommand, sqlitedatareader - it's other library

now have quest: connect , return users database. chose adapter pattern:

public class user { public string _name { get; set; } public string _surname { get; set;} } public interface iexercise { void connecttodatabase(); void list<user> returnallusers(); } public adapter : iexercise { sqliteconnection _conn = null; sqlitecommand _cmd; public adapter(sqliteconnection conn, sqlitecommand cmd){ _conn = conn; _cmd = cmd; } public void connecttodatabase(){ // not of import yet } public list<user> returnallusers(){ _cmd = _conn.newcommand(); _cmd.commandtext = "select * users"; sqlitedatareader dt = _cmd.executereader(); list<user> lu = new list<user>(); while(dt.read()){ lu.add(new user { _name = dt["name"].tostring(), _surname = dt["surname"].tostring() } ); } homecoming lu; } }

and question : it's adapter pattern?

i dont understand why think code can adapter, doesn't have unmatching interfaces, adapter pattern maps interface of 1 class onto can work together. these incompatible classes may come different libraries or frameworks.

working example

http://ideone.com/zzbwae

definition

an adapter helps 2 incompatible interfaces work together. real world definition adapter. adapter design pattern used when want 2 different classes incompatible interfaces work together. interfaces may incompatible inner functionality should suit need. adapter pattern allows otherwise incompatible classes work converting interface of 1 class interface expected clients.

example http://www.dofactory.com/patterns/patternadapter.aspx

using system; namespace adapterpattern { /// <summary> /// mainapp startup class real-world /// adapter design pattern. /// </summary> class mainapp { /// <summary> /// entry point console application. /// </summary> static void main() { // non-adapted chemical compound compound unknown = new compound("unknown"); unknown.display(); // adapted chemical compounds compound water = new richcompound("water"); water.display(); compound benzene = new richcompound("benzene"); benzene.display(); compound ethanol = new richcompound("ethanol"); ethanol.display(); // wait user console.readkey(); } } /// <summary> /// 'target' class /// </summary> class compound { protected string _chemical; protected float _boilingpoint; protected float _meltingpoint; protected double _molecularweight; protected string _molecularformula; // constructor public compound(string chemical) { this._chemical = chemical; } public virtual void display() { console.writeline("\ncompound: {0} ------ ", _chemical); } } /// <summary> /// 'adapter' class /// </summary> class richcompound : compound { private chemicaldatabank _bank; // constructor public richcompound(string name) : base(name) { } public override void display() { // adaptee _bank = new chemicaldatabank(); _boilingpoint = _bank.getcriticalpoint(_chemical, "b"); _meltingpoint = _bank.getcriticalpoint(_chemical, "m"); _molecularweight = _bank.getmolecularweight(_chemical); _molecularformula = _bank.getmolecularstructure(_chemical); base.display(); console.writeline(" formula: {0}", _molecularformula); console.writeline(" weight : {0}", _molecularweight); console.writeline(" melting pt: {0}", _meltingpoint); console.writeline(" boiling pt: {0}", _boilingpoint); } } /// <summary> /// 'adaptee' class /// </summary> class chemicaldatabank { // databank 'legacy api' public float getcriticalpoint(string compound, string point) { // melting point if (point == "m") { switch (compound.tolower()) { case "water": homecoming 0.0f; case "benzene": homecoming 5.5f; case "ethanol": homecoming -114.1f; default: homecoming 0f; } } // boiling point else { switch (compound.tolower()) { case "water": homecoming 100.0f; case "benzene": homecoming 80.1f; case "ethanol": homecoming 78.3f; default: homecoming 0f; } } } public string getmolecularstructure(string compound) { switch (compound.tolower()) { case "water": homecoming "h20"; case "benzene": homecoming "c6h6"; case "ethanol": homecoming "c2h5oh"; default: homecoming ""; } } public double getmolecularweight(string compound) { switch (compound.tolower()) { case "water": homecoming 18.015; case "benzene": homecoming 78.1134; case "ethanol": homecoming 46.0688; default: homecoming 0d; } } } }

c# design-patterns adapter

No comments:

Post a Comment