Tutorials to .com

Tutorials to .com » Jsp » Skills » Java design patterns using UML to prepare examples of FactoryMethod Pattern

Java design patterns using UML to prepare examples of FactoryMethod Pattern

Print View , by: iSee ,Total views: 39 ,Word Count: 309 ,Date: Wed, 26 Aug 2009 Time: 4:07 AM

Taken from the college a long time
Read the article on the forums, read FactoryMethod Pattern UML diagrams, wrote a small example program. As the article added!
/ / Creator.java

public abstract class Creator
(
/ **
* Looks like a factory
* Contains some products and some process methods
* /
protected Product duct;
abstract String processProduct ();
abstract String processProductSpecial ();
public String Operation ()
(
return processProduct (); / / FactoryMethod for implemention;
)
)

/ / ConcreteProduct.java

public class ConcreteProduct extends Product
(
public ConcreteProduct ()
(
System.out.println ( "construct the ConcreteProduct object");
)
public String makeString (String str)
(
return "This is the product after processing:" + str;
)
)

/ / Product.java
public abstract class Product
(
public abstract String makeString (String str);
)

/ / ConcreteCreatorA.java

public class ConcreteCreatorA extends Creator
(
/ **
* You can write some other class like this to override
* The same methods do diffrent thing;
* /
public ConcreteCreatorA ()
(
duct = new ConcreteProduct ();
System.out.println ( "instanced a product");

)
public String processProduct ()
(
System.out.println ( "product A:");
return duct.makeString ( "Make @ # a product");

)
public String processProductSpecial ()
(
System.out.println ( "special product B:");
/ / add another method here
return duct.makeString ( "Make Special @ # B product");
)
)



/ / TestFactoryMethod.java
public class TestFactoryMethod
(
public TestFactoryMethod ()
(
)
public static void main (String [] arg)
(
ConcreteCreatorA C++ = new ConcreteCreatorA ();
System.out.println (cc.Operation ());
System.out.println (cc.processProductSpecial ());
)
)


Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.