Thursday, August 31, 2006

think in generics: generics patterns, and design patterns with generics

Here are links to help think in generics, generics patterns, and design patterns with generics


------CSLA generics
http://www.rockfordlhotka.org/WeBlog/PermaLink.aspx?guid=10eb26b4-f52d-4ea9-9cb8-ac8c103ee1f2

http://forums.lhotka.net/forums/thread/5168.aspx


------Curiously Recurring Generic Pattern
http://www.artima.com/forums/flat.jsp?forum=106&thread=133275


-----Factory and generics

http://weblogs.asp.net/pgielens/archive/2004/07/01/171183.aspx

http://weblogs.asp.net/gmilano/archive/2005/07/29/420980.aspx



---------------------------------------
(I copy some code here for later reference; they do not have any comments; so, please go the above link for original thoughts)



public interface NoCastCloneable extends Cloneable
{ public T clone();
}

public class NoCastCloneableImpl implements NoCastCloneable {
public NoCastCloneableImpl clone()
{
return (NoCastCloneableImpl)this.clone();
}
}

--------------------------added here because comments cannot have !!

Another piece of code, from Jeffrey Richter’s “CLR via C#” (not from Tod Golding “Professional .Net 20 Generics – which is a “five star” book, but can be better to include “generics design patterns”)

Again, please refer to the original books! I put the code here for my later reference – I am gradually clearing up my notes.

------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{

/// you want all elements the same exact type T
/// or its subtype
/// but why we do not want to similar thing:
/// array of subclass is a subclass of array of class
/// (note: in java this is not the case; C# "fixed" it
/// but now generics it is "broken" again
///

public sealed class Node
{
public TType m_data;
public Node m_next;
public Node(TType data) : this(data, null)
{
}

public Node(TType data, Node next)
{
m_data = data;
m_next = next;
}

public override string ToString()
{
return m_data.ToString() +

((m_next != null)? m_next.ToString():null);
}

public static void TestIt()
{
Node head = new Node ('C');
head = new Node('B', head);
head = new Node('A', head);
MessageBox.Show("" + head.ToString());

}

}

/////////////////////////
public class Node2 {
protected Node2 m_next;

public Node2(Node2 next)
{
m_next = next;
}
}

public sealed class Node3 : Node2
{
public T m_data;


public Node3(T data): this(data, null)
{
}

public Node3(T data, Node2 next) :base(next)
{
m_data = data;
}

public override string ToString()
{
return m_data.ToString() +

((m_next != null) ? m_next.ToString() : null);
}

public static void TestIt()
{
Node2 head = new Node3('.');
head = new Node3(DateTime.Now, head);
head = new Node3("today is ", head);
MessageBox.Show("" + head.ToString());

}

}


}




---------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace PaulGielens.Patterns.Creational
{
public abstract class Vehicle
{
protected string name;
protected string type;

public Vehicle()
{
}

public Vehicle(string name)
{
this.name = name;
}

// a person who bought my car wants to change its name, unless
public string Name
{
get { return name; }
set { name = value; }
}

// but can't change it's type
public string Type
{
get { return type; }
}
}

// a concrete vehicle
public class Car : Vehicle
{
public Car() : base()
{
}

public Car(string name, string type) : base(name)
{
base.type = type;
}
}

interface IVehicleFactory
{
T CreateObject(string name) where T : Vehicle, new();
}

public class VehicleFactory : IVehicleFactory
{
public T CreateObject(string name) where T : Vehicle, new()
{
// create instance of Type T where this type derives from Vehicle
T v = new T();
v.Name = name;

return v;
}
}

public class FactoryPatternUsingGenericMethod
{
static void Main(string[] args)
{
IVehicleFactory f = new VehicleFactory();
Car c = f.CreateObject("Kitty");
}
}
}
---------------------------------------

public interface IFactory
{
T Create();
}

public class Factory : IFactory where T : new()
{
public T Create()
{
return new T();
}
}
----------------------------------------


public interface IFactory
{
T Create();
}

public class Factory : IFactory where T : new()
{
public T Create()
{
return new T();
}
}
-----------------------------------------

public interface ICar
{
string Name { get; }
}

public class Ford : ICar
{
#region ICar Members

public string Name
{
get { return "Ford"; }
}


#endregion
}

public class Fiat : ICar
{
#region ICar Members

public string Name
{
get { return "Fiat"; }
}

#endregion

}

-----------------------------------------


public interface IFactory where K : IComparable
{
T Create();
}


public interface IFactoryElement
{
object New();
}


public class FactoryElement : IFactoryElement
where T : new()
{

public object New()
{
return new T();
}

}

public class Factory : IFactory where K : IComparable
{
/// Elements that can be created
Dictionary elements = new Dictionary();

///
/// Add a new creatable kind of object to the factory. Here is the key with the beauty of the constrains in generics. Look that we are saying that V should be derived of T and it must be creatable
///

public void Add(K key) where V : T, new()
{
elements.Add(key, new FactoryElement());
}

public T Create(K key)
{
if (elements.ContainsKey(key))
{
return (T) elements[key].New();
}
throw new ArgumentException();
}
}



public class CarFactory
{
Factory internalFactory = new Factory();

public class CarFactory()
{
internalFactory.Add("fiat");
internalFactory.Add("ford");
}

public ICar Create(string carType)
{
return internalFactory.Create(carType);
}
}

------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
---------------------more Links for generics design patterns


----1. http://www.lrde.epita.fr/dload/papers/coots01.html

There are many other papers (C++ and academic) in:
http://www.lrde.epita.fr/cgi-bin/twiki/view/Publications/Papers
especially:
http://www.lrde.epita.fr/dload/papers/europlop00/index.html
http://www.lrde.epita.fr/cgi-bin/twiki/view/Publications/200310-MPOOL


