Encapsulate Behavior, Not Just State

Einar Landre

iN SYSTEMS THEORY, containment is one of the most useful constructs when dealing with large and complex system structures. In the software industry, the value of containment or encapsulation is well understood. Containment is supported by programming language constructs such as subroutines and functions, modules and packages, classes, and so on.

Modules and packages address the larger-scale needs for encapsulation, while classes, subroutines, and functions address the more fine-grained aspects of the matter. Over the years, I have discovered that classes seem to be one of the hardest encapsulation constructs for developers to get right. It’s not uncom- mon to find a class with a single 3,000-line main method, or a class with only set and get methods for its primitive attributes. These examples demonstrate that the developers involved have not fully understood object-oriented think- ing, having failed to take advantage of the power of objects as modeling con- structs. For developers familiar with the terms POJO (Plain Old Java Object) and POCO (Plain Old C# Object or Plain Old CLR Object), this was the intent in going back to the basics of OO as a modeling paradigm—the objects are plain and simple, but not dumb.

An object encapsulates both state and behavior, where the behavior is defined by the actual state. Consider a door object. It has four states: closed, open, closing, opening. It provides two operations: open and close. Depending on

??64 97 Things Every Programmer Should Know

?

???????????????the state, the open and close operations will behave differently. This inherent property of an object makes the design process conceptually simple. It boils down to two simple tasks: allocation and delegation of responsibility to the different objects including the interobject interaction protocols.

How this works in practice is best illustrated with an example. Let’s say we have three classes: Customer, Order, and Item. A Customer object is the natural place- holder for the credit limit and credit validation rules. An Order object knows about its associated Customer, and its addItem operation delegates the actual credit check by calling customer.validateCredit(item.price()). If the postcondition for the method fails, an exception can be thrown and the purchase aborted.

Less experienced object-oriented developers might decide to wrap all the busi- ness rules into an object very often referred to as OrderManager or OrderService. In these designs, Order, Customer, and Item are treated as little more than record types. All logic is factored out of the classes and tied together in one large, procedural method with a lot of internal if-then-else constructs. These meth- ods are easily broken and are almost impossible to maintain. The reason? The encapsulation is broken.

So, in the end, don’t break the encapsulation, and use the power of your pro- gramming language to maintain it.

时间: 2024-10-08 16:50:00

Encapsulate Behavior, Not Just State的相关文章

ARTS Week 13

Jan 20, 2020 ~ Jan 26, 2020 Algorithm Problem 141 Linked List Cycle (环形链表) 题目链接 题目描述:给定一个链表,判断链表中是否存在环形.pos 代表链表末尾指向的连接到链表的位置,从0开始.若 pos = -1 则表示链表中没有环(注意:输入的只有链表本身,没有 pos) 举例: Input: head = [3,2,0,-4] (pos = 1) Output: true 3 -> 2 -> 0 -> -4 \ /

Important Programming Concepts (Even on Embedded Systems) Part V: State Machines

Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part IV: Singletons Oh, hell, this article just had to be about state machines, didn’t it? State machines! Those damned little circles and arrows and q’s.

Domain Driven Design and Development In Practice--转载

原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about mapping business domain concepts into software artifacts. Most of the writings and articles on this topic have been based on Eric Evans' book "Domain Dr

C++ Design Pattern: What is a Design Pattern?

Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when developing software within a particular context. Each pattern describes a problem which occurs over and over again in our environment, and then describes

HeadFirst设计模式之状态模式

一. 1. 2.The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. 3.The State Pattern allows an object to have many different behaviors that are based on its internal state.

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

jquery.pjax.js bug问题解决集锦

jquery.pjax 是一个很好的局部刷新插件,但实际应用过程是还是会有很多小问题,部分问题解决如下: 1.pjax 局部加载时候,IE 存在缓存问题,很容易理解,pjax是通过jquery的ajax加载局部内容的,默认cache=true,这会导致ie下get数据从缓存中获取,解决办法是设置pjax options的cache=false,这样请求会自动变成如下方式: /XXXX?_pjax=%23pjax-container&_=1455092848927 2.pjax 与 jquery

Android Design Support Library详解(SnackBar、NavigationView、FloatActionButton等)

Material Design 设计风格非常受欢迎,那么支持其效果的Design Support Library(Android 2.1  API  level 7及其以上)库又有哪些控件呢.主要包括SnackBar.Navigation View.FloatActionbutton.CoordinatorLayout.CollapsingToolBarLayout等.我在Git上看见一个非常炫的效果 谷歌官网介绍:http://android-developers.blogspot.com.e

【游戏设计模式】之三 状态模式、有限状态机 & Unity版本实现

本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/52824776 作者:毛星云(浅墨)    微博:http://weibo.com/u/1723155442 游戏开发过程中,各种游戏状态的切换无处不在.但很多时候,简单粗暴的if else加标志位的方式并不能很地道地解决状态复杂变换的问题,这时,就可以运用到状态模式以及状态机来高效地完成任务.状态模式与状态机,因为他们关联紧密,常