开发者

How do you call this pattern?

开发者 https://www.devze.com 2023-01-03 22:39 出处:网络
What do you call the design where the object\'s constructor is responsible for all/any subsequent actions. Usage of this class involves simply creating an instance and then it\'s all fire-and-forget.

What do you call the design where the object's constructor is responsible for all/any subsequent actions. Usage of this class involves simply creating an instance and then it's all fire-and-forget.

A silly example:

public class Order
{
   public Order(int ammount,Product type)
   {
       Ammount = ammount;
       Namespace.OrderManager.RegisterNewOrder(this);
       Namespace.WarehouseManager.Substract(this);          
       Namespace.OrderManager.Invoice(this);
       Namespace.DeliveryManager.Deliver(this);
       .. well, you get the point;

   }

   // Called back later from DeliveryManager
   public void OrderHasBeenDelivered()
   { 
       //save some data to the DB, or notify the UI
   }

   // Called back later from OrderManager
   public void OrderHasBeenCanceled()
   {
       Namespace.DeliveryManager.CancelDelivery(this);
   } 
}

... usage of the Order class :

   public void CreateOrder_click(object sender, EventArgs e)
  {
            new Order(50, CDs);
            new Order(10, DVDs);
            new Order(10, DVDs);

  } 
开发者_开发技巧

Edit:

Well, the difference between using this and a simple static method, is that the newly created Order object is going to be used in many different places, just not by the function/thread/object that created it.

I simply create the order object, it registers itself with the OrderManager, then the OrderManager will close the order at a later time. I don't throw the object away per-se, it will continue to exist in the app.


  • "A Bad Idea"?
  • "Untestable"?
  • "Procedural mess"?
  • "The Anti-Object-Oriented Anti-Pattern"?


This looks more suited for a (static) method than creating an instance of a class which you then throw away!

I am not sure if this has a name, I am hoping it doesn't! Maybe this is a well known anti-pattern...

...check this out: http://geoffrey.vandiest.biz/post/Anti-Pattern-3-Overloaded-Constructor.aspx


This is no pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消