----2. http://www.codeproject.com/cs/design/GenericSingletonPattern.asp

----3. http://www.dofactory.com/Patterns/Patterns.aspx : this one is commercial, I do not know whether they have what they claim that they have. I am not sure… but put the link here anyway.

---------------------------------------
---------------------------------------
---------I have posted the following before, but I am putting them together here (I do not copy the code -- the code was only for backup purpose anyway)
http://survic.blogspot.com/2006/08/think-in-generics-generics-patterns.html

Note that generics and inheritance is interesting:

a. CLR via C# by Jeffrey Richer about moving generics out of inheritance:

Node and TypedNode:Node

b. Andrei Alexandrescu’s Modern C++ Design suggests using “policy”, which use parameters in the child class as the parent(s) – but it needs C++ multiple-inheritance to be a general mechanism; also, by itself, it is not possible in .Net.

c. The “Curiously Recurring Generics Pattern” use parameters in the parent class as the child class. This is especially interesting when you see that “parent” is just an interface (see the last example in the link http://www.artima.com/forums/flat.jsp?forum=106&thread=133275 ).

d. /// array of subclass is a subclass of array of class/// (note: in java this is not the case; C# "fixed" it/// but now generics it is "broken" again

I checked the Java’s “general tutorial” section “generics and subtyping”, it has explanation why this is the case.
http://java.sun.com/j2se/1.5.0/docs/guide/language/
or directly:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

---------------------------------------
---------------------------------------

----4. (from CSLA) http://www.rockfordlhotka.org/WeBlog/PermaLink.aspx?guid=10eb26b4-f52d-4ea9-9cb8-ac8c103ee1f2

----5. (from CSLA) http://forums.lhotka.net/forums/thread/5168.aspx

----6. Curiously Recurring Generic Pattern

http://www.artima.com/forums/flat.jsp?forum=106&thread=133275

----7. Factory pattern and generics
(note: Andrei Alexandrescu’s Modern C++ Design and the link the top of this post
also have this)

http://weblogs.asp.net/pgielens/archive/2004/07/01/171183.aspx

http://weblogs.asp.net/gmilano/archive/2005/07/29/420980.aspx

http://www.developerdotstar.com/mag/articles/troche_factorychain.html


------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
--------------Books on generics, aop, code generation, and generative programming

1. The title of this blog is about generics and aop. I did not mention “code generation” and “generative programming”, because I cannot put “design patterns” after “code generations”. In other words, my focus is on the "thinking at coding time". However, from literature point of view, “code generation” is definitely in the same line – the same line of “generative programming”. Also, by adding “code generation” and “generative programming”, it is clearer that why “generics” and “aop” are related.

2. Patterns and idioms: Nowadays all languages/platforms are interchangeable: C# and VB.Net are interchangeable; C# and Java are even more so; Java/C# and C++ are very close also. However, there are some "headaches" -- some idiomatic issues must be resolved.

(a) The multiple-inheritance in C++ needs to be simulated or replaced by some mechanisms. This is always a big problem, and sometimes needs a lot of work.

(b) C++ (and Java) generics is a kind of "structured code generation", while .Net is not. So, ideas from C++ generics has to be “digested” by all three things in .Net: generics per se, AOP, and code generation per se. Note that although code generation is very powerful; code generation must be treated as the last resort, for obvious reasons. As a result, as C#/VB programmers, we need to pay attention to using the build-in generics and “emit” techniques (ubiquitous factory pattern plus necessary attributes) to realize the same effects of those powerful ideas.

3. Generics, AOP, and code generation do not require any "frameworks": they are the old plain sweet low-profile “library” approach. It can support any frameworks; it also supports the “no framework” approach.

---------------------------Here is the list

Generative Programming: Methods, Tools, and Applications
(Fun read. You will know the sources of all the catching words! Academic and review book)
http://www.amazon.com/Generative-Programming-Methods-Tools-Applications/dp/0201309777/sr=1-1/qid=1159062124/ref=pd_bbs_1/102-3899147-0464903?ie=UTF8&s=books


C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
(C++ book; necessary to get the perspective)
http://www.amazon.com/C++-Template-Metaprogramming-Concepts-Techniques/dp/0321227255


Modern C++ Design: Generic Programming and Design Patterns Applied
(C++ book; the corner stone book; read it very carefully)
http://www.amazon.com/Modern-C++-Design-Programming-Patterns/dp/0201704315


C++ Templates: The Complete Guide
(C++ book; its chapters on "design" are especially useful)
http://www.amazon.com/C++-Templates-Complete-David-Vandevoorde/dp/0201734842/ref=pd_sim_b_5/102-3899147-0464903?ie=UTF8

-------------
AspectJ in Action: Practical Aspect-Oriented Programming
(java book; however, it is the best aop book. After reading it, you can look into .Net's Castle)
http://www.amazon.com/AspectJ-Action-Practical-Aspect-Oriented-Programming/dp/1930110936/sr=1-1/qid=1159063248/ref=pd_bbs_1/102-3899147-0464903?ie=UTF8&s=books

------------

Code Generation in Action
(using ruby, with a java perspective)
http://www.amazon.com/Code-Generation-Action-Jack-Herrington/dp/1930110979/ref=pd_sim_b_4/102-3899147-0464903?ie=UTF8


Code Generation in Microsoft .NET
http://www.amazon.com/Generation-Microsoft-NET-Kathleen-Dollard/dp/1590591372/ref=pd_sim_b_4/102-3899147-0464903?ie=UTF8

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home