OOPS Concept in PHP in HINDI | Method Chaining | how to chain methods a...

seen from United Kingdom

seen from United States

seen from United States
seen from United States

seen from China

seen from United States
seen from Russia
seen from Germany
seen from China

seen from United States

seen from Malaysia
seen from United States
seen from TΓΌrkiye
seen from China
seen from Algeria
seen from China
seen from China

seen from United States
seen from United States
seen from Germany
OOPS Concept in PHP in HINDI | Method Chaining | how to chain methods a...

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch β’ No registration required β’ HD streaming
jQuery Method Chaining
jQuery Method Chaining
In jQuery Method Chaining, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element(s). This way, browsers do not have to find the same element(s) more than once.
To chain an action, you simply append the action to the previous action. The following example chains together the css(), slideUp(), andβ¦
View On WordPress
Fluent interface in python
Fluent Interface is an implementation of API which improves readability.
Example
Poem('The Road Not Taken').indent(4).suffix('Robert Frost').
Fluent Interface is similar to method chaining. I was wondering how to implement this in Python. Returning self during method call seemed good idea .
class Poem(object): def __init__(self, content): self.content = content def indent(self, spaces=4): self.content = " " * spaces + self.content return self def suffix(self, content): self.content += " - {}".format(content) return self def __str__(self): return self.content >>>print Poem('Road Not Taken').indent(4).suffix('Rober Frost').content Road Not Taken - Rober Frost
Everything seems to be ok here.
Side effects
Above approach has side effect. We are mutating the same object during every method call. Consider the following code
>>>p = Poem('Road Not Taken') >>>q = p.indent(4) >>>r = p.indent(2) >>>print str(q) == str(r) True >>>print id(q), id(r) 4459640464 4459640464
Clearly this isn't expected. q and r is pointing to same instance. Ideally we should create a new instance during every method call and return the object.
New object
Decorator is a function which takes a function as argument and returns a function (most cases).
from functools import wraps def newobj(method): @wraps(method) # Well, newobj can be decorated with function, but we will cover the case # where it decorated with method def inner(self, *args, **kwargs): obj = self.__class__.__new__(self.__class__) obj.__dict__ = self.__dict__.copy() method(obj, *args, **kwargs) return obj return inner class NPoem(object): def __init__(self, content): self.content = content @newobj def indent(self, spaces=4): self.content = " " * spaces + self.content @newobj def suffix(self, content): self.content += " - {}".format(content) def __str__(self): return self.content >>>p = NPoem("foo") >>>q = p.indent(4) >>>r = p.indent(2) >>>print str(q) == str(r) False >>>print(q) foo >>>print(r) foo >>>print id(q), id(r) 4459642640 4459639248
In the above approach newobj decorator creates a new instance, copies all the atrributes, calls the method with arguments and returns new instance. Rather than using decorator private function can do the same.
Fluent Interface is used heavily in SQLAlchemy and Django. Django uses _clone method to create new QuerySet and SQLAlchemy uses decorator based approach to create new Query instance.
Thanks Mike Bayer and Daniel Roy Greenfeld helping me understand this.
Do you know any other way to implement this ? Feel free to comment.
λ°λ©ν λ₯΄μ λ²μΉ Law of Demeter
λ°λ©ν λ₯΄λ κ·Έλ¦¬μ€ μ νμ λμ€λ μΆμμ μ μ΄λ€. λ‘λ§μ νμμλ μΈλ μ€ Ceres λΌκ³ λΆλ¦¬λ λ°λ‘ κ·Έ μ . νμ§λ§, κ·Έ λ°λ©ν λ₯΄νκ³ μ΄ λ²μΉνκ³ λ μκ΄μλ€λ κ²μ΄ ν¨μ . [μν€νΌλμ]1μμλ λ°λ©ν λ₯΄μ λ²μΉμ μλμ κ°μ΄ μ μνκ³ μλ€.
λ°λ©ν λ₯΄μ λ²μΉμμλ μ΄λ€ κ°μ²΄ Oμ λ©μλ mλ λ€μκ³Ό κ°μ μ’ λ₯μ κ°μ²΄μ μλ λ©μλλ€λ§ μ€νμν¬ μ μλ€.
O μ체
m μ λ³μ
m μμμ λ§λ€μ΄μ§ κ°μ²΄
Oκ° μ§μ κ΄λ¦¬νλ μ½€ν¬λνΈ κ°μ²΄
mμ μ€μ½ν μμμ Oκ° μ κ·Ό κ°λ₯ν μ μλ³μ
μ’ λ§μ΄ μ΄λ €μ΄λ°, Richard Carrμ [The Law of Demeter]2 ν¬μ€νΈμ μ’ λ μ¬μ΄ μ€λͺ μ΄ μλ€.
μ΄λ€ ν΄λΌμ€μ λ©€λ² β λ©μλ λλ μμ± β λ λ°λμ λ€μκ³Ό κ°μ κ°μ²΄λ€μ λ©€λ²λ€λ§μ μ€νμμΌμΌ νλ€:
ν΄λΉ λ©μλ λλ μμ±μ΄ μ μΈλ κ°μ²΄
λ©μλμ νλΌλ―Έν°λ‘ 보λ΄μ§ κ°μ²΄
λ©μλ λλ μμ±μ΄ μ§μ μ΄κΈ°νμν¨ κ°μ²΄
νΈμΆμ μν λ©μλ λλ μμ±μΌλ‘μ κ°μ ν΄λΌμ€ μμμ μ μΈλ κ°μ²΄
μ μ κ°μ²΄
μλ μμ μ½λλ₯Ό 보μ. ASP.NET MVC μΉμ¬μ΄νΈλ₯Ό κ°λ°νλ€λ³΄λ©΄ μ½νΈλ‘€λ¬μμ νν λ³Ό μ μλ μν©μ΄λ€.
public class ProductController : Controller { private IProductService _service; public ProductController(IProductService service) { this._service = service; } public ActionResult Index() { var products = this._service.Repository.Get(); return View(products); } } public class ProductService : IProductService { public ProductService(IProductRepository repository) { this.Repository = repository; } public IProductRepository Repository { get; private set; } }
μμ μ½λμμ Index μ‘μ μ 보면 λλ΅ μμμ΄ κ°λ₯νκ² μ§λ§ ProductServiceλΌλ μλΉμ€ λ μ΄μ΄ μμμ ProductRepositoryλΌλ λ°μ΄ν° 리ν¬μ§ν 리 ν¨ν΄μ ν΅ν΄ CRUDλ₯Ό ꡬννκ³ μλ€. Index μ‘μ μ μ 체 μ ν 리μ€νΈλ₯Ό 보μ¬μ£Όλ λ·°λ₯Ό κ°κ³ μμ΄μ μ 체 μ ν 리μ€νΈλ μλΉμ€ μμ ꡬνλ 리ν¬μ§ν 리μ Get λ©μλλ₯Ό ν΅ν΄ κ°μ Έμ€κ² λλ€. μ΄λ κ² λ©μλ 체μ΄λμ νλ κ²μ΄ λ°λ‘ λ°λ©ν λ₯΄μ λ²μΉμ μλ°νλ κ²μ΄ λλ€. ProductController κ°μ²΄λ μμ±μλ₯Ό ν΅ν΄ λ³μλ‘ λ°μ ProductService κ°μ²΄μ λ©μλ λλ μμ±μ νΈμΆν΄μΌ νμ§ κ·Έ λ΄λΆμ μλ ProductRepository κ°μ²΄μ Get λ©μλλ₯Ό μ§μ νΈμΆν΄μλ μλλ€. ProductRepository κ°μ²΄μ νμ¬ μνκ° nullμ΄λΌλ©΄ ν΄λΉ μ½λλ NullReferenceExceptionμ λμ§κΈ° λλ¬Έμ΄λ€. λ°λΌμ ProductService ν΄λΌμ€ μμ μΆκ°μ μΈ λ©μλλ₯Ό μ μΈν΄μ£Όλ λ°©μμΌλ‘ 리ν©ν λ§μ ν΄μΌ νλ€.
public class ProductController : Controller { private IProductService _service; public ProductController(IProductService service) { this._service = service; } public ActionResult Index() { var products = this._service.GetProducts(); return View(products); } } public class ProductService : IProductService { private IProductRepository _repository; public ProductService(IProductRepository repository) { this._repository = repository; } public IList<Product> GetProducts() { return this._repository.Get(); } }
μ¦ ProductRepository κ°μ²΄λ₯Ό public μμ±μ΄λ νλλ‘ λλ κ²μ΄ μλλΌ λ΄λΆμ μΌλ‘ encapsulation μν€κ³ ProductRepository ν΄λΌμ€μ λ©€λ²λ ProductService ν΄λΌμ€μ λ©€λ²λ₯Ό ν΅ν΄ νΈμΆνλ λ°©μμΌλ‘ νκ² λλ©΄, ProductController ν΄λΌμ€λ μ§μ μ μΌλ‘ κ΄λ ¨μ΄ μλ μ½€ν¬λνΈμΈ ProductSerivceμ λν΄μλ§ ν΅μ κΆμ κ°μ§ μ μμ΄μ λ³΄λ€ μμ νκ³ μ μ°ν μ½λλ₯Ό μμ±ν μ μκ² λλ€.
μ΄λ°μμΌλ‘ λ©μλ 체μ΄λμ μ΅λν μ€μ΄λ κ²μ΄ λ°λμ§ν κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ΄λΌκ³ ν μ μκ² λ€. νμ§λ§ μ΄λ κ² νλ‘κ·Έλλ°μ νκ² λλ©΄ μΆκ°μ μΈ λ©μλλ₯Ό μμ±ν΄μΌ νλ λΆλ΄μ΄ μκΈ°κ² λλλ°, μ΄κ²μ΄ κΌ λΆμ μ μ΄λΌκ³ λ ν μ μλ κ²μ΄ κ°μ²΄ μ¬μ΄μ μμ‘΄μ±μ μ΅μννλ λ°©μμΌλ‘ μ μ°νκ² κ°λ°μ ν μ μκΈ° λλ¬Έμ΄λ€.
λ¨, LINQλ₯Ό μ°λ μν©μ΄λΌλ©΄ μκΈ°κ° λ¬λΌμ§λ€. LINQμμλ νΉμ±μ λ©μλ 체μ΄λμ΄ νμμΌ μ λ°μ μλμ§λΌ, μ΄ λ°λ©ν λ₯΄μ λ²μΉμμ λ²μ΄λ μ μλλ°, κ·Έ μ΄μ λ λ©μλ 체μ΄λμ νλ κ²κ³Ό μκ΄μμ΄ νμ 리ν΄νμ μ΄ λμΌνκΈ° λλ¬Έμ΄λ€. μμ μμ μ½λμ λμ¨ ProductRepository ν΄λΌμ€μ Get λ©μλλ μλ§λ λ΄λΆμ μΌλ‘ μλμ κ°μ΄ ꡬνμ΄ λμ΄ μμ κ²μ΄λ€.
public class ProductRepository : IProductRepository { private CompanyDataContext _context; public ProductRepository(ICompanyDataContext context) { this._context = context as CompanyDataContext; } public IList<Product> Get() { return this._context .Products .Where(p => p.IsActive) .OrderBy(p => p.DateRegistered) .ToList(); } }
μ¬κΈ°μ Products, Where κ·Έλ¦¬κ³ OrderByλ λͺ¨λ λμΌν IEnumerable<Product> κ°μ²΄λ₯Ό λ°ννλ€. μ¦ LINQλ₯Ό μ΄μ©ν λ©μλ 체μ΄λμ κ²½μ° λ©μλλ§λ€ λμΌν λ°μ΄ν° νμ μ λ°ννκΈ° λλ¬Έμ μ΄ λ°λ©ν λ₯΄μ λ²μΉμ μλ°νμ§ μκ³ μμ νκ² μ¬μ©ν μ μλ€.
μ°Έμ‘°:
[Law of Demeter]1 from Wikipedia
[The Law of Demeter]2 by Richard Carr
http://en.wikipedia.org/wiki/Law_of_DemeterΒ β©οΈ β©οΈ
http://www.blackwasp.co.uk/LawOfDemeter.aspxΒ β©οΈ β©οΈ
Eric Feminella explains how to use the method chaining pattern to both simplify your API and make it more fluent.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch β’ No registration required β’ HD streaming